Skip to content

Commit 7b57b25

Browse files
committed
Add missing RecordAccessor methods: index, field(String), field(int)
o Also break inheritance between Field and Property in favour of adding asProperty (this inheritance failed to work property with equals)
1 parent adb16ec commit 7b57b25

File tree

7 files changed

+158
-15
lines changed

7 files changed

+158
-15
lines changed

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

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,41 @@
1818
*/
1919
package org.neo4j.driver.internal;
2020

21+
import java.util.Objects;
22+
2123
import org.neo4j.driver.v1.Field;
24+
import org.neo4j.driver.v1.Function;
25+
import org.neo4j.driver.v1.Property;
2226

23-
public class InternalField<V> extends InternalProperty<V> implements Field<V>
27+
public class InternalField<V> implements Field<V>
2428
{
2529
private final int index;
30+
private final String key;
31+
private final V value;
2632

2733
public InternalField( String key, int index, V value )
2834
{
29-
super( key, value );
35+
if ( key == null )
36+
{
37+
throw new IllegalArgumentException( "null key" );
38+
}
39+
if ( value == null )
40+
{
41+
throw new IllegalArgumentException( "null value" );
42+
}
3043
this.index = index;
44+
this.key = key;
45+
this.value = value;
46+
}
47+
48+
public String key()
49+
{
50+
return key;
51+
}
52+
53+
public V value()
54+
{
55+
return value;
3156
}
3257

3358
@Override
@@ -36,6 +61,48 @@ public int index()
3661
return index;
3762
}
3863

