Skip to content

Commit b78b2cb

Browse files
committed
Add fieldIndex
1 parent 100c389 commit b78b2cb

File tree

4 files changed

+57
-7
lines changed

4 files changed

+57
-7
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Arrays;
2222
import java.util.List;
2323
import java.util.Map;
24+
import java.util.NoSuchElementException;
2425

2526
import org.neo4j.driver.internal.util.Extract;
2627
import org.neo4j.driver.v1.Function;
@@ -55,6 +56,20 @@ public List<String> keys()
5556
return keys;
5657
}
5758

59+
@Override
60+
public int fieldIndex( String key )
61+
{
62+
Integer result = keyIndexLookup.get( key );
63+
if ( result == null )
64+
{
65+
throw new NoSuchElementException( "Unknown key: " + key );
66+
}
67+
else
68+
{
69+
return result;
70+
}
71+
}
72+
5873
@Override
5974
public boolean containsKey( String key )
6075
{

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public boolean isOpen()
5959

6060
public Value value( int index )
6161
{
62-
return current == null ? throwNoRecord() : current.value( index );
62+
return record().value( index );
6363
}
6464

6565
@Override
@@ -73,10 +73,15 @@ public List<String> keys()
7373
return keys;
7474
}
7575

76+
@Override
77+
public int fieldIndex( String key )
78+
{
79+
return record().fieldIndex( key );
80+
}
81+
7682
public Value value( String key )
7783
{
78-
assertOpen();
79-
return current == null ? throwNoRecord() : current.value( key );
84+
return record().value( key );
8085
}
8186

8287
private Value throwNoRecord()
@@ -102,23 +107,20 @@ public Record record()
102107
public long position()
103108
{
104109
assertOpen();
105-
106110
return position;
107111
}
108112

109113
@Override
110114
public boolean atEnd()
111115
{
112116
assertOpen();
113-
114117
return !iter.hasNext();
115118
}
116119

117120
@Override
118121
public boolean next()
119122
{
120123
assertOpen();
121-
122124
if ( iter.hasNext() )
123125
{
124126
current = iter.next();

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ public interface RecordAccessor extends ListAccessor, MapAccessor
4040
*/
4141
int fieldCount();
4242

43+
/**
44+
* Retrieve the index of the field with the given key
45+
*
46+
* @throws java.util.NoSuchElementException if the given key is not from {@link #keys()}
47+
* @param key the give key
48+
* @return the index of the field as used by {@link #value(int)}
49+
*/
50+
int fieldIndex( String key );
51+
4352
/**
4453
* Retrieve all record fields
4554
*

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
*/
1919
package org.neo4j.driver.internal;
2020

21-
2221
import java.util.Arrays;
2322
import java.util.HashMap;
2423
import java.util.List;
2524
import java.util.Map;
25+
import java.util.NoSuchElementException;
2626

2727
import org.junit.Test;
2828

@@ -35,6 +35,7 @@
3535
import static org.junit.Assert.assertFalse;
3636
import static org.junit.Assert.assertThat;
3737
import static org.junit.Assert.assertTrue;
38+
import static org.junit.Assert.fail;
3839

3940
import static org.neo4j.driver.v1.Values.value;
4041

@@ -57,6 +58,29 @@ public void shouldHaveCorrectFieldCount()
5758
assertThat( record.fieldCount(), equalTo( 2 ) );
5859
}
5960

61+
@Test
62+
public void shouldHaveCorrectFieldIndices()
63+
{
64+
InternalRecord record = createRecord();
65+
assertThat( record.fieldIndex( "k1" ), equalTo( 0 ) );
66+
assertThat( record.fieldIndex( "k2" ), equalTo( 1 ) );
67+
}
68+
69+
@Test
70+
public void shouldThrowWhenAskingForIndexOfUnknownField()
71+
{
72+
InternalRecord record = createRecord();
73+
try
74+
{
75+
record.fieldIndex( "BATMAN" );
76+
fail( "Expected NoSuchElementException to be thrown" );
77+
}
78+
catch ( NoSuchElementException e )
79+
{
80+
// yay
81+
}
82+
}
83+
6084
@Test
6185
public void accessingOutOfBoundsShouldBeNull()
6286
{

0 commit comments

Comments
 (0)