Skip to content

Commit 90313f2

Browse files
bogglejakewins
authored andcommitted
Rewire how toString() works and some refactoring around Value implementation
1 parent 6f1b9f6 commit 90313f2

29 files changed

+437
-294
lines changed

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

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

21+
import java.util.Objects;
22+
23+
import org.neo4j.driver.v1.Function;
2124
import org.neo4j.driver.v1.Property;
2225

2326
public class InternalProperty<V> implements Property<V>
@@ -46,6 +49,17 @@ public static <V> Property<V> of( String key, V value )
4649
return new InternalProperty<>( key, value );
4750
}
4851

52+
@Override
53+
public String toString()
54+
{
55+
return String.format( "%s: %s", key, Objects.toString( value ) );
56+
}
57+
58+
public String toString( Function<V, String> printValue )
59+
{
60+
return String.format( "%s: %s", key, printValue.apply( value ) );
61+
}
62+
4963
@Override
5064
public boolean equals( Object o )
5165
{
@@ -70,10 +84,4 @@ public int hashCode()
7084
result = 31 * result + value.hashCode();
7185
return result;
7286
}
73-
74-
@Override
75-
public String toString()
76-
{
77-
return String.format( "%s: %s", key, value.toString() );
78-
}
7987
}

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
import org.neo4j.driver.v1.Value;
3030
import org.neo4j.driver.v1.Values;
3131

32+
import static java.lang.String.format;
33+
34+
import static org.neo4j.driver.internal.util.Format.formatProperties;
35+
import static org.neo4j.driver.internal.value.InternalValue.Format.VALUE_WITH_TYPE;
3236
import static org.neo4j.driver.v1.Values.valueAsIs;
3337

3438
public class InternalRecord extends InternalRecordAccessor implements Record
@@ -109,13 +113,10 @@ public <T> Map<String, T> asMap( Function<Value, T> mapFunction )
109113
return Extract.map( this, mapFunction );
110114
}
111115

112-
public int hashcode()
116+
@Override
117+
public String toString()
113118
{
114-
if ( hashcode == 0 )
115-
{
116-
hashcode = 31 * keys.hashCode() + Arrays.hashCode( values );
117-
}
118-
return hashcode;
119+
return format( "Record<%s>", formatProperties( VALUE_WITH_TYPE, fieldCount(), fields() ) );
119120
}
120121

121122
public boolean equals( Object other )
@@ -152,4 +153,13 @@ else if ( other instanceof Record )
152153
return false;
153154
}
154155
}
156+
157+
public int hashcode()
158+
{
159+
if ( hashcode == 0 )
160+
{
161+
hashcode = 31 * keys.hashCode() + Arrays.hashCode( values );
162+
}
163+
return hashcode;
164+
}
155165
}

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

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
import java.util.Iterator;
2222

23+
import org.neo4j.driver.internal.InternalProperty;
24+
import org.neo4j.driver.v1.Function;
2325
import org.neo4j.driver.v1.Property;
2426
import org.neo4j.driver.v1.Value;
2527

@@ -30,27 +32,64 @@ private Format()
3032
throw new UnsupportedOperationException();
3133
}
3234

