avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cutt...@apache.org
Subject svn commit: r990852 - in /avro/trunk: ./ lang/java/src/java/org/apache/avro/generic/ lang/java/src/java/org/apache/avro/ipc/trace/ lang/java/src/java/org/apache/avro/specific/ lang/java/src/test/java/org/apache/avro/
Date Mon, 30 Aug 2010 16:36:31 GMT
Author: cutting
Date: Mon Aug 30 16:36:31 2010
New Revision: 990852

URL: http://svn.apache.org/viewvc?rev=990852&view=rev
Log:
AVRO-637. Java: Change GenericArray to implement List.

Modified:
    avro/trunk/CHANGES.txt
    avro/trunk/lang/java/src/java/org/apache/avro/generic/GenericArray.java
    avro/trunk/lang/java/src/java/org/apache/avro/generic/GenericData.java
    avro/trunk/lang/java/src/java/org/apache/avro/generic/GenericDatumReader.java
    avro/trunk/lang/java/src/java/org/apache/avro/generic/GenericDatumWriter.java
    avro/trunk/lang/java/src/java/org/apache/avro/generic/package.html
    avro/trunk/lang/java/src/java/org/apache/avro/ipc/trace/TracePlugin.java
    avro/trunk/lang/java/src/java/org/apache/avro/specific/SpecificCompiler.java
    avro/trunk/lang/java/src/java/org/apache/avro/specific/SpecificData.java
    avro/trunk/lang/java/src/test/java/org/apache/avro/TestSchema.java

Modified: avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=990852&r1=990851&r2=990852&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Mon Aug 30 16:36:31 2010
@@ -29,6 +29,11 @@ Avro 1.4.0 (31 August 2010)
     representation of enum symbols from java.lang.String to
     org.apache.avro.generic.GenericEnumSymbol.
 
+    AVRO-637. Java: Change GenericArray to implement List.  Also
+    incompatibly change generated array signatures to List.  The
+    runtime will now accept any Collection implementation for array
+    types.  (cutting)
+
   NEW FEATURES
 
     AVRO-613. Create basic frontend to view trace results.

Modified: avro/trunk/lang/java/src/java/org/apache/avro/generic/GenericArray.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/src/java/org/apache/avro/generic/GenericArray.java?rev=990852&r1=990851&r2=990852&view=diff
==============================================================================
--- avro/trunk/lang/java/src/java/org/apache/avro/generic/GenericArray.java (original)
+++ avro/trunk/lang/java/src/java/org/apache/avro/generic/GenericArray.java Mon Aug 30 16:36:31
2010
@@ -15,20 +15,13 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.avro.generic;
-
 
