HBASE-19746 Add default impl to Cell#getType
diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/ByteBufferKeyOnlyKeyValue.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/ByteBufferKeyOnlyKeyValue.java
index db2890f..31f71f9 100644
--- a/hbase-common/src/main/java/org/apache/hadoop/hbase/ByteBufferKeyOnlyKeyValue.java
+++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/ByteBufferKeyOnlyKeyValue.java
@@ -152,10 +152,6 @@
     return ByteBufferUtils.toByte(this.buf, this.offset + this.length - 1);
   }
 
-  public Type getType() {
-    return PrivateCellUtil.toType(getTypeByte());
-  }
-
   @Override
   public void setSequenceId(long seqId) throws IOException {
     throw new IllegalArgumentException("This is a key only Cell");
diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/Cell.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/Cell.java
index f208625..8cdefff 100644
--- a/hbase-common/src/main/java/org/apache/hadoop/hbase/Cell.java
+++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/Cell.java
@@ -201,10 +201,19 @@
   int getTagsLength();
 
   /**
-   * Returns the type of cell in a human readable format using {@link Type}
+   * Returns the type of cell in a human readable format using {@link Type}.
+   * Note : This does not expose the internal types of Cells like {@link KeyValue.Type#Maximum} and
+   * {@link KeyValue.Type#Minimum}
    * @return The data type this cell: one of Put, Delete, etc
    */
-  Type getType();
+  default Type getType() {
+    byte byteType = getTypeByte();
+    Type t = Type.CODE_ARRAY[byteType & 0xff];
+    if (t != null) {
+      return t;
+    }
+    throw new UnsupportedOperationException("Invalid type of cell " + byteType);
+  }
 
   /**
    * The valid types for user to build the cell. Currently, This is subset of {@link KeyValue.Type}.
@@ -229,5 +238,13 @@
     public byte getCode() {
       return this.code;
     }
+
+    private static final Type[] CODE_ARRAY = new Type[256];
+
+    static {
+      for (Type t : Type.values()) {
+        CODE_ARRAY[t.code & 0xff] = t;
+      }
+    }
   }
 }
diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/ExtendedCell.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/ExtendedCell.java
index be6d96c..07b0e3f 100644
--- a/hbase-common/src/main/java/org/apache/hadoop/hbase/ExtendedCell.java
+++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/ExtendedCell.java
@@ -163,17 +163,6 @@
   int getTagsLength();
 
   /**
-   * {@inheritDoc}
-   * <p>
-   * Note : This does not expose the internal types of Cells like {@link KeyValue.Type#Maximum} and
-   * {@link KeyValue.Type#Minimum}
-   */
-  @Override
-  default Type getType() {
-    return PrivateCellUtil.toType(getTypeByte());
-  }
-
-  /**
    * @return The byte representation of the KeyValue.TYPE of this cell: one of Put, Delete, etc
    */
   byte getTypeByte();
diff --git a/hbase-common/src/test/java/org/apache/hadoop/hbase/TestCellUtil.java b/hbase-common/src/test/java/org/apache/hadoop/hbase/TestCellUtil.java
index 60a1d55..1f95db9 100644
--- a/hbase-common/src/test/java/org/apache/hadoop/hbase/TestCellUtil.java
+++ b/hbase-common/src/test/java/org/apache/hadoop/hbase/TestCellUtil.java
@@ -21,6 +21,7 @@
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
@@ -36,6 +37,7 @@
 import org.junit.Assert;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
+import org.mockito.Mockito;
 
 @Category({MiscTests.class, SmallTests.class})
 public class TestCellUtil {
@@ -195,11 +197,6 @@
       // TODO Auto-generated method stub
       return 0;
     }
-
-    @Override
-    public Type getType() {
-      return PrivateCellUtil.toType(getTypeByte());
-    }
   }
 
   /**
@@ -382,7 +379,7 @@
         HConstants.EMPTY_BYTE_ARRAY);
     cellToString = CellUtil.getCellKeyAsString(cell);
     assertEquals(kv.toString(), cellToString);
-    
+
   }
 
   @Test
@@ -522,6 +519,30 @@
     assertTrue(CellUtil.equals(kv, res));
   }
 
+  @Test
+  public void testGetType() throws IOException {
+    Cell c = Mockito.mock(Cell.class);
+    Mockito.when(c.getType()).thenCallRealMethod();
+    for (Cell.Type type : Cell.Type.values()) {
+      Mockito.when(c.getTypeByte()).thenReturn(type.getCode());
+      assertEquals(type, c.getType());
+    }
+
+    try {
+      Mockito.when(c.getTypeByte()).thenReturn(KeyValue.Type.Maximum.getCode());
+      c.getType();
+      fail("The code of Maximum can't be handled by Cell.Type");
+    } catch(UnsupportedOperationException e) {
+    }
+
+    try {
+      Mockito.when(c.getTypeByte()).thenReturn(KeyValue.Type.Minimum.getCode());
+      c.getType();
+      fail("The code of Maximum can't be handled by Cell.Type");
+    } catch(UnsupportedOperationException e) {
+    }
+  }
+
   private static class NonExtendedCell implements Cell {
     private KeyValue kv;
 
@@ -618,10 +639,5 @@
     public int getTagsLength() {
       return this.kv.getTagsLength();
     }
-
-    @Override
-    public Type getType() {
-      return PrivateCellUtil.toType(getTypeByte());
-    }
   }
 }
diff --git a/hbase-common/src/test/java/org/apache/hadoop/hbase/TestKeyValue.java b/hbase-common/src/test/java/org/apache/hadoop/hbase/TestKeyValue.java
index bd45a09..7c33ff8 100644
--- a/hbase-common/src/test/java/org/apache/hadoop/hbase/TestKeyValue.java
+++ b/hbase-common/src/test/java/org/apache/hadoop/hbase/TestKeyValue.java
@@ -737,10 +737,5 @@
     public byte[] getTagsArray() {
       return this.kv.getTagsArray();
     }
-
-    @Override
-    public Type getType() {
-      return PrivateCellUtil.toType(getTypeByte());
-    }
   }
 }
diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/fs/HFileSystem.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/fs/HFileSystem.java
index 0723f85..b89470f 100644
--- a/hbase-server/src/main/java/org/apache/hadoop/hbase/fs/HFileSystem.java
+++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/fs/HFileSystem.java
@@ -86,7 +86,7 @@
     this.useHBaseChecksum = useHBaseChecksum;
 
     fs.initialize(getDefaultUri(conf), conf);
-    
+
     // disable checksum verification for local fileSystem, see HBASE-11218
     if (fs instanceof LocalFileSystem) {
       fs.setWriteChecksum(false);