Skip to content

Commit adb16ec

Browse files
committed
Add missing RecordAccessor methods: index, field(String), field(int)
1 parent 70cd1a8 commit adb16ec

File tree

6 files changed

+67
-18
lines changed

6 files changed

+67
-18
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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 )

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/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

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,15 +229,15 @@ public interface Value extends PropertyMapAccessor, ListAccessor
229229
Identity asIdentity();
230230

231231
/**
232-
* @return the value as a list of values, if possible
232+
* @return the value as a Java list of values, if possible
233233
*/
234234
List<Value> asList();
235235

236236
/**
237237
* @param mapFunction a function to map from Value to T. See {@link Values} for some predefined functions, such
238238
* as {@link Values#valueAsBoolean()}, {@link Values#valueAsList(Function)}.
239-
* @param <T> the type of list elements
240-
* @return the value as a list of T, if possible
239+
* @param <T> the type of target list elements
240+
* @return the value as a list of T obtained by mapping from the list elements, if possible
241241
*/
242242
<T> List<T> asList( Function<Value, T> mapFunction );
243243

@@ -304,15 +304,15 @@ public interface Value extends PropertyMapAccessor, ListAccessor
304304
float[] asFloatArray();
305305

306306
/**
307-
* @return the value as a value map, if possible
307+
* @return the value as a Java value map, if possible
308308
*/
309309
Map<String, Value> asMap();
310310

311311
/**
312312
* @param mapFunction a function to map from Value to T. See {@link Values} for some predefined functions, such
313313
* as {@link Values#valueAsBoolean()}, {@link Values#valueAsList(Function)}.
314314
* @param <T> the type of map values
315-
* @return the value as a map from string keys to values of type T, if possible
315+
* @return the value as a map from string keys to values of type T obtained from mapping he original map values, if possible
316316
*/
317317
<T> Map<String, T> asMap( Function<Value, T> mapFunction );
318318

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ public void shouldHaveCorrectFieldCount()
6262
public void shouldHaveCorrectFieldIndices()
6363
{
6464
InternalRecord record = createRecord();
65-
assertThat( record.fieldIndex( "k1" ), equalTo( 0 ) );
66-
assertThat( record.fieldIndex( "k2" ), equalTo( 1 ) );
65+
assertThat( record.index( "k1" ), equalTo( 0 ) );
66+
assertThat( record.index( "k2" ), equalTo( 1 ) );
6767
}
6868

6969
@Test
@@ -72,7 +72,7 @@ public void shouldThrowWhenAskingForIndexOfUnknownField()
7272
InternalRecord record = createRecord();
7373
try
7474
{
75-
record.fieldIndex( "BATMAN" );
75+
record.index( "BATMAN" );
7676
fail( "Expected NoSuchElementException to be thrown" );
7777
}
7878
catch ( NoSuchElementException e )

0 commit comments

Comments
 (0)