Browse Source

Revert "HADOOP-10465. Fix use of generics within SortedMapWritable. Contributed by Bertrand Dechoux."

This reverts commit 1d175f0f3f80a066a6de71213392d288329d837f.
Akira Ajisaka 8 years ago
parent
commit
a8cfcce542

+ 27 - 20
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/SortedMapWritable.java

@@ -36,15 +36,15 @@ import org.apache.hadoop.util.ReflectionUtils;
  */
  */
 @InterfaceAudience.Public
 @InterfaceAudience.Public
 @InterfaceStability.Stable
 @InterfaceStability.Stable
-public class SortedMapWritable<K extends WritableComparable<? super K>> extends AbstractMapWritable
-  implements SortedMap<K, Writable> {
+public class SortedMapWritable extends AbstractMapWritable
+  implements SortedMap<WritableComparable, Writable> {
   
   
-  private SortedMap<K, Writable> instance;
+  private SortedMap<WritableComparable, Writable> instance;
   
   
   /** default constructor. */
   /** default constructor. */
   public SortedMapWritable() {
   public SortedMapWritable() {
     super();
     super();
-    this.instance = new TreeMap<K, Writable>();
+    this.instance = new TreeMap<WritableComparable, Writable>();
   }
   }
   
   
   /**
   /**
@@ -52,39 +52,45 @@ public class SortedMapWritable<K extends WritableComparable<? super K>> extends
    * 
    * 
    * @param other the map to copy from
    * @param other the map to copy from
    */
    */
-  public SortedMapWritable(SortedMapWritable<K> other) {
+  public SortedMapWritable(SortedMapWritable other) {
     this();
     this();
     copy(other);
     copy(other);
   }
   }
 
 
   @Override
   @Override
-  public Comparator<? super K> comparator() {
+  public Comparator<? super WritableComparable> comparator() {
     // Returning null means we use the natural ordering of the keys
     // Returning null means we use the natural ordering of the keys
     return null;
     return null;
   }
   }
 
 
   @Override
   @Override
-  public K firstKey() {
+  public WritableComparable firstKey() {
     return instance.firstKey();
     return instance.firstKey();
   }
   }
 
 
   @Override
   @Override
-  public SortedMap<K, Writable> headMap(K toKey) {
+  public SortedMap<WritableComparable, Writable>
+  headMap(WritableComparable toKey) {
+    
     return instance.headMap(toKey);
     return instance.headMap(toKey);
   }
   }
 
 
   @Override
   @Override
-  public K lastKey() {
+  public WritableComparable lastKey() {
     return instance.lastKey();
     return instance.lastKey();
   }
   }
 
 
   @Override
   @Override
-  public SortedMap<K, Writable> subMap(K fromKey, K toKey) {
+  public SortedMap<WritableComparable, Writable>
+  subMap(WritableComparable fromKey, WritableComparable toKey) {
+    
     return instance.subMap(fromKey, toKey);
     return instance.subMap(fromKey, toKey);
   }
   }
 
 
   @Override
   @Override
-  public SortedMap<K, Writable> tailMap(K fromKey) {
+  public SortedMap<WritableComparable, Writable>
+  tailMap(WritableComparable fromKey) {
+    
     return instance.tailMap(fromKey);
     return instance.tailMap(fromKey);
   }
   }
 
 
@@ -104,7 +110,7 @@ public class SortedMapWritable<K extends WritableComparable<? super K>> extends
   }
   }
 
 
   @Override
   @Override
-  public Set<Map.Entry<K, Writable>> entrySet() {
+  public Set<java.util.Map.Entry<WritableComparable, Writable>> entrySet() {
     return instance.entrySet();
     return instance.entrySet();
   }
   }
 
 
@@ -119,21 +125,22 @@ public class SortedMapWritable<K extends WritableComparable<? super K>> extends
   }
   }
 
 
   @Override
   @Override
-  public Set<K> keySet() {
+  public Set<WritableComparable> keySet() {
     return instance.keySet();
     return instance.keySet();
   }
   }
 
 
   @Override
   @Override