64+
@Override
65+
public Property<V> asProperty()
66+
{
67+
return InternalProperty.of( key, value );
68+
}
69+
70+
@Override
71+
public String toString()
72+
{
73+
return String.format( "%s: %s", key, Objects.toString( value ) );
74+
}
75+
76+
public String toString( Function<V, String> printValue )
77+
{
78+
return String.format( "%s: %s", key, printValue.apply( value ) );
79+
}
80+
81+
@Override
82+
public boolean equals( Object o )
83+
{
84+
if ( this == o )
85+
{
86+
return true;
87+
}
88+
if ( o == null || getClass() != o.getClass() )
89+
{
90+
return false;
91+
}
92+
93+
InternalField<?> that = (InternalField<?>) o;
94+
return index == that.index && key.equals( that.key ) && value.equals( that.value );
95+
}
96+
97+
@Override
98+
public int hashCode()
99+
{
100+
int result = index;
101+
result = 31 * result + key.hashCode();
102+
result = 31 * result + value.hashCode();
103+
return result;
104+
}
105+
39106
public static <V> Field<V> of( String key, int index, V value )
40107
{
41108
return new InternalField<>( key, index, value );

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,14 @@ public class InternalProperty<V> implements Property<V>
3030

3131
protected InternalProperty( String key, V value )
3232
{
33+
if ( key == null )
34+
{
35+
throw new IllegalArgumentException( "null key" );
36+
}
37+
if ( value == null )
38+
{
39+
throw new IllegalArgumentException( "null value" );
40+
}
3341
this.key = key;
3442
this.value = value;
3543
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
import static java.lang.String.format;
3333

34-
import static org.neo4j.driver.internal.util.Format.formatProperties;
34+
import static org.neo4j.driver.internal.util.Format.formatFields;
3535
import static org.neo4j.driver.internal.value.InternalValue.Format.VALUE_WITH_TYPE;
3636
import static org.neo4j.driver.v1.Values.valueAsIs;
3737

@@ -122,7 +122,7 @@ public <T> Map<String, T> asMap( Function<Value, T> mapFunction )
122122
@Override
123123
public String toString()
124124
{
125-
return format( "Record<%s>", formatProperties( VALUE_WITH_TYPE, fieldCount(), fields() ) );
125+
return format( "Record<%s>", formatFields( VALUE_WITH_TYPE, fieldCount(), fields() ) );
126126
}
127127

128128
public boolean equals( Object other )

driver/src/main/java/org/neo4j/driver/internal/util/Format.java

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@
2020

2121
import java.util.Iterator;
2222

23+
import org.neo4j.driver.internal.InternalField;
2324
import org.neo4j.driver.internal.InternalProperty;
25+
import org.neo4j.driver.v1.Field;
2426
import org.neo4j.driver.v1.Function;
2527
import org.neo4j.driver.v1.Property;
26-
import org.neo4j.driver.v1.Value;
2728

2829
public abstract class Format
2930
{
@@ -32,9 +33,9 @@ private Format()
3233
throw new UnsupportedOperationException();
3334
}
3435

35-
public static <V extends Property<Value>> String formatProperties( Function<Value, String> printValue,
36-
int propertyCount,
37-
Iterable<V> properties )
36+
public static <V> String formatProperties( Function<V, String> printValue,
37+
int propertyCount,
38+
Iterable<Property<V>> properties )
3839
{
3940
switch ( propertyCount ) {
4041
case 0:
@@ -49,7 +50,7 @@ public static <V extends Property<Value>> String formatProperties( Function<Valu
4950
{
5051
StringBuilder builder = new StringBuilder();
5152
builder.append( "{" );
52-
Iterator<V> iterator = properties.iterator();
53+
Iterator<Property<V>> iterator = properties.iterator();
5354
builder.append( internalProperty( iterator.next() ).toString( printValue ) );
5455
while ( iterator.hasNext() )
5556
{
@@ -64,12 +65,49 @@ public static <V extends Property<Value>> String formatProperties( Function<Valu
6465
}
6566

6667
@SuppressWarnings("unchecked")
67-
private static <V extends Property<Value>> InternalProperty<Value> internalProperty( V property )
68+
private static <V> InternalProperty<V> internalProperty( Property<V> property )
6869
{
69-
return (InternalProperty<Value>) property;
70+
return (InternalProperty<V>) property;
7071
}
7172

72-
public static String formatElements( Function<Value, String> printValue, Value[] elements )
73+
public static <V> String formatFields( Function<V, String> printValue,
74+
int propertyCount,
75+
Iterable<Field<V>> fields )
76+
{
77+
switch ( propertyCount ) {
78+
case 0:
79+
return "{}";
80+
81+
case 1:
82+
{
83+
return String.format( "{%s}", internalField( fields.iterator().next() ).toString( printValue ) );
84+
}
85+
86+
default:
87+
{
88+
StringBuilder builder = new StringBuilder();
89+
builder.append( "{" );
90+
Iterator<Field<V>> iterator = fields.iterator();
91+
builder.append( internalField( iterator.next() ).toString( printValue ) );
92+
while ( iterator.hasNext() )
93+
{
94+
builder.append( ',' );
95+
builder.append( ' ' );
96+
builder.append( internalField( iterator.next() ).toString( printValue ) );
97+
}
98+
builder.append( "}" );
99+
return builder.toString();
100+
}
101+
}
102+
}
103+
104+
@SuppressWarnings("unchecked")
105+
private static <V> InternalField<V> internalField( Field<V> property )
106+
{
107+
return (InternalField<V>) property;
108+
}
109+
110+
public static <V> String formatElements( Function<V, String> printValue, V[] elements )
73111
{
74112
int elementCount = elements.length;
75113
switch ( elementCount ) {

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,22 @@
2828
* @see RecordAccessor
2929
*/
3030
@Immutable
31-
public interface Field<V> extends Property<V>
31+
public interface Field<V>
3232
{
33+
/**
34+
* @return the property key
35+
*/
36+
String key();
37+
38+
/**
39+
* @return the property value
40+
*/
41+
V value();
42+
3343
/**
3444
* @return the index of the field in the original record
3545
*/
3646
int index();
47+
48+
Property<V> asProperty();
3749
}

driver/src/test/java/org/neo4j/driver/internal/InternalRecordTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,24 @@ public void testContainsKey()
102102
assertFalse( record.containsKey( "k3" ) );
103103
}
104104

105+
@Test
106+
public void testIndex()
107+
{
108+
InternalRecord record = createRecord();
109+
110+
assertThat( record.index( "k1" ), equalTo( 0 ) );
111+
assertThat( record.index( "k2" ), equalTo( 1 ) );
112+
}
113+
114+
@Test
115+
public void testField()
116+
{
117+
InternalRecord record = createRecord();
118+
119+
assertThat( record.field( "k1" ), equalTo( record.field( 0 ) ) );
120+
assertThat( record.field( "k2" ), equalTo( record.field( 1 ) ) );
121+
}
122+
105123
@Test
106124
public void testAsMap()
107125
{

driver/src/test/java/org/neo4j/driver/internal/InternalResultTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
import org.neo4j.driver.internal.summary.ResultBuilder;
3131
import org.neo4j.driver.internal.value.NullValue;
32-
import org.neo4j.driver.v1.Property;
32+
import org.neo4j.driver.v1.Field;
3333
import org.neo4j.driver.v1.Record;
3434
import org.neo4j.driver.v1.Records;
3535
import org.neo4j.driver.v1.Result;
@@ -287,7 +287,7 @@ private Result createResult( int numberOfRecords )
287287
private List<Value> values( Record record )
288288
{
289289
List<Value> result = new ArrayList<>( record.keys().size() );
290-
for ( Property<Value> property : record.fields() )
290+
for ( Field<Value> property : record.fields() )
291291
{
292292
result.add( property.value() );
293293
}

0 commit comments

Comments
 (0)