source: sample/hadoop-0.16/tw/org/nchc/tuple/HashMapWritable.java @ 123

Last change on this file since 123 was 26, checked in by waue, 16 years ago

No any Warnning
@SuppressWarnings?("unused")
Just let it no more warning

File size: 2.4 KB
Line 
1
2package tw.org.nchc.tuple;
3
4import java.io.DataInput;
5import java.io.DataOutput;
6import java.io.IOException;
7import java.util.Map;
8import java.util.Set;
9import java.util.HashMap;
10
11
12import org.apache.hadoop.io.Writable;
13@SuppressWarnings("unchecked") 
14public class HashMapWritable<K extends Writable, V extends Writable> extends HashMap<K, V> implements
15        Writable {
16
17  /**
18     *
19     */
20    private static final long serialVersionUID = 1L;
21
22    /**
23   * Creates a HashMapWritable object.
24   */
25  public HashMapWritable() {
26    super();
27  }
28 
29  /**
30   * Creates a HashMapWritable object from a regular HashMap.
31   */
32  public HashMapWritable(HashMap<K, V> map) {
33    super(map);
34  }
35
36  /**
37   * Deserializes the array.
38   *
39   * @param in
40   *            source for raw byte representation
41   */
42
43  public void readFields(DataInput in) throws IOException {
44   
45    this.clear();
46
47    int numEntries = in.readInt();
48    if(numEntries==0) return;
49   
50    String keyClassName = in.readUTF();
51    String valueClassName = in.readUTF();
52   
53    K objK;
54    V objV;
55    try {
56      Class keyClass = Class.forName(keyClassName);
57      Class valueClass = Class.forName(valueClassName);
58      for (int i = 0; i < numEntries; i++) {
59        objK = (K) keyClass.newInstance();
60        objK.readFields(in);
61        objV = (V) valueClass.newInstance();
62        objV.readFields(in);
63        put(objK, objV);
64      }
65
66    } catch (ClassNotFoundException e) {
67      e.printStackTrace();
68    } catch (IllegalAccessException e) {
69      e.printStackTrace();
70    } catch (InstantiationException e) {
71      e.printStackTrace();
72    }
73   
74  }
75
76  /**
77   * Serializes this array.
78   *
79   * @param out
80   *            where to write the raw byte representation
81   */
82  public void write(DataOutput out) throws IOException {
83    // Write out the number of entries in the map
84      out.writeInt(size());
85      if(size()==0) return;
86     
87      // Write out the class names for keys and values
88      // assuming that data is homogeneuos (i.e., all entries have same types)
89      Set<Map.Entry<K, V>> entries = entrySet();
90      Map.Entry<K, V> first = entries.iterator().next();
91      K objK = first.getKey();
92      V objV = first.getValue();
93      out.writeUTF(objK.getClass().getCanonicalName());
94      out.writeUTF(objV.getClass().getCanonicalName());
95
96      // Then write out each key/value pair
97      for (Map.Entry<K, V> e: entrySet()) {
98        e.getKey().write(out);
99        e.getValue().write(out);
100      }
101  }
102
103}
Note: See TracBrowser for help on using the repository browser.