-  public Writable put(K key, Writable value) {
+  public Writable put(WritableComparable key, Writable value) {
     addToMap(key.getClass());
     addToMap(key.getClass());
     addToMap(value.getClass());
     addToMap(value.getClass());
     return instance.put(key, value);
     return instance.put(key, value);
   }
   }
 
 
   @Override
   @Override
-  public void putAll(Map<? extends K, ? extends Writable> t) {
-    for (Map.Entry<? extends K, ? extends Writable> e:
+  public void putAll(Map<? extends WritableComparable, ? extends Writable> t) {
+    for (Map.Entry<? extends WritableComparable, ? extends Writable> e:
       t.entrySet()) {
       t.entrySet()) {
+      
       put(e.getKey(), e.getValue());
       put(e.getKey(), e.getValue());
     }
     }
   }
   }
@@ -165,8 +172,8 @@ public class SortedMapWritable<K extends WritableComparable<? super K>> extends
     // Then read each key/value pair
     // Then read each key/value pair
     
     
     for (int i = 0; i < entries; i++) {
     for (int i = 0; i < entries; i++) {
-      K key =
-        (K) ReflectionUtils.newInstance(getClass(
+      WritableComparable key =
+        (WritableComparable) ReflectionUtils.newInstance(getClass(
             in.readByte()), getConf());
             in.readByte()), getConf());
       
       
       key.readFields(in);
       key.readFields(in);
@@ -189,7 +196,7 @@ public class SortedMapWritable<K extends WritableComparable<? super K>> extends
     
     
     // Then write out each key/value pair
     // Then write out each key/value pair
     
     
-    for (Map.Entry<K, Writable> e: instance.entrySet()) {
+    for (Map.Entry<WritableComparable, Writable> e: instance.entrySet()) {
       out.writeByte(getId(e.getKey().getClass()));
       out.writeByte(getId(e.getKey().getClass()));
       e.getKey().write(out);
       e.getKey().write(out);
       out.writeByte(getId(e.getValue().getClass()));
       out.writeByte(getId(e.getValue().getClass()));
@@ -204,7 +211,7 @@ public class SortedMapWritable<K extends WritableComparable<? super K>> extends
     }
     }
 
 
     if (obj instanceof SortedMapWritable) {
     if (obj instanceof SortedMapWritable) {
-      Map<?,?> map = (Map<?,?>) obj;
+      Map map = (Map) obj;
       if (size() != map.size()) {
       if (size() != map.size()) {
         return false;
         return false;
       }
       }

+ 18 - 19
hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/io/TestSortedMapWritable.java

@@ -45,7 +45,7 @@ public class TestSortedMapWritable {
         new BytesWritable("value3".getBytes())
         new BytesWritable("value3".getBytes())
     };
     };
 
 
-    SortedMapWritable<Text> inMap = new SortedMapWritable<Text>();
+    SortedMapWritable inMap = new SortedMapWritable();
     for (int i = 0; i < keys.length; i++) {
     for (int i = 0; i < keys.length; i++) {
       inMap.put(keys[i], values[i]);
       inMap.put(keys[i], values[i]);
     }
     }
@@ -53,14 +53,13 @@ public class TestSortedMapWritable {
     assertEquals(0, inMap.firstKey().compareTo(keys[0]));
     assertEquals(0, inMap.firstKey().compareTo(keys[0]));
     assertEquals(0, inMap.lastKey().compareTo(keys[2]));
     assertEquals(0, inMap.lastKey().compareTo(keys[2]));
 
 
-    SortedMapWritable<Text> outMap = new SortedMapWritable<Text>(inMap);
+    SortedMapWritable outMap = new SortedMapWritable(inMap);
     assertEquals(inMap.size(), outMap.size());
     assertEquals(inMap.size(), outMap.size());
     
     
-    for (Map.Entry<Text, Writable> e: inMap.entrySet()) {
+    for (Map.Entry<WritableComparable, Writable> e: inMap.entrySet()) {
       assertTrue(outMap.containsKey(e.getKey()));
       assertTrue(outMap.containsKey(e.getKey()));
-      WritableComparable<WritableComparable<?>> aValue = (WritableComparable<WritableComparable<?>>) outMap.get(e.getKey());
-      WritableComparable<WritableComparable<?>> bValue = (WritableComparable<WritableComparable<?>>) e.getValue();
-      assertEquals(0, aValue.compareTo(bValue));
+      assertEquals(0, ((WritableComparable) outMap.get(e.getKey())).compareTo(
+          e.getValue()));
     }
     }
     
     
     // Now for something a little harder...
     // Now for something a little harder...
@@ -70,24 +69,24 @@ public class TestSortedMapWritable {
         new Text("map2")
         new Text("map2")
     };
     };
     
     
-    SortedMapWritable<Text> mapOfMaps = new SortedMapWritable<Text>();
+    SortedMapWritable mapOfMaps = new SortedMapWritable();
     mapOfMaps.put(maps[0], inMap);
     mapOfMaps.put(maps[0], inMap);
     mapOfMaps.put(maps[1], outMap);
     mapOfMaps.put(maps[1], outMap);
     
     
-    SortedMapWritable<Text> copyOfMapOfMaps = new SortedMapWritable<Text>(mapOfMaps);
+    SortedMapWritable copyOfMapOfMaps = new SortedMapWritable(mapOfMaps);
     for (int i = 0; i < maps.length; i++) {
     for (int i = 0; i < maps.length; i++) {
       assertTrue(copyOfMapOfMaps.containsKey(maps[i]));
       assertTrue(copyOfMapOfMaps.containsKey(maps[i]));
 
 
-      SortedMapWritable<Text> a = (SortedMapWritable<Text>) mapOfMaps.get(maps[i]);
-      SortedMapWritable<Text> b = (SortedMapWritable<Text>) copyOfMapOfMaps.get(maps[i]);
+      SortedMapWritable a = (SortedMapWritable) mapOfMaps.get(maps[i]);
+      SortedMapWritable b = (SortedMapWritable) copyOfMapOfMaps.get(maps[i]);
       assertEquals(a.size(), b.size());
       assertEquals(a.size(), b.size());
       for (Writable key: a.keySet()) {
       for (Writable key: a.keySet()) {
         assertTrue(b.containsKey(key));
         assertTrue(b.containsKey(key));
         
         
         // This will work because we know what we put into each set
         // This will work because we know what we put into each set
         
         
-        WritableComparable<WritableComparable<?>> aValue = (WritableComparable<WritableComparable<?>>) a.get(key);
-        WritableComparable<WritableComparable<?>> bValue = (WritableComparable<WritableComparable<?>>) b.get(key);
+        WritableComparable aValue = (WritableComparable) a.get(key);
+        WritableComparable bValue = (WritableComparable) b.get(key);
         assertEquals(0, aValue.compareTo(bValue));
         assertEquals(0, aValue.compareTo(bValue));
       }
       }
     }
     }
@@ -99,11 +98,11 @@ public class TestSortedMapWritable {
   @Test
   @Test
   @SuppressWarnings("deprecation")
   @SuppressWarnings("deprecation")
   public void testForeignClass() {
   public void testForeignClass() {
-    SortedMapWritable<Text> inMap = new SortedMapWritable<Text>();
+    SortedMapWritable inMap = new SortedMapWritable();
     inMap.put(new Text("key"), new UTF8("value"));
     inMap.put(new Text("key"), new UTF8("value"));
     inMap.put(new Text("key2"), new UTF8("value2"));
     inMap.put(new Text("key2"), new UTF8("value2"));
-    SortedMapWritable<Text> outMap = new SortedMapWritable<Text>(inMap);
-    SortedMapWritable<Text> copyOfCopy = new SortedMapWritable<Text>(outMap);
+    SortedMapWritable outMap = new SortedMapWritable(inMap);
+    SortedMapWritable copyOfCopy = new SortedMapWritable(outMap);
     assertEquals(1, copyOfCopy.getNewClasses());
     assertEquals(1, copyOfCopy.getNewClasses());
   }
   }
   
   
@@ -113,8 +112,8 @@ public class TestSortedMapWritable {
   @Test
   @Test
   public void testEqualsAndHashCode() {
   public void testEqualsAndHashCode() {
     String failureReason;
     String failureReason;
-    SortedMapWritable<Text> mapA = new SortedMapWritable<Text>();
-    SortedMapWritable<Text> mapB = new SortedMapWritable<Text>();
+    SortedMapWritable mapA = new SortedMapWritable();
+    SortedMapWritable mapB = new SortedMapWritable();
     
     
     // Sanity checks
     // Sanity checks
     failureReason = "SortedMapWritable couldn't be initialized. Got null reference";
     failureReason = "SortedMapWritable couldn't be initialized. Got null reference";
@@ -168,8 +167,8 @@ public class TestSortedMapWritable {
 
 
   @Test(timeout = 1000)
   @Test(timeout = 1000)
   public void testPutAll() {
   public void testPutAll() {
-    SortedMapWritable<Text> map1 = new SortedMapWritable<Text>();
-    SortedMapWritable<Text> map2 = new SortedMapWritable<Text>();
+    SortedMapWritable map1 = new SortedMapWritable();
+    SortedMapWritable map2 = new SortedMapWritable();
     map1.put(new Text("key"), new Text("value"));
     map1.put(new Text("key"), new Text("value"));
     map2.putAll(map1);
     map2.putAll(map1);
 
 

+ 5 - 8
hadoop-tools/hadoop-streaming/src/main/java/org/apache/hadoop/typedbytes/TypedBytesWritableInput.java

@@ -330,25 +330,22 @@ public class TypedBytesWritableInput implements Configurable {
     return readMap(null);
     return readMap(null);
   }
   }
 
 
-  public <K extends WritableComparable<? super K>>
-    SortedMapWritable<K> readSortedMap(SortedMapWritable<K> mw)
+  public SortedMapWritable readSortedMap(SortedMapWritable mw)
     throws IOException {
     throws IOException {
     if (mw == null) {
     if (mw == null) {
-      mw = new SortedMapWritable<K>();
+      mw = new SortedMapWritable();
     }
     }
     int length = in.readMapHeader();
     int length = in.readMapHeader();
     for (int i = 0; i < length; i++) {
     for (int i = 0; i < length; i++) {
-      @SuppressWarnings("unchecked")
-      K key = (K) read();
+      WritableComparable key = (WritableComparable) read();
       Writable value = read();
       Writable value = read();
       mw.put(key, value);
       mw.put(key, value);
     }
     }
     return mw;
     return mw;
   }
   }
 
 
-  public <K extends WritableComparable<? super K>> SortedMapWritable<K>
-    readSortedMap() throws IOException {
-    return readSortedMap((SortedMapWritable<K>)null);
+  public SortedMapWritable readSortedMap() throws IOException {
+    return readSortedMap(null);
   }
   }
   
   
   public Writable readWritable(Writable writable) throws IOException {
   public Writable readWritable(Writable writable) throws IOException {

+ 3 - 3
hadoop-tools/hadoop-streaming/src/main/java/org/apache/hadoop/typedbytes/TypedBytesWritableOutput.java

@@ -131,7 +131,7 @@ public class TypedBytesWritableOutput {
     } else if (w instanceof MapWritable) {
     } else if (w instanceof MapWritable) {
       writeMap((MapWritable) w);
       writeMap((MapWritable) w);
     } else if (w instanceof SortedMapWritable) {
     } else if (w instanceof SortedMapWritable) {
-      writeSortedMap((SortedMapWritable<?>) w);
+      writeSortedMap((SortedMapWritable) w);
     } else if (w instanceof Record) {
     } else if (w instanceof Record) {
       writeRecord((Record) w);
       writeRecord((Record) w);
     } else {
     } else {
@@ -200,9 +200,9 @@ public class TypedBytesWritableOutput {
     }
     }
   }
   }
 
 
-  public void writeSortedMap(SortedMapWritable<?> smw) throws IOException {
+  public void writeSortedMap(SortedMapWritable smw) throws IOException {
     out.writeMapHeader(smw.size());
     out.writeMapHeader(smw.size());
-    for (Map.Entry<? extends WritableComparable<?>, Writable> entry : smw.entrySet()) {
+    for (Map.Entry<WritableComparable, Writable> entry : smw.entrySet()) {
       write(entry.getKey());
       write(entry.getKey());
       write(entry.getValue());
       write(entry.getValue());
     }
     }