From aace858a93a39934138d33f6741fc96f7ec2de16 Mon Sep 17 00:00:00 2001 From: fickludd Date: Wed, 24 May 2017 14:40:32 +0200 Subject: [PATCH] Merged Value and ValueImpl again Also made custom ValueLoader interface to feed LazyValue. --- .../java/org/neo4j/values/DirectArray.java | 2 +- .../org/neo4j/values/DirectLongArray.java | 2 +- .../java/org/neo4j/values/DirectScalar.java | 2 +- .../main/java/org/neo4j/values/LazyArray.java | 4 +- .../org/neo4j/values/LazyBooleanArray.java | 3 +- .../java/org/neo4j/values/LazyByteArray.java | 3 +- .../java/org/neo4j/values/LazyCharArray.java | 3 +- .../org/neo4j/values/LazyDoubleArray.java | 3 +- .../java/org/neo4j/values/LazyFloatArray.java | 3 +- .../neo4j/values/LazyFloatingPointArray.java | 4 +- .../java/org/neo4j/values/LazyIntArray.java | 3 +- .../org/neo4j/values/LazyIntegralArray.java | 4 +- .../java/org/neo4j/values/LazyLongArray.java | 3 +- .../java/org/neo4j/values/LazyShortArray.java | 3 +- .../java/org/neo4j/values/LazyString.java | 4 +- .../org/neo4j/values/LazyStringArray.java | 3 +- .../java/org/neo4j/values/LazyTextArray.java | 4 +- .../main/java/org/neo4j/values/LazyValue.java | 27 +++++------ .../main/java/org/neo4j/values/NoValue.java | 2 +- .../src/main/java/org/neo4j/values/Value.java | 45 ++++++++++++------- .../main/java/org/neo4j/values/ValueImpl.java | 35 --------------- .../main/java/org/neo4j/values/Values.java | 33 ++++++++------ .../java/org/neo4j/values/LazyValueTest.java | 8 ++-- 23 files changed, 82 insertions(+), 121 deletions(-) delete mode 100644 community/values/src/main/java/org/neo4j/values/ValueImpl.java diff --git a/community/values/src/main/java/org/neo4j/values/DirectArray.java b/community/values/src/main/java/org/neo4j/values/DirectArray.java index 512a9f7c5a399..5ca66eff0b955 100644 --- a/community/values/src/main/java/org/neo4j/values/DirectArray.java +++ b/community/values/src/main/java/org/neo4j/values/DirectArray.java @@ -19,7 +19,7 @@ */ package org.neo4j.values; -abstract class DirectArray extends ValueImpl +abstract class DirectArray extends Value { abstract int length(); diff --git a/community/values/src/main/java/org/neo4j/values/DirectLongArray.java b/community/values/src/main/java/org/neo4j/values/DirectLongArray.java index 374b669f25ad4..dc15ca5635c9f 100644 --- a/community/values/src/main/java/org/neo4j/values/DirectLongArray.java +++ b/community/values/src/main/java/org/neo4j/values/DirectLongArray.java @@ -90,7 +90,7 @@ public void writeTo( ValueWriter writer ) @Override public Object asPublic() { - return value; + return value.clone(); } @Override diff --git a/community/values/src/main/java/org/neo4j/values/DirectScalar.java b/community/values/src/main/java/org/neo4j/values/DirectScalar.java index 1747440028ea4..295d5e2762ce2 100644 --- a/community/values/src/main/java/org/neo4j/values/DirectScalar.java +++ b/community/values/src/main/java/org/neo4j/values/DirectScalar.java @@ -19,7 +19,7 @@ */ package org.neo4j.values; -abstract class DirectScalar extends ValueImpl +abstract class DirectScalar extends Value { @Override public boolean equals( byte[] x ) diff --git a/community/values/src/main/java/org/neo4j/values/LazyArray.java b/community/values/src/main/java/org/neo4j/values/LazyArray.java index f432e7dfa34b4..fedbeaf41ed55 100644 --- a/community/values/src/main/java/org/neo4j/values/LazyArray.java +++ b/community/values/src/main/java/org/neo4j/values/LazyArray.java @@ -19,11 +19,9 @@ */ package org.neo4j.values; -import java.util.concurrent.Callable; - abstract class LazyArray extends LazyValue { - LazyArray( Callable producer ) + LazyArray( Values.ValueLoader producer ) { super( producer ); } diff --git a/community/values/src/main/java/org/neo4j/values/LazyBooleanArray.java b/community/values/src/main/java/org/neo4j/values/LazyBooleanArray.java index 6610615c4ff53..8338c720a5279 100644 --- a/community/values/src/main/java/org/neo4j/values/LazyBooleanArray.java +++ b/community/values/src/main/java/org/neo4j/values/LazyBooleanArray.java @@ -20,11 +20,10 @@ package org.neo4j.values; import java.util.Arrays; -import java.util.concurrent.Callable; final class LazyBooleanArray extends LazyArray implements ValueGroup.VBooleanArray { - LazyBooleanArray( Callable producer ) + LazyBooleanArray( Values.ValueLoader producer ) { super( producer ); } diff --git a/community/values/src/main/java/org/neo4j/values/LazyByteArray.java b/community/values/src/main/java/org/neo4j/values/LazyByteArray.java index d2e0643caa555..2b905f583dc3b 100644 --- a/community/values/src/main/java/org/neo4j/values/LazyByteArray.java +++ b/community/values/src/main/java/org/neo4j/values/LazyByteArray.java @@ -20,11 +20,10 @@ package org.neo4j.values; import java.util.Arrays; -import java.util.concurrent.Callable; final class LazyByteArray extends LazyIntegralArray { - LazyByteArray( Callable producer ) + LazyByteArray( Values.ValueLoader producer ) { super( producer ); } diff --git a/community/values/src/main/java/org/neo4j/values/LazyCharArray.java b/community/values/src/main/java/org/neo4j/values/LazyCharArray.java index 603131b03f85f..f4851ef13f5d3 100644 --- a/community/values/src/main/java/org/neo4j/values/LazyCharArray.java +++ b/community/values/src/main/java/org/neo4j/values/LazyCharArray.java @@ -20,11 +20,10 @@ package org.neo4j.values; import java.util.Arrays; -import java.util.concurrent.Callable; final class LazyCharArray extends LazyTextArray { - LazyCharArray( Callable producer ) + LazyCharArray( Values.ValueLoader producer ) { super( producer ); } diff --git a/community/values/src/main/java/org/neo4j/values/LazyDoubleArray.java b/community/values/src/main/java/org/neo4j/values/LazyDoubleArray.java index 8cc0606d16d58..2bbb2cb272cbb 100644 --- a/community/values/src/main/java/org/neo4j/values/LazyDoubleArray.java +++ b/community/values/src/main/java/org/neo4j/values/LazyDoubleArray.java @@ -20,11 +20,10 @@ package org.neo4j.values; import java.util.Arrays; -import java.util.concurrent.Callable; final class LazyDoubleArray extends LazyFloatingPointArray { - LazyDoubleArray( Callable producer ) + LazyDoubleArray( Values.ValueLoader producer ) { super( producer ); } diff --git a/community/values/src/main/java/org/neo4j/values/LazyFloatArray.java b/community/values/src/main/java/org/neo4j/values/LazyFloatArray.java index 34a42113f73e2..bb8554cb38631 100644 --- a/community/values/src/main/java/org/neo4j/values/LazyFloatArray.java +++ b/community/values/src/main/java/org/neo4j/values/LazyFloatArray.java @@ -20,11 +20,10 @@ package org.neo4j.values; import java.util.Arrays; -import java.util.concurrent.Callable; final class LazyFloatArray extends LazyFloatingPointArray { - LazyFloatArray( Callable producer ) + LazyFloatArray( Values.ValueLoader producer ) { super( producer ); } diff --git a/community/values/src/main/java/org/neo4j/values/LazyFloatingPointArray.java b/community/values/src/main/java/org/neo4j/values/LazyFloatingPointArray.java index c3689c80774d3..4c2f60e8ac55d 100644 --- a/community/values/src/main/java/org/neo4j/values/LazyFloatingPointArray.java +++ b/community/values/src/main/java/org/neo4j/values/LazyFloatingPointArray.java @@ -19,11 +19,9 @@ */ package org.neo4j.values; -import java.util.concurrent.Callable; - abstract class LazyFloatingPointArray extends LazyArray implements ValueGroup.VFloatingPointArray { - LazyFloatingPointArray( Callable producer ) + LazyFloatingPointArray( Values.ValueLoader producer ) { super( producer ); } diff --git a/community/values/src/main/java/org/neo4j/values/LazyIntArray.java b/community/values/src/main/java/org/neo4j/values/LazyIntArray.java index a4af73248ba09..3e04b4072b935 100644 --- a/community/values/src/main/java/org/neo4j/values/LazyIntArray.java +++ b/community/values/src/main/java/org/neo4j/values/LazyIntArray.java @@ -20,11 +20,10 @@ package org.neo4j.values; import java.util.Arrays; -import java.util.concurrent.Callable; final class LazyIntArray extends LazyIntegralArray { - LazyIntArray( Callable producer ) + LazyIntArray( Values.ValueLoader producer ) { super( producer ); } diff --git a/community/values/src/main/java/org/neo4j/values/LazyIntegralArray.java b/community/values/src/main/java/org/neo4j/values/LazyIntegralArray.java index 907a9114ce63d..5c1df0104da9d 100644 --- a/community/values/src/main/java/org/neo4j/values/LazyIntegralArray.java +++ b/community/values/src/main/java/org/neo4j/values/LazyIntegralArray.java @@ -19,11 +19,9 @@ */ package org.neo4j.values; -import java.util.concurrent.Callable; - abstract class LazyIntegralArray extends LazyArray implements ValueGroup.VIntegerArray { - LazyIntegralArray( Callable producer ) + LazyIntegralArray( Values.ValueLoader producer ) { super( producer ); } diff --git a/community/values/src/main/java/org/neo4j/values/LazyLongArray.java b/community/values/src/main/java/org/neo4j/values/LazyLongArray.java index b6d7bd96cb1cc..d3b3542e40934 100644 --- a/community/values/src/main/java/org/neo4j/values/LazyLongArray.java +++ b/community/values/src/main/java/org/neo4j/values/LazyLongArray.java @@ -20,11 +20,10 @@ package org.neo4j.values; import java.util.Arrays; -import java.util.concurrent.Callable; final class LazyLongArray extends LazyIntegralArray { - LazyLongArray( Callable producer ) + LazyLongArray( Values.ValueLoader producer ) { super( producer ); } diff --git a/community/values/src/main/java/org/neo4j/values/LazyShortArray.java b/community/values/src/main/java/org/neo4j/values/LazyShortArray.java index 9181848a471e4..d10b3cbd1229a 100644 --- a/community/values/src/main/java/org/neo4j/values/LazyShortArray.java +++ b/community/values/src/main/java/org/neo4j/values/LazyShortArray.java @@ -20,11 +20,10 @@ package org.neo4j.values; import java.util.Arrays; -import java.util.concurrent.Callable; final class LazyShortArray extends LazyIntegralArray { - LazyShortArray( Callable producer ) + LazyShortArray( Values.ValueLoader producer ) { super( producer ); } diff --git a/community/values/src/main/java/org/neo4j/values/LazyString.java b/community/values/src/main/java/org/neo4j/values/LazyString.java index c8830c16204c2..048d271f077dd 100644 --- a/community/values/src/main/java/org/neo4j/values/LazyString.java +++ b/community/values/src/main/java/org/neo4j/values/LazyString.java @@ -19,13 +19,11 @@ */ package org.neo4j.values; -import java.util.concurrent.Callable; - import static java.lang.String.format; final class LazyString extends LazyValue implements ValueGroup.VText { - LazyString( Callable producer ) + LazyString( Values.ValueLoader producer ) { super( producer ); } diff --git a/community/values/src/main/java/org/neo4j/values/LazyStringArray.java b/community/values/src/main/java/org/neo4j/values/LazyStringArray.java index 492bc28b0a3a4..0ee11ddbad787 100644 --- a/community/values/src/main/java/org/neo4j/values/LazyStringArray.java +++ b/community/values/src/main/java/org/neo4j/values/LazyStringArray.java @@ -20,11 +20,10 @@ package org.neo4j.values; import java.util.Arrays; -import java.util.concurrent.Callable; final class LazyStringArray extends LazyTextArray { - LazyStringArray( Callable producer ) + LazyStringArray( Values.ValueLoader producer ) { super( producer ); } diff --git a/community/values/src/main/java/org/neo4j/values/LazyTextArray.java b/community/values/src/main/java/org/neo4j/values/LazyTextArray.java index b6900620790c7..30cb1522a57bc 100644 --- a/community/values/src/main/java/org/neo4j/values/LazyTextArray.java +++ b/community/values/src/main/java/org/neo4j/values/LazyTextArray.java @@ -19,11 +19,9 @@ */ package org.neo4j.values; -import java.util.concurrent.Callable; - abstract class LazyTextArray extends LazyArray implements ValueGroup.VTextArray { - LazyTextArray( Callable producer ) + LazyTextArray( Values.ValueLoader producer ) { super( producer ); } diff --git a/community/values/src/main/java/org/neo4j/values/LazyValue.java b/community/values/src/main/java/org/neo4j/values/LazyValue.java index eb72e8a1573c2..f0b201ab7b6f9 100644 --- a/community/values/src/main/java/org/neo4j/values/LazyValue.java +++ b/community/values/src/main/java/org/neo4j/values/LazyValue.java @@ -21,11 +21,13 @@ import java.util.concurrent.Callable; -abstract class LazyValue extends ValueImpl +import org.neo4j.values.Values.ValueLoader; + +abstract class LazyValue extends Value { private volatile Object value; - LazyValue( Callable producer ) + LazyValue( ValueLoader producer ) { this.value = producer; } @@ -35,32 +37,25 @@ abstract class LazyValue extends ValueImpl */ final T getOrLoad() { - Object value = this.value; - if ( value instanceof Callable ) + Object maybeValue = this.value; + if ( maybeValue instanceof ValueLoader ) { synchronized ( this ) { - value = this.value; - if ( value instanceof Callable ) + maybeValue = this.value; + if ( maybeValue instanceof ValueLoader ) { - this.value = value = produceValue(); + this.value = produceValue(); } } } //noinspection unchecked - return (T) value; + return (T) this.value; } private Object produceValue() { - try - { - return ((Callable) value).call(); - } - catch ( Exception e ) - { - throw new RuntimeException( e ); - } + return ((ValueLoader) value).load(); } boolean valueIsLoaded() diff --git a/community/values/src/main/java/org/neo4j/values/NoValue.java b/community/values/src/main/java/org/neo4j/values/NoValue.java index 8a4f7749377c7..54774ca8658a2 100644 --- a/community/values/src/main/java/org/neo4j/values/NoValue.java +++ b/community/values/src/main/java/org/neo4j/values/NoValue.java @@ -19,7 +19,7 @@ */ package org.neo4j.values; -final class NoValue extends ValueImpl +final class NoValue extends Value { @SuppressWarnings( "WeakerAccess" ) public static NoValue NO_VALUE = new NoValue(); diff --git a/community/values/src/main/java/org/neo4j/values/Value.java b/community/values/src/main/java/org/neo4j/values/Value.java index fecf66f69b56f..55bcf155aebe5 100644 --- a/community/values/src/main/java/org/neo4j/values/Value.java +++ b/community/values/src/main/java/org/neo4j/values/Value.java @@ -19,28 +19,41 @@ */ package org.neo4j.values; -public interface Value extends ValueGroup.WithId +public abstract class Value implements ValueGroup.WithId { - boolean equals( Value other ); + @Override + public abstract boolean equals( Object other ); - boolean equals( byte[] x ); - boolean equals( short[] x ); - boolean equals( int[] x ); - boolean equals( long[] x ); + @Override + public abstract int hashCode(); - boolean equals( float[] x ); - boolean equals( double[] x ); + public abstract boolean equals( Value other ); - boolean equals( boolean x ); - boolean equals( boolean[] x ); + public abstract boolean equals( byte[] x ); - boolean equals( char x ); - boolean equals( String x ); + public abstract boolean equals( short[] x ); - boolean equals( char[] x ); - boolean equals( String[] x ); + public abstract boolean equals( int[] x ); - void writeTo( ValueWriter writer ); + public abstract boolean equals( long[] x ); - Object asPublic(); + public abstract boolean equals( float[] x ); + + public abstract boolean equals( double[] x ); + + public abstract boolean equals( boolean x ); + + public abstract boolean equals( boolean[] x ); + + public abstract boolean equals( char x ); + + public abstract boolean equals( String x ); + + public abstract boolean equals( char[] x ); + + public abstract boolean equals( String[] x ); + + public abstract void writeTo( ValueWriter writer ); + + public abstract Object asPublic(); } diff --git a/community/values/src/main/java/org/neo4j/values/ValueImpl.java b/community/values/src/main/java/org/neo4j/values/ValueImpl.java deleted file mode 100644 index 2d6f97f9939c9..0000000000000 --- a/community/values/src/main/java/org/neo4j/values/ValueImpl.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (c) 2002-2017 "Neo Technology," - * Network Engine for Objects in Lund AB [http://neotechnology.com] - * - * This file is part of Neo4j. - * - * Neo4j is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.neo4j.values; - -abstract class ValueImpl implements Value -{ - @Override - public boolean equals( Object other ) - { - throw new UnsupportedOperationException( "You forgot to implement `equals()` in concrete Value class!" ); - } - - @Override - public int hashCode() - { - throw new UnsupportedOperationException( "You forgot to implement `hashCode()` in concrete Value class!" ); - } -} diff --git a/community/values/src/main/java/org/neo4j/values/Values.java b/community/values/src/main/java/org/neo4j/values/Values.java index 56677d92cc48d..1887ddc1fdd54 100644 --- a/community/values/src/main/java/org/neo4j/values/Values.java +++ b/community/values/src/main/java/org/neo4j/values/Values.java @@ -20,7 +20,6 @@ package org.neo4j.values; import java.lang.reflect.Array; -import java.util.concurrent.Callable; @SuppressWarnings( "WeakerAccess" ) public class Values @@ -29,64 +28,70 @@ private Values() { } - // DIRECT FACTORY METHODS + interface ValueLoader + { + T load() throws ValueLoadException; + } - public static Value noValue() + class ValueLoadException extends RuntimeException { - return NoValue.NO_VALUE; } + // DIRECT FACTORY METHODS + + public static Value NO_VALUE = NoValue.NO_VALUE; + public static Value stringValue( String value ) { return new DirectString( value ); } - public static Value lazyStringValue( Callable producer ) + public static Value lazyStringValue( ValueLoader producer ) { return new LazyString( producer ); } - public static Value lazyByteArray( Callable producer ) + public static Value lazyByteArray( ValueLoader producer ) { return new LazyByteArray( producer ); } - public static Value lazyShortArray( Callable producer ) + public static Value lazyShortArray( ValueLoader producer ) { return new LazyShortArray( producer ); } - public static Value lazyIntArray( Callable producer ) + public static Value lazyIntArray( ValueLoader producer ) { return new LazyIntArray( producer ); } - public static Value lazyLongArray( Callable producer ) + public static Value lazyLongArray( ValueLoader producer ) { return new LazyLongArray( producer ); } - public static Value lazyFloatArray( Callable producer ) + public static Value lazyFloatArray( ValueLoader producer ) { return new LazyFloatArray( producer ); } - public static Value lazyDoubleArray( Callable producer ) + public static Value lazyDoubleArray( ValueLoader producer ) { return new LazyDoubleArray( producer ); } - public static Value lazyCharArray( Callable producer ) + public static Value lazyCharArray( ValueLoader producer ) { return new LazyCharArray( producer ); } - public static Value lazyStringArray( Callable producer ) + public static Value lazyStringArray( ValueLoader producer ) { return new LazyStringArray( producer ); } - public static Value lazyBooleanArray( Callable producer ) + public static Value lazyBooleanArray( ValueLoader producer ) { return new LazyBooleanArray( producer ); } diff --git a/community/values/src/test/java/org/neo4j/values/LazyValueTest.java b/community/values/src/test/java/org/neo4j/values/LazyValueTest.java index 4b203488d95b9..a20801fd9a23b 100644 --- a/community/values/src/test/java/org/neo4j/values/LazyValueTest.java +++ b/community/values/src/test/java/org/neo4j/values/LazyValueTest.java @@ -23,6 +23,8 @@ import java.util.concurrent.Callable; +import org.neo4j.values.Values.ValueLoader; + import static org.hamcrest.Matchers.equalTo; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertThat; @@ -67,14 +69,14 @@ public void shouldLoadLazyArrayValueOnlyOnce() throws Exception assertThat( value.getOrLoad(), equalTo( new int[]{1, 2, 3} ) ); } - private static Callable value( final T value ) + private static ValueLoader value( final T value ) { - return new Callable() + return new ValueLoader() { boolean called; @Override - public T call() throws Exception + public T load() throws Values.ValueLoadException { assertFalse( "Already called for value: " + value, called ); called = true;