Skip to content

Commit e1d696e

Browse files
bogglejakewins
authored andcommitted
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 90313f2 commit e1d696e

File tree

11 files changed

+225
-33
lines changed

11 files changed

+225
-33
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: 9 additions & 3 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

@@ -55,14 +55,20 @@ public Value value( int index )
5555
return index >= 0 && index < values.length ? values[index] : Values.NULL;
5656
}
5757

58+
@Override
59+
public String key( int index )
60+
{
61+
return keys.get( index );
62+
}
63+
5864
@Override
5965
public List<String> keys()
6066
{
6167
return keys;
6268
}
6369

6470
@Override
65-
public int fieldIndex( String key )
71+
public int index( String key )
6672
{
6773
Integer result = keyIndexLookup.get( key );
6874
if ( result == null )
@@ -116,7 +122,7 @@ public <T> Map<String, T> asMap( Function<Value, T> mapFunction )
116122
@Override
117123
public String toString()
118124
{
119-
return format( "Record<%s>", formatProperties( VALUE_WITH_TYPE, fieldCount(), fields() ) );
125+
return format( "Record<%s>", formatFields( VALUE_WITH_TYPE, fieldCount(), fields() ) );
120126
}
121127

122128
public boolean equals( Object other )

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,19 @@ public int fieldCount()
3636
return keys().size();
3737
}
3838

39+
@Override
40+
public Field field( String key )
41+
{
42+
int index = index( key );
43+
return InternalField.of( key, index, value( index ) );
44+
}
45+
46+
@Override
47+
public Field field( int index )
48+
{
49+
return InternalField.of( key( index ), index, value( index) );
50+
}
51+
3952
@Override
4053
public List<Field<Value>> fields()
4154
{

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,26 +62,32 @@ public Value value( int index )
6262
return record().value( index );
6363
}
6464

65+
public Value value( String key )
66+
{
67+
return record().value( key );
68+
}
69+
6570
@Override
6671
public boolean containsKey( String key )
6772
{
6873
return keys.contains( key );
6974
}
7075

71-
public List<String> keys()
76+
@Override
77+
public String key( int index )
7278
{
73-
return keys;
79+
return record().key( index );
7480
}
7581

7682
@Override
77-
public int fieldIndex( String key )
83+
public int index( String key )
7884
{
79-
return record().fieldIndex( key );
85+
return record().index( key );
8086
}
8187

82-
public Value value( String key )
88+
public List<String> keys()
8389
{
84-
return record().value( key );
90+
return keys;
8591
}
8692

8793
private Value throwNoRecord()

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/main/java/org/neo4j/driver/v1/RecordAccessor.java

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@ public interface RecordAccessor extends ListAccessor, MapAccessor
3636
List<String> keys();
3737

3838
/**
39-
* @return number of fields in this record
39+
* Retrieve the key of the field for the given index
40+
*
41+
* @param index the index of the key
42+
* @return the key of the field for the given index
4043
*/
41-
int fieldCount();
44+
String key( int index );
4245

4346
/**
4447
* Retrieve the index of the field with the given key
@@ -47,7 +50,28 @@ public interface RecordAccessor extends ListAccessor, MapAccessor
4750
* @param key the give key
4851
* @return the index of the field as used by {@link #value(int)}
4952
*/
50-
int fieldIndex( String key );
53+
int index( String key );
54+
55+
/**
56+
* Retrieve the field for the given key
57+
*
58+
* @param key the key
59+
* @return the field for the given key
60+
*/
61+
Field field( String key );
62+
63+
/**
64+
* Retrieve the field at the given index
65+
*
66+
* @param index the index
67+
* @return the field at the given index
68+
*/
69+
Field field( int index );
70+
71+
/**
72+
* @return number of fields in this record
73+
*/
74+
int fieldCount();
5175

5276
/**
5377
* Retrieve all record fields

0 commit comments

Comments
 (0)