Skip to content

Commit 9295478

Browse files
committed
Record.equals
1 parent b78b2cb commit 9295478

File tree

3 files changed

+58
-5
lines changed

3 files changed

+58
-5
lines changed

driver/src/main/java/org/neo4j/driver/internal/InternalRecord.java

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class InternalRecord extends InternalRecordAccessor implements Record
3636
private final List<String> keys;
3737
private final Map<String, Integer> keyIndexLookup;
3838
private final Value[] values;
39+
private int hashcode = 0;
3940

4041
public InternalRecord( List<String> keys, Map<String, Integer> keyIndexLookup, Value[] values )
4142
{
@@ -108,11 +109,47 @@ public <T> Map<String, T> asMap( Function<Value, T> mapFunction )
108109
return Extract.map( this, mapFunction );
109110
}
110111

111-
@Override
112-
public int hashCode()
112+
public int hashcode()
113113
{
114-
int result = keys.hashCode();
115-
result = 31 * result + Arrays.hashCode( values );
116-
return result;
114+
if ( hashcode == 0 )
115+
{
116+
hashcode = 31 * keys.hashCode() + Arrays.hashCode( values );
117+
}
118+
return hashcode;
119+
}
120+
121+
public boolean equals( Object other )
122+
{
123+
if ( this == other )
124+
{
125+
return true;
126+
}
127+
else if ( other instanceof Record )
128+
{
129+
Record otherRecord = (Record) other;
130+
int size = fieldCount();
131+
if ( ! ( size == otherRecord.fieldCount() ) )
132+
{
133+
return false;
134+
}
135+
if ( ! keys.equals( otherRecord.keys() ) )
136+
{
137+
return false;
138+
}
139+
for ( int i = 0; i < size; i++ )
140+
{
141+
Value value = value( i );
142+
Value otherValue = otherRecord.value( i );
143+
if ( ! value.equals( otherValue ) )
144+
{
145+
return false;
146+
}
147+
}
148+
return true;
149+
}
150+
else
151+
{
152+
return false;
153+
}
117154
}
118155
}

driver/src/main/java/org/neo4j/driver/v1/Record.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,12 @@ public interface Record extends RecordAccessor
4141
* @return the value as a map from string keys to values of type T
4242
*/
4343
<T> Map<String, T> asMap( Function<Value, T> mapFunction );
44+
45+
// Force implementation
46+
@Override
47+
boolean equals( Object other );
48+
49+
// Force implementation
50+
@Override
51+
int hashCode();
4452
}

driver/src/main/java/org/neo4j/driver/v1/Value.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,4 +333,12 @@ public interface Value extends PropertyMapAccessor, ListAccessor
333333
* @throws Uncoercible if value types are incompatible.
334334
*/
335335
Path asPath();
336+
337+
// Force implementation
338+
@Override
339+
boolean equals( Object other );
340+
341+
// Force implementation
342+
@Override
343+
int hashCode();
336344
}

0 commit comments

Comments
 (0)