avro-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Nick Palmer (JIRA)" <j...@apache.org>
Subject [jira] [Updated] (AVRO-841) Implement GenericData$Array.add(int i, T o)
Date Thu, 23 Jun 2011 08:49:49 GMT

     [ https://issues.apache.org/jira/browse/AVRO-841?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]

Nick Palmer updated AVRO-841:
-----------------------------

    Release Note: Implement GenericData$Array.add(int i, T o)
          Status: Patch Available  (was: Open)

diff --git a/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.ja
index b07fd4f..ee3d01c 100644
--- a/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java
+++ b/lang/java/avro/src/main/java/org/apache/avro/generic/GenericData.java
@@ -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 boun
+      }
+      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 - locat
+      elements[location] = o;
+      size++;
+    }
     @Override public T set(int i, T o) {
       if (i >= size)
         throw new IndexOutOfBoundsException("Index " + i + " out of bounds.");
diff --git a/lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericDat
index 9ae869a..8f53e37 100644
--- a/lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericData.java
+++ b/lang/java/avro/src/test/java/org/apache/avro/generic/TestGenericData.java
@@ -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));

> Implement GenericData$Array.add(int i, T o)
> -------------------------------------------
>
>                 Key: AVRO-841
>                 URL: https://issues.apache.org/jira/browse/AVRO-841
>             Project: Avro
>          Issue Type: Improvement
>          Components: java
>            Reporter: Nick Palmer
>            Priority: Minor
>
> Currently GenericData$Array does not implement List.add(int i, T o).
> I need this method for an application I am working on.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

Mime
View raw message