avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cutt...@apache.org
Subject svn commit: r1139328 - in /avro/trunk: CHANGES.txt lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericData.java
Date Fri, 24 Jun 2011 14:34:51 GMT
Author: cutting
Date: Fri Jun 24 14:34:51 2011
New Revision: 1139328

URL: http://svn.apache.org/viewvc?rev=1139328&view=rev
Log:
AVRO-841.  Java: Implement insertion in GenericData.Array.  Contributed by Nick Palmer.

Modified:
    avro/trunk/CHANGES.txt
    avro/trunk/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java
    avro/trunk/lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericData.java

Modified: avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1139328&r1=1139327&r2=1139328&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Fri Jun 24 14:34:51 2011
@@ -14,6 +14,9 @@ Avro 1.6.0 (unreleased)
     AVRO-833. Don't require simplejson for python >= 2.6.
     (Miki Tebeka via philz)
 
+    AVRO-841. Java: Implement insertion in GenericData.Array.
+    (Nick Palmer via cutting)
+
   BUG FIXES
     
 Avro 1.5.2 (unreleased)

Modified: avro/trunk/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java?rev=1139328&r1=1139327&r2=1139328&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java (original)
+++ avro/trunk/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java Fri Jun
24 14:34:51 2011
@@ -123,6 +123,19 @@ public class GenericData {
       elements[size++] = o;
       return true;
     }
+    @Override public void add(int location, T o) {
+      if (location > size || location < 0) {
+        throw new IndexOutOfBoundsException("Index " + location + " out of bounds.");
+      }
+      if (size == elements.length) {
+        Object[] newElements = new Object[(size * 3)/2 + 1];
+        System.arraycopy(elements, 0, newElements, 0, size);
+        elements = newElements;
+      }
+      System.arraycopy(elements, location, elements, location + 1, size - location);
+      elements[location] = o;
+      size++;
+    }
     @Override public T set(int i, T o) {
       if (i >= size)
         throw new IndexOutOfBoundsException("Index " + i + " out of bounds.");

Modified: avro/trunk/lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericData.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericData.java?rev=1139328&r1=1139327&r2=1139328&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericData.java (original)
+++ avro/trunk/lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericData.java Fri
Jun 24 14:34:51 2011
@@ -153,6 +153,35 @@ public class TestGenericData {
 
   }
   @Test
+  public void testArrayAddAtLocation()
+  {
+    Schema schema = Schema.createArray(Schema.create(Schema.Type.INT));
+    GenericArray<Integer> array = new GenericData.Array<Integer>(6, schema);
+    array.clear();
+    for(int i=0; i<5; ++i)
+      array.add(i);
+    assertEquals(5, array.size());
+    array.add(0, 6);
+    assertEquals(new Integer(6), array.get(0));
+    assertEquals(6, array.size());
+    assertEquals(new Integer(0), array.get(1));
+    assertEquals(new Integer(4), array.get(5));
+    array.add(6, 7);
+    assertEquals(new Integer(7), array.get(6));
+    assertEquals(7, array.size());
+    assertEquals(new Integer(6), array.get(0));
+    assertEquals(new Integer(4), array.get(5));
+    array.add(1, 8);
+    assertEquals(new Integer(8), array.get(1));
+    assertEquals(new Integer(0), array.get(2));
+    assertEquals(new Integer(6), array.get(0));
+    assertEquals(8, array.size());
+    try {
+	array.get(9);
+	fail("Expected IndexOutOfBoundsException after adding elements");
+    } catch (IndexOutOfBoundsException e){}
+  }
+  @Test
   public void testArrayRemove()
   {
     Schema schema = Schema.createArray(Schema.create(Schema.Type.INT));



Mime
View raw message