Author: cutting
Date: Tue Jun 2 17:40:30 2009
New Revision: 781089
URL: http://svn.apache.org/viewvc?rev=781089&view=rev
Log:
AVRO-32. Java specific generated record classes now implement equals and
hashCode.
Added:
hadoop/avro/trunk/src/java/org/apache/avro/specific/SpecificExceptionBase.java
hadoop/avro/trunk/src/java/org/apache/avro/specific/SpecificRecordBase.java
Modified:
hadoop/avro/trunk/CHANGES.txt
hadoop/avro/trunk/src/java/org/apache/avro/specific/SpecificCompiler.java
hadoop/avro/trunk/src/test/java/org/apache/avro/TestProtocolSpecific.java
Modified: hadoop/avro/trunk/CHANGES.txt
URL:
http://svn.apache.org/viewvc/hadoop/avro/trunk/CHANGES.txt?rev=781089&r1=781088&r2=781089&view=diff
==============================================================================
--- hadoop/avro/trunk/CHANGES.txt (original)
+++ hadoop/avro/trunk/CHANGES.txt Tue Jun 2 17:40:30 2009
@@ -47,6 +47,9 @@
AVRO-37. Add C api docs. Also link to py docs. (Matt Massie & cutting)
+ AVRO-32. Java specific generated record classes now implement
+ equals() and hashCode(). (cutting)
+
OPTIMIZATIONS
BUG FIXES
Modified:
hadoop/avro/trunk/src/java/org/apache/avro/specific/SpecificCompiler.java
URL:
http://svn.apache.org/viewvc/hadoop/avro/trunk/src/java/org/apache/avro/specific/SpecificCompiler.java?rev=781089&r1=781088&r2=781089&view=diff
==============================================================================
--- hadoop/avro/trunk/src/java/org/apache/avro/specific/SpecificCompiler.java
(original)
+++ hadoop/avro/trunk/src/java/org/apache/avro/specific/SpecificCompiler.java
Tue Jun 2 17:40:30 2009
@@ -98,6 +98,8 @@
line(0, "import org.apache.avro.util.Utf8;");
line(0, "import org.apache.avro.ipc.AvroRemoteException;");
line(0, "import org.apache.avro.generic.GenericArray;");
+ line(0, "import org.apache.avro.specific.SpecificExceptionBase;");
+ line(0, "import org.apache.avro.specific.SpecificRecordBase;");
line(0, "import org.apache.avro.specific.SpecificRecord;");
line(0, "import org.apache.avro.specific.SpecificFixed;");
line(0, "import org.apache.avro.reflect.FixedSize;");
@@ -134,7 +136,9 @@
buffer.append("\n");
line(d, ((d==0)?"public ":"")
+((d>1)?"static ":"")+"class "+type
- +(schema.isError()?" extends AvroRemoteException":"")
+ +(schema.isError()
+ ? " extends SpecificExceptionBase"
+ : " extends SpecificRecordBase")
+" implements SpecificRecord {");
// schema definition
line(d+1, "private static final Schema _SCHEMA = Schema.parse(\""
Added:
hadoop/avro/trunk/src/java/org/apache/avro/specific/SpecificExceptionBase.java
URL:
http://svn.apache.org/viewvc/hadoop/avro/trunk/src/java/org/apache/avro/specific/SpecificExceptionBase.java?rev=781089&view=auto
==============================================================================
---
hadoop/avro/trunk/src/java/org/apache/avro/specific/SpecificExceptionBase.java
(added)
+++
hadoop/avro/trunk/src/java/org/apache/avro/specific/SpecificExceptionBase.java
Tue Jun 2 17:40:30 2009
@@ -0,0 +1,40 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.avro.specific;
+
+import org.apache.avro.Schema;
+import org.apache.avro.ipc.AvroRemoteException;
+
+/** Base class for specific exceptions. */
+public abstract class SpecificExceptionBase extends AvroRemoteException
+ implements SpecificRecord {
+
+ public abstract Schema schema();
+ public abstract Object get(int field);
+ public abstract void set(int field, Object value);
+
+ public boolean equals(Object o) {
+ return SpecificRecordBase.equals(this, o);
+ }
+
+ public int hashCode() {
+ return SpecificRecordBase.hashCode(this);
+ }
+
+}
Added:
hadoop/avro/trunk/src/java/org/apache/avro/specific/SpecificRecordBase.java
URL:
http://svn.apache.org/viewvc/hadoop/avro/trunk/src/java/org/apache/avro/specific/SpecificRecordBase.java?rev=781089&view=auto
==============================================================================
--- hadoop/avro/trunk/src/java/org/apache/avro/specific/SpecificRecordBase.java
(added)
+++ hadoop/avro/trunk/src/java/org/apache/avro/specific/SpecificRecordBase.java
Tue Jun 2 17:40:30 2009
@@ -0,0 +1,64 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.avro.specific;
+
+import org.apache.avro.Schema;
+
+/** Base class for generated record classes. */
+public abstract class SpecificRecordBase implements SpecificRecord {
+ public abstract Schema schema();
+ public abstract Object get(int field);
+ public abstract void set(int field, Object value);
+
+ public boolean equals(Object o) {
+ return SpecificRecordBase.equals(this, o);
+ }
+
+ static boolean equals(SpecificRecord r1, Object o) {
+ if (r1 == o) return true;
+ if (!(o instanceof SpecificRecord)) return false;
+
+ SpecificRecord r2 = (SpecificRecord)o;
+ if (!r1.schema().equals(r2.schema())) return false;
+
+ int end = r1.schema().getFields().size();
+ for (int i = 0; i < end; i++) {
+ Object v1 = r1.get(i);
+ Object v2 = r2.get(i);
+ if (v1 == null) {
+ if (v2 != null) return false;
+ } else {
+ if (!v1.equals(v2)) return false;
+ }
+ }
+ return true;
+ }
+
+ public int hashCode() {
+ return SpecificRecordBase.hashCode(this);
+ }
+
+ static int hashCode(SpecificRecord r) {
+ int result = 0;
+ int end = r.schema().getFields().size();
+ for (int i = 0; i < end; i++)
+ result += r.get(i).hashCode();
+ return result;
+ }
+
+}
Modified:
hadoop/avro/trunk/src/test/java/org/apache/avro/TestProtocolSpecific.java
URL:
http://svn.apache.org/viewvc/hadoop/avro/trunk/src/test/java/org/apache/avro/TestProtocolSpecific.java?rev=781089&r1=781088&r2=781089&view=diff
==============================================================================
--- hadoop/avro/trunk/src/test/java/org/apache/avro/TestProtocolSpecific.java
(original)
+++ hadoop/avro/trunk/src/test/java/org/apache/avro/TestProtocolSpecific.java
Tue Jun 2 17:40:30 2009
@@ -98,7 +98,8 @@
System.arraycopy(new byte[]{0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5}, 0,
record.hash.bytes(), 0, 16);
TestRecord echoed = proxy.echo(record);
- assertEquals(record.name, echoed.name);
+ assertEquals(record, echoed);
+ assertEquals(record.hashCode(), echoed.hashCode());
}
@Test