Commits

Rhys ! committed df62ddf Draft

made Tuple contents private since consensus seems to be that runtime inlining will remove any additional cost from method calls

  • Participants
  • Parent commits 41eeffd

Comments (0)

Files changed (3)

File src/org/bert_rpc/Decoder.java

         Object simple = this.readSimple();
         if (simple instanceof Tuple) {
             Tuple tuple = (Tuple)simple;
-            if (tuple.contents.length > 0 &&
-                    Constants.$bert.equals(tuple.contents[0])) {
+            if (tuple.size() > 0 &&
+                    Constants.$bert.equals(tuple.get(0))) {
                 return this.extractComplex(tuple);
             }
         }
     }
 
     private Object extractComplex(Tuple tuple) throws DecodingException {
-        if (tuple.contents.length <= 1) {
+        if (tuple.size() <= 1) {
             throw new DecodingException(
                     "complex type tuple must have length >= 2");
-        } else if (!(tuple.contents[1] instanceof Atom)) {
+        } else if (!(tuple.get(1) instanceof Atom)) {
             throw new DecodingException(
                     "second element of complex type tuple must be atom");
         }
 
-        Atom type = (Atom)tuple.contents[1];
+        Atom type = (Atom)tuple.get(1);
         if (type.equals(Constants.$dict)) {
             return this.extractDict(tuple);
         } else if (type.equals(Constants.$time)) {
     @SuppressWarnings("unchecked")
     private Map<Object, Object> extractDict(Tuple tuple)
             throws DecodingException {
-        if (tuple.contents.length != 3) {
+        if (tuple.size() != 3) {
             throw new DecodingException("dict type must be 3-tuple");
         }
 
         List<Tuple> pairs;
         try {
-            pairs = (List<Tuple>) tuple.contents[2];
+            pairs = (List<Tuple>) tuple.get(2);
         } catch (ClassCastException e) {
             throw new DecodingException(
                     "third element of dict type tuple must be list of " +
 
         Map<Object, Object> dict = new HashMap<Object, Object>(pairs.size());
         for (Tuple pair : pairs) {
-            if (pair.contents.length != 2) {
+            if (pair.size() != 2) {
                 throw new DecodingException(
                         "dict tuple pairs must be 2-tuples");
             }
 
-            dict.put(pair.contents[0], pair.contents[1]);
+            dict.put(pair.get(0), pair.get(1));
         }
 
         return dict;
     }
 
     private Date extractTime(Tuple tuple) throws DecodingException {
-        if (tuple.contents.length != 5) {
+        if (tuple.size() != 5) {
             throw new DecodingException("time type must be 5-tuple");
         }
 
         double stamp;
         try {
-            stamp = (Integer)tuple.contents[2] * 1e9 +
-                    (Integer)tuple.contents[3] * 1e3 +
-                    (Integer)tuple.contents[4];
+            stamp = (Integer)tuple.get(2) * 1e9 +
+                    (Integer)tuple.get(3) * 1e3 +
+                    (Integer)tuple.get(4);
         } catch (ClassCastException e) {
             throw new DecodingException("time type contain 3 integers");
         }
     // pattern component is assumed to be encoded as UTF-8, since Java does
     // not accept binary patterns.
     private Pattern extractRegex(Tuple tuple) throws DecodingException {
-        if (tuple.contents.length != 4) {
-            throw new DecodingException("regex type must be 5-tuple");
-        } else if (!(tuple.contents[2] instanceof byte[])) {
+        if (tuple.size() != 4) {
+            throw new DecodingException("regex type must be 4-tuple");
+        } else if (!(tuple.get(2) instanceof byte[])) {
             throw new DecodingException(
                     "regex type must have binary component at index 2");
-        } else if (!(tuple.contents[3] instanceof List)) {
+        } else if (!(tuple.get(3) instanceof List)) {
             throw new DecodingException(
                     "regex type must have list at index 3");
         }
 
-        List flags = (List)tuple.contents[3];
+        List flags = (List)tuple.get(3);
         int mask = 0;
         for (Object flag : flags) {
             Integer maskComponent = Constants.REGEX_FLAGS.get(flag);
         }
 
         return Pattern.compile(
-                new String((byte[])tuple.contents[2], Charset.forName("UTF8")),
+                new String((byte[])tuple.get(2), Charset.forName("UTF8")),
                 mask);
     }
 

File src/org/bert_rpc/Encoder.java

 
     private void writeTuple(Tuple tuple)
             throws IOException, EncodingException {
-        if (tuple.contents.length < 256) {
+        if (tuple.size() < 256) {
             this.outputStream.write(Constants.SMALL_TUPLE);
-            this.outputStream.writeByte(tuple.contents.length);
+            this.outputStream.writeByte(tuple.size());
         } else {
             this.outputStream.write(Constants.LARGE_TUPLE);
-            this.outputStream.writeInt(tuple.contents.length);
+            this.outputStream.writeInt(tuple.size());
         }
 
-        for (Object element : tuple.contents) {
+        for (Object element : tuple) {
             this.writeUnknownType(element);
         }
     }

File src/org/bert_rpc/Tuple.java

  * A BERT tuple.
  */
 public class Tuple extends AbstractList {
-    public final Object[] contents;
+    private final Object[] contents;
 
     public Tuple() {
         this(new Object[0]);