Commits

Rhys ! committed 41eeffd Draft

properly support encoding generic Maps

Comments (0)

Files changed (1)

src/org/bert_rpc/Encoder.java

         this.outputStream = new DataOutputStream(outputStream);
     }
 
-    public void encodeObject(Object object) throws IOException, EncodingException {
+    public void encodeObject(Object object)
+            throws IOException, EncodingException {
         this.outputStream.writeByte(Constants.MAGIC);
-        this.write(object);
+        this.writeUnknownType(object);
         this.outputStream.flush();
     }
 
-    @SuppressWarnings("unchecked")
-    private void write(Object value) throws EncodingException, IOException {
+    @SuppressWarnings("unchecked") // for Map to Map<K, V> cast
+    private void writeUnknownType(Object value)
+            throws EncodingException, IOException {
         if (value == null) {
             this.writeNull();
         } else if (value instanceof Tuple) {
         } else if (value instanceof Atom) {
             this.writeAtom((Atom) value);
         } else if (value instanceof Map) {
-            this.writeMap((Map<Object, Object>) value);
+            this.writeMap((Map) value);
         } else if (value instanceof Date) {
             this.writeDate((Date) value);
         } else if (value instanceof Integer) {
         }
 
         for (Object element : tuple.contents) {
-            this.write(element);
+            this.writeUnknownType(element);
         }
     }
 
         this.outputStream.writeBytes(atom.identifier);
     }
 
-    public void encode(Map<Object, Object> map)
+    public <K, V> void encode(Map<K, V> map)
             throws IOException, EncodingException {
         this.outputStream.writeByte(Constants.MAGIC);
         if (map == null) this.writeNull(); else this.writeMap(map);
         this.outputStream.flush();
     }
 
-    private void writeMap(Map<Object, Object> map)
+    private <K, V> void writeMap(Map<K, V> map)
             throws IOException, EncodingException {
         List<Tuple> pairs = new ArrayList<Tuple>(map.size());
-        for (Map.Entry o : map.entrySet()) {
+        for (Map.Entry<K, V> o : map.entrySet()) {
             pairs.add(new Tuple(o.getKey(), o.getValue()));
         }
 
 
         try {
             this.writeTuple(new Tuple(Constants.$bert, Constants.$time,
-                    (int)Math.floor(millis / 1e9),
-                    (int)Math.floor((millis % 1e9) / 1e3),
-                    (int)Math.floor(millis % 1e3)));
+                    (int) Math.floor(millis / 1e9),
+                    (int) Math.floor((millis % 1e9) / 1e3),
+                    (int) Math.floor(millis % 1e3)));
         } catch (EncodingException e) {
             throw new RuntimeException(
                     "somehow failed to encode date (" + e.getMessage()
             this.outputStream.writeInt(list.size());
 
             for (Object element : list) {
-                this.write(element);
+                this.writeUnknownType(element);
             }
 
             this.outputStream.writeByte(Constants.NIL);
         List<Object> flags = new ArrayList<Object>();
         int runningMask = regex.flags();
         for (Map.Entry<Object, Integer> p : Constants.REGEX_FLAGS.entrySet()) {
-            if ((runningMask & (int)p.getValue()) != 0) {
-                runningMask = runningMask ^ (int)p.getValue();
+            if ((runningMask & p.getValue()) != 0) {
+                runningMask = runningMask ^ p.getValue();
                 flags.add(p.getKey());
             }
         }