-/** An array of objects. */
-public interface GenericArray<T> extends Iterable<T>, GenericContainer {
-  /** The number of elements contained in this array. */
-  long size();
-
-  /** Reset the size of the array to zero. */
-  void clear();
+package org.apache.avro.generic;
 
-  /** Add an element to this array. */
-  void add(T element);
+import java.util.List;
 
+/** Array that permits reuse of contained elements. */
+public interface GenericArray<T> extends List<T>, GenericContainer {
   /** The current content of the location where {@link #add(Object)} would next
    * store an element, if any.  This permits reuse of arrays and their elements
    * without allocating new objects. */

Modified: avro/trunk/lang/java/src/java/org/apache/avro/generic/GenericData.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/src/java/org/apache/avro/generic/GenericData.java?rev=990852&r1=990851&r2=990852&view=diff
==============================================================================
--- avro/trunk/lang/java/src/java/org/apache/avro/generic/GenericData.java (original)
+++ avro/trunk/lang/java/src/java/org/apache/avro/generic/GenericData.java Mon Aug 30 16:36:31
2010
@@ -21,6 +21,8 @@ import java.nio.ByteBuffer;
 import java.util.Iterator;
 import java.util.Arrays;
 import java.util.Map;
+import java.util.Collection;
+import java.util.AbstractList;
 
 import org.apache.avro.AvroRuntimeException;
 import org.apache.avro.AvroTypeException;
@@ -79,10 +81,10 @@ public class GenericData {
     }
   }
 
-  /** Default implementation of {@link GenericArray}. */
+  /** Default implementation of an array. */
   @SuppressWarnings(value="unchecked")
-  public static class Array<T>
-    implements GenericArray<T>, Comparable<Array<T>> {
+  public static class Array<T> extends AbstractList<T>
+    implements GenericArray<T>, Comparable<GenericArray<T>> {
     private static final Object[] EMPTY = new Object[0];
     private final Schema schema;
     private int size;
@@ -95,9 +97,9 @@ public class GenericData {
         elements = new Object[capacity];
     }
     public Schema getSchema() { return schema; }
-    public long size() { return size; }
-    public void clear() { size = 0; }
-    public Iterator<T> iterator() {
+    @Override public int size() { return size; }
+    @Override public void clear() { size = 0; }
+    @Override public Iterator<T> iterator() {
       return new Iterator<T>() {
         private int position = 0;
         public boolean hasNext() { return position < size; }
@@ -105,13 +107,15 @@ public class GenericData {
         public void remove() { throw new UnsupportedOperationException(); }
       };
     }
-    public void add(T o) {
+    @Override public T get(int i) { return (T)elements[i]; }
+    @Override public boolean add(T o) {
       if (size == elements.length) {
         Object[] newElements = new Object[(size * 3)/2 + 1];
         System.arraycopy(elements, 0, newElements, 0, size);
         elements = newElements;
       }
       elements[size++] = o;
+      return true;
     }
     public T peek() {
       return (size < elements.length) ? (T)elements[size] : null;
@@ -127,7 +131,7 @@ public class GenericData {
         return false;                             // not the same schema
       return this.compareTo(that) == 0;
     }
-    public int compareTo(Array<T> that) {
+    public int compareTo(GenericArray<T> that) {
       return GenericData.get().compare(this, that, this.getSchema());
     }
     public void reverse() {
@@ -268,8 +272,8 @@ public class GenericData {
           buffer.append(", ");
       }
       buffer.append("}");
-    } else if (datum instanceof GenericArray) {
-      GenericArray array = (GenericArray)datum;
+    } else if (datum instanceof Collection) {
+      Collection array = (Collection)datum;
       buffer.append("[");
       long last = array.size()-1;
       int i = 0;
@@ -311,9 +315,9 @@ public class GenericData {
   public Schema induce(Object datum) {
     if (datum instanceof IndexedRecord) {
       return ((IndexedRecord)datum).getSchema();
-    } else if (datum instanceof GenericArray) {
+    } else if (datum instanceof Collection) {
       Schema elementType = null;
-      for (Object element : (GenericArray)datum) {
+      for (Object element : (Collection)datum) {
         if (elementType == null) {
           elementType = induce(element);
         } else if (!elementType.equals(induce(element))) {
@@ -394,7 +398,7 @@ public class GenericData {
 
   /** Called by the default implementation of {@link #instanceOf}.*/
   protected boolean isArray(Object datum) {
-    return datum instanceof GenericArray;
+    return datum instanceof Collection;
   }
 
   /** Called by the default implementation of {@link #instanceOf}.*/

Modified: avro/trunk/lang/java/src/java/org/apache/avro/generic/GenericDatumReader.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/src/java/org/apache/avro/generic/GenericDatumReader.java?rev=990852&r1=990851&r2=990852&view=diff
==============================================================================
--- avro/trunk/lang/java/src/java/org/apache/avro/generic/GenericDatumReader.java (original)
+++ avro/trunk/lang/java/src/java/org/apache/avro/generic/GenericDatumReader.java Mon Aug
30 16:36:31 2010
@@ -20,6 +20,7 @@ package org.apache.avro.generic;
 import java.io.IOException;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Collection;
 import java.nio.ByteBuffer;
 
 import org.apache.avro.AvroRuntimeException;
@@ -196,14 +197,16 @@ public class GenericDatumReader<D> imple
    * GenericArray}.*/
   @SuppressWarnings("unchecked")
   protected Object peekArray(Object array) {
-    return ((GenericArray) array).peek();
+    return (array instanceof GenericArray)
+      ? ((GenericArray)array).peek()
+      : null;
   }
 
-  /** Called by the default implementation of {@link #readArray} to add a value.
-   * The default implementation is for {@link GenericArray}.*/
+  /** Called by the default implementation of {@link #readArray} to add a
+   * value.  The default implementation is for {@link Collection}.*/
   @SuppressWarnings("unchecked")
   protected void addToArray(Object array, long pos, Object e) {
-    ((GenericArray) array).add(e);
+    ((Collection) array).add(e);
   }
   
   /** Called to read a map instance.  May be overridden for alternate map
@@ -277,8 +280,8 @@ public class GenericDatumReader<D> imple
    * GenericData.Array}.*/
   @SuppressWarnings("unchecked")
   protected Object newArray(Object old, int size, Schema schema) {
-    if (old instanceof GenericArray) {
-      ((GenericArray) old).clear();
+    if (old instanceof Collection) {
+      ((Collection) old).clear();
       return old;
     } else return new GenericData.Array(size, schema);
   }

Modified: avro/trunk/lang/java/src/java/org/apache/avro/generic/GenericDatumWriter.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/src/java/org/apache/avro/generic/GenericDatumWriter.java?rev=990852&r1=990851&r2=990852&view=diff
==============================================================================
--- avro/trunk/lang/java/src/java/org/apache/avro/generic/GenericDatumWriter.java (original)
+++ avro/trunk/lang/java/src/java/org/apache/avro/generic/GenericDatumWriter.java Mon Aug
30 16:36:31 2010
@@ -22,6 +22,7 @@ import java.nio.ByteBuffer;
 import java.util.Iterator;
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.Collection;
 
 import org.apache.avro.AvroTypeException;
 import org.apache.avro.Schema;
@@ -135,18 +136,17 @@ public class GenericDatumWriter<D> imple
   }
 
   /** Called by the default implementation of {@link #writeArray} to get the
-   * size of an array.  The default implementation is for {@link
-   * GenericArray}.*/
+   * size of an array.  The default implementation is for {@link Collection}.*/
   @SuppressWarnings("unchecked")
   protected long getArraySize(Object array) {
-    return ((GenericArray) array).size();
+    return ((Collection) array).size();
   }
 
   /** Called by the default implementation of {@link #writeArray} to enumerate
-   * array elements.  The default implementation is for {@link GenericArray}.*/
+   * array elements.  The default implementation is for {@link Collection}.*/
   @SuppressWarnings("unchecked")
   protected Iterator<? extends Object> getArrayElements(Object array) {
-    return ((GenericArray) array).iterator();
+    return ((Collection) array).iterator();
   }
   
   /** Called to write a map.  May be overridden for alternate map

Modified: avro/trunk/lang/java/src/java/org/apache/avro/generic/package.html
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/src/java/org/apache/avro/generic/package.html?rev=990852&r1=990851&r2=990852&view=diff
==============================================================================
--- avro/trunk/lang/java/src/java/org/apache/avro/generic/package.html (original)
+++ avro/trunk/lang/java/src/java/org/apache/avro/generic/package.html Mon Aug 30 16:36:31
2010
@@ -26,11 +26,11 @@ A generic representation for Avro data.
 <p>Avro schemas are mapped to Java types as follows:
 <ul>
 <li>Schema records are implemented as {@link org.apache.avro.generic.GenericRecord}.
-<li>Schema enums are implemented as {@link java.lang.String}.
-<li>Schema arrays are implemented as {@link org.apache.avro.generic.GenericArray}.
+<li>Schema enums are implemented as {@link org.apache.avro.generic.GenericEnumSymbol}.
+<li>Schema arrays are implemented as {@link java.util.Collection}.
 <li>Schema maps are implemented as {@link java.util.Map}.
 <li>Schema fixed are implemented as {@link org.apache.avro.generic.GenericFixed}.
-<li>Schema strings are implemented as {@link org.apache.avro.util.Utf8}.
+<li>Schema strings are implemented as {@link java.lang.CharSequence}.
 <li>Schema bytes are implemented as {@link java.nio.ByteBuffer}.
 <li>Schema ints are implemented as {@link java.lang.Integer}.
 <li>Schema longs are implemented as {@link java.lang.Long}.

Modified: avro/trunk/lang/java/src/java/org/apache/avro/ipc/trace/TracePlugin.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/src/java/org/apache/avro/ipc/trace/TracePlugin.java?rev=990852&r1=990851&r2=990852&view=diff
==============================================================================
--- avro/trunk/lang/java/src/java/org/apache/avro/ipc/trace/TracePlugin.java (original)
+++ avro/trunk/lang/java/src/java/org/apache/avro/ipc/trace/TracePlugin.java Mon Aug 30 16:36:31
2010
@@ -29,7 +29,6 @@ import java.util.Random;
 
 import org.apache.avro.AvroRuntimeException;
 import org.apache.avro.Schema;
-import org.apache.avro.generic.GenericArray;
 import org.apache.avro.generic.GenericData;
 import org.apache.avro.ipc.AvroRemoteException;
 import org.apache.avro.ipc.HttpServer;
@@ -84,28 +83,14 @@ public class TracePlugin extends RPCPlug
     }
 
     @Override
-    public GenericArray<Span> getAllSpans() throws AvroRemoteException {
-      List<Span> spans = this.spanStorage.getAllSpans();
-      GenericData.Array<Span> out;
-      out = new GenericData.Array<Span>(spans.size(), 
-        Schema.createArray(Span.SCHEMA$));
-      for (Span s: spans) {
-        out.add(s);
-      }
-      return out;
+    public List<Span> getAllSpans() throws AvroRemoteException {
+      return this.spanStorage.getAllSpans();
     }
 
     @Override
-    public GenericArray<Span> getSpansInRange(long start, long end)
-        throws AvroRemoteException {
-      List<Span> spans = this.spanStorage.getSpansInRange(start, end);
-      GenericData.Array<Span> out;
-      out = new GenericData.Array<Span>(spans.size(), 
-        Schema.createArray(Span.SCHEMA$));
-      for (Span s: spans) {
-        out.add(s);
-      }
-      return out;
+    public List<Span> getSpansInRange(long start, long end)
+      throws AvroRemoteException {
+      return this.spanStorage.getSpansInRange(start, end);
     }
   }
   

Modified: avro/trunk/lang/java/src/java/org/apache/avro/specific/SpecificCompiler.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/src/java/org/apache/avro/specific/SpecificCompiler.java?rev=990852&r1=990851&r2=990852&view=diff
==============================================================================
--- avro/trunk/lang/java/src/java/org/apache/avro/specific/SpecificCompiler.java (original)
+++ avro/trunk/lang/java/src/java/org/apache/avro/specific/SpecificCompiler.java Mon Aug 30
16:36:31 2010
@@ -351,7 +351,7 @@ public class SpecificCompiler {
     case FIXED:
       return mangle(schema.getFullName());
     case ARRAY:
-      return "org.apache.avro.generic.GenericArray<"+type(schema.getElementType())+">";
+      return "java.util.List<"+type(schema.getElementType())+">";
     case MAP:
       return "java.util.Map<java.lang.CharSequence,"+type(schema.getValueType())+">";
     case UNION:

Modified: avro/trunk/lang/java/src/java/org/apache/avro/specific/SpecificData.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/src/java/org/apache/avro/specific/SpecificData.java?rev=990852&r1=990851&r2=990852&view=diff
==============================================================================
--- avro/trunk/lang/java/src/java/org/apache/avro/specific/SpecificData.java (original)
+++ avro/trunk/lang/java/src/java/org/apache/avro/specific/SpecificData.java Mon Aug 30 16:36:31
2010
@@ -18,6 +18,7 @@
 package org.apache.avro.specific;
 
 import java.util.Map;
+import java.util.Collection;
 import java.util.List;
 import java.util.WeakHashMap;
 import java.util.concurrent.ConcurrentHashMap;
@@ -31,7 +32,6 @@ import org.apache.avro.AvroRuntimeExcept
 import org.apache.avro.AvroTypeException;
 import org.apache.avro.Schema.Type;
 import org.apache.avro.generic.GenericData;
-import org.apache.avro.generic.GenericArray;
 
 /** Utilities for generated Java classes and interfaces. */
 public class SpecificData extends GenericData {
@@ -70,7 +70,7 @@ public class SpecificData extends Generi
         classCache.put(name, c);
       }
       return c == NO_CLASS ? null : c;
-    case ARRAY:   return GenericArray.class;
+    case ARRAY:   return Collection.class;
     case MAP:     return Map.class;
     case UNION:
       List<Schema> types = schema.getTypes();     // elide unions with null
@@ -137,7 +137,7 @@ public class SpecificData extends Generi
       ParameterizedType ptype = (ParameterizedType)type;
       Class raw = (Class)ptype.getRawType();
       java.lang.reflect.Type[] params = ptype.getActualTypeArguments();
-      if (GenericArray.class.isAssignableFrom(raw)) { // array
+      if (Collection.class.isAssignableFrom(raw)) { // array
         if (params.length != 1)
           throw new AvroTypeException("No array type specified.");
         return Schema.createArray(createSchema(params[0], names));

Modified: avro/trunk/lang/java/src/test/java/org/apache/avro/TestSchema.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/src/test/java/org/apache/avro/TestSchema.java?rev=990852&r1=990851&r2=990852&view=diff
==============================================================================
--- avro/trunk/lang/java/src/test/java/org/apache/avro/TestSchema.java (original)
+++ avro/trunk/lang/java/src/test/java/org/apache/avro/TestSchema.java Mon Aug 30 16:36:31
2010
@@ -32,9 +32,9 @@ import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Collection;
 
 import org.apache.avro.Schema.Type;
-import org.apache.avro.generic.GenericArray;
 import org.apache.avro.generic.GenericData;
 import org.apache.avro.generic.GenericDatumReader;
 import org.apache.avro.generic.GenericDatumWriter;
@@ -141,7 +141,10 @@ public class TestSchema {
   public void testArray() throws Exception {
     String json = "{\"type\":\"array\", \"items\": \"long\"}";
     Schema schema = Schema.parse(json);
-    GenericArray<Long> array = new GenericData.Array<Long>(1, schema);
+    Collection<Long> array = new GenericData.Array<Long>(1, schema);
+    array.add(1L);
+    check(json, "[1]", array);
+    array = new ArrayList<Long>(1);
     array.add(1L);
     check(json, "[1]", array);
     checkParseError("{\"type\":\"array\"}");      // items required



Mime
View raw message