Commits

Rhys !  committed f1e79ea Draft

simplified Tuple creation and subclassed AbstractList

  • Participants
  • Parent commits ad0840e

Comments (0)

Files changed (5)

File src/org/bert_rpc/Decoder.java

         Object simple = this.readSimple();
         if (simple instanceof Tuple &&
                 ((Tuple) simple).contents.length > 0 &&
-                ((Tuple) simple).contents[0].equals(Constants.$bert)) {
+                Constants.$bert.equals(((Tuple) simple).contents[0])) {
             return this.extractComplex((Tuple) simple);
         } else {
             return simple;

File src/org/bert_rpc/Encoder.java

             throws IOException, EncodingException {
         List<Tuple> pairs = new ArrayList<Tuple>(map.size());
         for (Map.Entry o : map.entrySet()) {
-            pairs.add(Tuple.n(o.getKey(), o.getValue()));
+            pairs.add(new Tuple(o.getKey(), o.getValue()));
         }
 
-        this.writeTuple(Tuple.n(Constants.$bert, Constants.$dict, pairs));
+        this.writeTuple(new Tuple(Constants.$bert, Constants.$dict, pairs));
     }
 
     private void writeDate(Date date) throws IOException, EncodingException {
         long millis = date.getTime();
 
-        this.writeTuple(Tuple.n(Constants.$bert, Constants.$time,
+        this.writeTuple(new Tuple(Constants.$bert, Constants.$time,
                 (int)Math.floor(millis / 1e9),
                 (int)Math.floor((millis % 1e9) / 1e3),
                 (int)Math.floor(millis % 1e3)));
 
     private void writeBoolean(Boolean value)
             throws IOException, EncodingException {
-        this.writeTuple(Tuple.n(Constants.$bert, new Atom(value.toString())));
+        this.writeTuple(
+                new Tuple(Constants.$bert, new Atom(value.toString())));
     }
 
     private void writeNull() throws IOException, EncodingException {
-        this.writeTuple(Tuple.n(Constants.$bert, Constants.$nil));
+        this.writeTuple(new Tuple(Constants.$bert, Constants.$nil));
     }
 
     private void writeBinary(byte[] bytes) throws IOException {
 
     private void writeRegex(Pattern regex)
             throws IOException, EncodingException {
-        this.writeTuple(Tuple.n(Constants.$bert, Constants.$regex,
+        this.writeTuple(new Tuple(Constants.$bert, Constants.$regex,
                 regex.toString().getBytes("UTF8"), Collections.emptyList()));
     }
 }

File src/org/bert_rpc/Tuple.java

 package org.bert_rpc;
 
-import java.util.Arrays;
+import java.util.*;
 
 /**
  * A BERT tuple.
  */
-public class Tuple {
+public class Tuple extends AbstractList {
     public final Object[] contents;
 
-    public Tuple(Object[] contents) {
-        this.contents = contents;
+    public Tuple() {
+        this(new Object[0]);
     }
 
-    public static Tuple n(Object... contents) {
-        return new Tuple(contents);
+    public Tuple(Object... contents) {
+        this.contents = contents;
     }
 
     @Override
         return "Tuple(" + cs.substring(1, cs.length() - 1) + ")";
     }
 
+    public boolean equals(Tuple other) {
+        return Arrays.equals(this.contents, other.contents);
+    }
+
     @Override
     public boolean equals(Object other) {
         return other != null &&
         return Arrays.hashCode(this.contents);
     }
 
-    public boolean equals(Tuple other) {
-        return Arrays.equals(other.contents, this.contents);
+    @Override
+    public int size() {
+        return this.contents.length;
+    }
+
+    @Override
+    public Object get(int index) {
+        return this.contents[index];
     }
 }

File test/src/org/bert_rpc/TestDecoder.java

         TestDecoder.map.put(new Atom("name"), new Atom("symbol"));
     }
 
-    public static final Tuple complexTuple = Tuple.n(new Atom("user"),
+    public static final Tuple complexTuple = new Tuple(new Atom("user"),
             TestDecoder.map, Arrays.asList(9.9), TestDecoder.date, null, true,
             false, new Atom("true"), new Atom("false"));
     public static final byte[] complexBert = new byte[] { -125, 104, 9, 100, 0,
     @Test
     public void testDecodeTuples() throws Exception {
         Assert.assertEquals("decodes tuples",
-                Tuple.n(new Atom("foo"), new Atom("bar")),
+                new Tuple(new Atom("foo"), new Atom("bar")),
                 Bert.decode(new byte[] { -125, 104, 2, 100, 0, 3, 102, 111,
                         111, 100, 0, 3, 98, 97, 114 }));
     }
                 Bert.decode(new byte[] { -125, 108, 0, 0, 0, 3, 100, 0, 1, 97,
                         97, 1, 97, 5, 106 }));
         Assert.assertEquals("decodes a list followed by another value",
-                Tuple.n(Arrays.asList(1), new Atom("foo")),
+                new Tuple(Arrays.asList(1), new Atom("foo")),
                 Bert.decode(new byte[] { -125, 104, 2, 108, 0, 0, 0, 1, 97, 1,
                         106, 100, 0, 3, 102, 111, 111 }));
     }

File test/src/org/bert_rpc/TestEncoder.java

         Assert.assertArrayEquals("encodes tuples",
                 new byte[] { -125, 104, 2, 100, 0, 3, 102, 111, 111, 100, 0, 3,
                         98, 97, 114 },
-                Bert.encode(Tuple.n(new Atom("foo"), new Atom("bar"))));
+                Bert.encode(new Tuple(new Atom("foo"), new Atom("bar"))));
     }
 
     @Test