33-
public static <V extends Property<Value>> String properties( int propertyCount, Iterable<V> properties )
35+
public static <V extends Property<Value>> String formatProperties( Function<Value, String> printValue,
36+
int propertyCount,
37+
Iterable<V> properties )
3438
{
35-
switch (propertyCount) {
39+
switch ( propertyCount ) {
3640
case 0:
3741
return "{}";
3842

3943
case 1:
40-
return String.format( "{%s}", properties.iterator().next() );
44+
{
45+
return String.format( "{%s}", internalProperty( properties.iterator().next() ).toString( printValue ) );
46+
}
4147

4248
default:
49+
{
4350
StringBuilder builder = new StringBuilder();
44-
builder.append("{");
51+
builder.append( "{" );
4552
Iterator<V> iterator = properties.iterator();
46-
builder.append( iterator.next() );
47-
while( iterator.hasNext() )
53+
builder.append( internalProperty( iterator.next() ).toString( printValue ) );
54+
while ( iterator.hasNext() )
4855
{
4956
builder.append( ',' );
5057
builder.append( ' ' );
51-
builder.append( iterator.next() );
58+
builder.append( internalProperty( iterator.next() ).toString( printValue ) );
5259
}
53-
builder.append("}");
60+
builder.append( "}" );
61+
return builder.toString();
62+
}
63+
}
64+
}
65+
66+
@SuppressWarnings("unchecked")
67+
private static <V extends Property<Value>> InternalProperty<Value> internalProperty( V property )
68+
{
69+
return (InternalProperty<Value>) property;
70+
}
71+
72+
public static String formatElements( Function<Value, String> printValue, Value[] elements )
73+
{
74+
int elementCount = elements.length;
75+
switch ( elementCount ) {
76+
case 0:
77+
return "[]";
78+
79+
case 1:
80+
return String.format( "[%s]", printValue.apply( elements[0] ) );
81+
82+
default:
83+
StringBuilder builder = new StringBuilder();
84+
builder.append("[");
85+
builder.append( printValue.apply( elements[0] ) );
86+
for (int i = 1; i < elementCount; i++ )
87+
{
88+
builder.append( ',' );
89+
builder.append( ' ' );
90+
builder.append( printValue.apply( elements[i] ) );
91+
}
92+
builder.append("]");
5493
return builder.toString();
5594
}
5695
}

driver/src/main/java/org/neo4j/driver/internal/value/BooleanValue.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import org.neo4j.driver.internal.types.InternalTypeSystem;
2222
import org.neo4j.driver.v1.Type;
2323

24-
public abstract class BooleanValue extends ValueAdapter
24+
public abstract class BooleanValue extends ScalarValueAdapter
2525
{
2626
private BooleanValue()
2727
{
@@ -36,6 +36,9 @@ public static BooleanValue fromBoolean( boolean value )
3636
return value ? TRUE : FALSE;
3737
}
3838

39+
@Override
40+
public abstract Boolean asObject();
41+
3942
@Override
4043
public Type type()
4144
{
@@ -51,7 +54,7 @@ public int hashCode()
5154
private static class TrueValue extends BooleanValue {
5255

5356
@Override
54-
public Object asObject()
57+
public Boolean asObject()
5558
{
5659
return Boolean.TRUE;
5760
}
@@ -82,7 +85,7 @@ public boolean equals( Object obj )
8285
}
8386

8487
@Override
85-
protected String asLiteralString()
88+
public String asLiteralString()
8689
{
8790
return "TRUE";
8891
}
@@ -91,7 +94,7 @@ protected String asLiteralString()
9194
private static class FalseValue extends BooleanValue
9295
{
9396
@Override
94-
public Object asObject()
97+
public Boolean asObject()
9598
{
9699
return Boolean.FALSE;
97100
}
@@ -122,7 +125,7 @@ public boolean equals( Object obj )
122125
}
123126

124127
@Override
125-
protected String asLiteralString()
128+
public String asLiteralString()
126129
{
127130
return "FALSE";
128131
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* Copyright (c) 2002-2015 "Neo Technology,"
3+
* Network Engine for Objects in Lund AB [http://neotechnology.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.neo4j.driver.internal.value;
20+
21+
import org.neo4j.driver.v1.Entity;
22+
import org.neo4j.driver.v1.Value;
23+
24+
public abstract class EntityValueAdapter<V extends Entity> extends GraphValueAdapter<V>
25+
{
26+
protected EntityValueAdapter( V adapted )
27+
{
28+
super( adapted );
29+
}
30+
31+
public V asEntity()
32+
{
33+
return asObject();
34+
}
35+
36+
@Override
37+
public int propertyCount()
38+
{
39+
return asEntity().propertyCount();
40+
}
41+
42+
@Override
43+
public Iterable<String> keys()
44+
{
45+
return asEntity().keys();
46+
}
47+
48+
@Override
49+
public Value value( String key )
50+
{
51+
return asEntity().value( key );
52+
}
53+
}

driver/src/main/java/org/neo4j/driver/internal/value/FloatValue.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import org.neo4j.driver.v1.Type;
2323
import org.neo4j.driver.v1.exceptions.value.LossyCoercion;
2424

25-
public class FloatValue extends NumberValueAdapter
25+
public class FloatValue extends NumberValueAdapter<Double>
2626
{
2727
private final double val;
2828

@@ -37,7 +37,8 @@ public Type type()
3737
return InternalTypeSystem.TYPE_SYSTEM.FLOAT();
3838
}
3939

40-
public Number asNumber()
40+
@Override
41+
public Double asNumber()
4142
{
4243
return val;
4344
}
@@ -131,7 +132,7 @@ public int hashCode()
131132
}
132133

133134
@Override
134-
protected String asLiteralString()
135+
public String asLiteralString()
135136
{
136137
return Double.toString( val );
137138
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* Copyright (c) 2002-2015 "Neo Technology,"
3+
* Network Engine for Objects in Lund AB [http://neotechnology.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.neo4j.driver.internal.value;
20+
21+
import static java.lang.String.format;
22+
23+
public abstract class GraphValueAdapter<V> extends ValueAdapter
24+
{
25+
private final V adapted;
26+
27+
protected GraphValueAdapter( V adapted )
28+
{
29+
if ( adapted == null )
30+
{
31+
throw new IllegalArgumentException( format( "Cannot construct %s from null", getClass().getSimpleName() ) );
32+
}
33+
this.adapted = adapted;
34+
}
35+
36+
@Override
37+
public int size()
38+
{
39+
return propertyCount();
40+
}
41+
42+
@Override
43+
public V asObject()
44+
{
45+
return adapted;
46+
}
47+
48+
@Override
49+
public String toString( Format valueFormat )
50+
{
51+
return maybeWithType( valueFormat.includeType(), adapted.toString() );
52+
}
53+
54+
@Override
55+
public boolean equals( Object o )
56+
{
57+
if ( this == o )
58+
{
59+
return true;
60+
}
61+
if ( o == null || getClass() != o.getClass() )
62+
{
63+
return false;
64+
}
65+
66+
GraphValueAdapter<?> values = (GraphValueAdapter<?>) o;
67+
return adapted.equals( values.adapted );
68+
69+
}
70+
71+
@Override
72+
public int hashCode()
73+
{
74+
return adapted.hashCode();
75+
}
76+
}

0 commit comments

Comments
 (0)