avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cutt...@apache.org
Subject svn commit: r1471047 - in /avro/trunk: CHANGES.txt lang/java/avro/src/main/java/org/apache/avro/specific/SpecificRecordBase.java lang/java/avro/src/test/java/org/apache/avro/specific/TestSpecificData.java
Date Tue, 23 Apr 2013 17:11:24 GMT
Author: cutting
Date: Tue Apr 23 17:11:23 2013
New Revision: 1471047

URL: http://svn.apache.org/r1471047
Log:
AVRO-1299. Java: SpecificRecordBase implements GenericRecord.  Contributed by Christophe Taton.

Modified:
    avro/trunk/CHANGES.txt
    avro/trunk/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificRecordBase.java
    avro/trunk/lang/java/avro/src/test/java/org/apache/avro/specific/TestSpecificData.java

Modified: avro/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/avro/trunk/CHANGES.txt?rev=1471047&r1=1471046&r2=1471047&view=diff
==============================================================================
--- avro/trunk/CHANGES.txt (original)
+++ avro/trunk/CHANGES.txt Tue Apr 23 17:11:23 2013
@@ -31,6 +31,9 @@ Trunk (not yet released)
     AVRO-867. Java: Enable command-line tools to read data files from
     any Hadoop FileSystem implementation. (Vincenz Priesnitz via cutting)
 
+    AVRO-1299. Java: SpecificRecordBase implements GenericRecord.
+    (Christophe Taton via cutting)
+
   BUG FIXES
 
     AVRO-1296. Python: Fix schemas retrieved from protocol types

Modified: avro/trunk/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificRecordBase.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificRecordBase.java?rev=1471047&r1=1471046&r2=1471047&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificRecordBase.java
(original)
+++ avro/trunk/lang/java/avro/src/main/java/org/apache/avro/specific/SpecificRecordBase.java
Tue Apr 23 17:11:23 2013
@@ -18,16 +18,27 @@
 package org.apache.avro.specific;
 
 import org.apache.avro.Schema;
+import org.apache.avro.generic.GenericRecord;
 
 /** Base class for generated record classes. */
 public abstract class SpecificRecordBase
-  implements SpecificRecord, Comparable<SpecificRecord> {
+  implements SpecificRecord, Comparable<SpecificRecord>, GenericRecord {
 
   public abstract Schema getSchema();
   public abstract Object get(int field);
   public abstract void put(int field, Object value);
 
   @Override
+  public void put(String fieldName, Object value) {
+    put(getSchema().getField(fieldName).pos(), value);
+  }
+
+  @Override
+  public Object get(String fieldName) {
+    return get(getSchema().getField(fieldName).pos());
+  }
+
+  @Override
   public boolean equals(Object that) {
     if (that == this) return true;                        // identical object
     if (!(that instanceof SpecificRecord)) return false;  // not a record

Modified: avro/trunk/lang/java/avro/src/test/java/org/apache/avro/specific/TestSpecificData.java
URL: http://svn.apache.org/viewvc/avro/trunk/lang/java/avro/src/test/java/org/apache/avro/specific/TestSpecificData.java?rev=1471047&r1=1471046&r2=1471047&view=diff
==============================================================================
--- avro/trunk/lang/java/avro/src/test/java/org/apache/avro/specific/TestSpecificData.java
(original)
+++ avro/trunk/lang/java/avro/src/test/java/org/apache/avro/specific/TestSpecificData.java
Tue Apr 23 17:11:23 2013
@@ -18,13 +18,17 @@
 
 package org.apache.avro.specific;
 
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
+import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.List;
 
 import org.apache.avro.Schema;
+import org.apache.avro.Schema.Field;
 import org.apache.avro.Schema.Type;
 import org.junit.Before;
 import org.junit.Test;
@@ -77,4 +81,48 @@ public class TestSpecificData {
     public void primitive(int i) {}
     public void primitiveWrapper(Integer i) {}
   }
+
+  private static class TestRecord extends SpecificRecordBase {
+    private static final Schema SCHEMA = Schema.createRecord("TestRecord", null, null, false);
+    static {
+      List<Field> fields = new ArrayList<Field>();
+      fields.add(new Field("x", Schema.create(Type.INT), null, null));
+      fields.add(new Field("y", Schema.create(Type.STRING), null, null));
+      SCHEMA.setFields(fields);
+    }
+    private int x;
+    private String y;
+
+    @Override
+    public void put(int i, Object v) {
+      switch (i) {
+      case 0: x = (Integer) v; break;
+      case 1: y = (String) v; break;
+      default: throw new RuntimeException();
+      }
+    }
+
+    @Override
+    public Object get(int i) {
+      switch (i) {
+      case 0: return x;
+      case 1: return y;
+      }
+      throw new RuntimeException();
+    }
+
+    @Override
+    public Schema getSchema() {
+      return SCHEMA;
+    }
+  }
+
+  @Test
+  public void testSpecificRecordBase() {
+    final TestRecord record = new TestRecord();
+    record.put("x", 1);
+    record.put("y", "str");
+    assertEquals(1, record.get("x"));
+    assertEquals("str", record.get("y"));
+  }
 }



Mime
View raw message