MINOR: Remove Redundant BiValue Interface and Rename Abstract Class Accordingly

Fixes #7850
This commit is contained in:
Armin 2017-07-30 20:42:35 +02:00 committed by Armin Braun
parent 601d9aeebe
commit cda021341d
15 changed files with 116 additions and 134 deletions

View file

@ -1,18 +1,112 @@
package org.logstash.bivalues;
import com.fasterxml.jackson.annotation.JsonValue;
import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.io.ObjectStreamException;
import java.io.Serializable;
import org.jruby.Ruby;
import org.jruby.runtime.builtin.IRubyObject;
public interface BiValue<R extends IRubyObject, J> {
IRubyObject rubyValue(Ruby runtime);
public abstract class BiValue<R extends IRubyObject, J> implements Serializable {
J javaValue();
private static final long serialVersionUID = -8602478677605589528L;
R rubyValueUnconverted();
protected transient R rubyValue;
protected J javaValue;
boolean hasRubyValue();
public final R rubyValue(Ruby runtime) {
if (hasRubyValue()) {
return rubyValue;
}
addRuby(runtime);
return rubyValue;
}
boolean hasJavaValue();
@JsonValue
public J javaValue() {
if (javaValue == null) {
addJava();
}
return javaValue;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (hasJavaValue() && javaValue.getClass().isAssignableFrom(o.getClass())){
return javaValue.equals(o);
}
if(!(o instanceof BiValue)) {
return false;
}
BiValue<?, ?> other = (BiValue<?, ?>) o;
return (other.hasJavaValue() && other.javaValue().equals(javaValue)) ||
(other.hasRubyValue() && other.rubyValueUnconverted().equals(rubyValue));
}
@Override
public final int hashCode() {
if (hasRubyValue()) {
return rubyValue.hashCode();
}
if (hasJavaValue()) {
return javaValue.hashCode();
}
return 0;
}
public final R rubyValueUnconverted() {
return rubyValue;
}
public boolean hasRubyValue() {
return null != rubyValue;
}
public boolean hasJavaValue() {
return null != javaValue;
}
protected abstract void addRuby(Ruby runtime);
protected abstract void addJava();
@Override
public String toString() {
if (hasRubyValue()) {
javaValue();
}
if (javaValue == null) {
return "";
}
return String.valueOf(javaValue);
}
protected static Object newProxy(BiValue instance) {
return new SerializationProxy(instance);
}
private static final class SerializationProxy implements Serializable {
private static final long serialVersionUID = -1749700725129586973L;
private final Object javaValue;
public SerializationProxy(BiValue o) {
javaValue = o.javaValue(); // ensure the javaValue is converted from a ruby one if it exists
}
private Object readResolve() throws ObjectStreamException {
return BiValues.newBiValue(javaValue);
}
}
private void readObject(ObjectInputStream stream) throws InvalidObjectException {
throw new InvalidObjectException("Proxy required");
}
}

View file

@ -1,110 +0,0 @@
package org.logstash.bivalues;
import com.fasterxml.jackson.annotation.JsonValue;
import org.jruby.Ruby;
import org.jruby.runtime.builtin.IRubyObject;
import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.io.ObjectStreamException;
import java.io.Serializable;
public abstract class BiValueCommon<R extends IRubyObject, J> implements Serializable {
protected transient R rubyValue;
protected J javaValue;
public R rubyValue(Ruby runtime) {
if (hasRubyValue()) {
return rubyValue;
}
addRuby(runtime);
return rubyValue;
}
@JsonValue
public J javaValue() {
if (javaValue == null) {
addJava();
}
return javaValue;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (hasJavaValue() && javaValue.getClass().isAssignableFrom(o.getClass())){
return javaValue.equals(o);
}
if(!(o instanceof BiValue)) {
return false;
}
BiValueCommon<?, ?> other = (BiValueCommon<?, ?>) o;
return (other.hasJavaValue() && other.javaValue().equals(javaValue)) ||
(other.hasRubyValue() && other.rubyValueUnconverted().equals(rubyValue));
}
@Override
public int hashCode() {
if (hasRubyValue()) {
return rubyValue.hashCode();
}
if (hasJavaValue()) {
return javaValue.hashCode();
}
return 0;
}
public R rubyValueUnconverted() {
return rubyValue;
}
public boolean hasRubyValue() {
return null != rubyValue;
}
public boolean hasJavaValue() {
return null != javaValue;
}
protected abstract void addRuby(Ruby runtime);
protected abstract void addJava();
@Override
public String toString() {
if (hasRubyValue()) {
javaValue();
}
if (javaValue == null) {
return "";
}
return String.valueOf(javaValue);
}
protected static Object newProxy(BiValue instance) {
return new SerializationProxy(instance);
}
private static class SerializationProxy implements Serializable {
private static final long serialVersionUID = -1749700725129586973L;
private final Object javaValue;
public SerializationProxy(BiValue o) {
javaValue = o.javaValue(); // ensure the javaValue is converted from a ruby one if it exists
}
private Object readResolve() throws ObjectStreamException {
return BiValues.newBiValue(javaValue);
}
}
private void readObject(ObjectInputStream stream) throws InvalidObjectException {
throw new InvalidObjectException("Proxy required");
}
}

View file

@ -6,7 +6,7 @@ import org.jruby.ext.bigdecimal.RubyBigDecimal;
import java.io.ObjectStreamException;
import java.math.BigDecimal;
public class BigDecimalBiValue extends BiValueCommon<RubyBigDecimal, BigDecimal> implements BiValue<RubyBigDecimal, BigDecimal> {
public class BigDecimalBiValue extends BiValue<RubyBigDecimal, BigDecimal> {
public BigDecimalBiValue(RubyBigDecimal rubyValue) {
this.rubyValue = rubyValue;

View file

@ -6,7 +6,7 @@ import org.jruby.RubyBignum;
import java.io.ObjectStreamException;
import java.math.BigInteger;
public class BigIntegerBiValue extends BiValueCommon<RubyBignum, BigInteger> implements BiValue<RubyBignum, BigInteger> {
public class BigIntegerBiValue extends BiValue<RubyBignum, BigInteger> {
public BigIntegerBiValue(RubyBignum rubyValue) {
this.rubyValue = rubyValue;

View file

@ -6,7 +6,7 @@ import org.jruby.RubyBoolean;
import java.io.ObjectStreamException;
public class BooleanBiValue extends BiValueCommon<RubyBoolean, Boolean> implements BiValue<RubyBoolean, Boolean> {
public class BooleanBiValue extends BiValue<RubyBoolean, Boolean> {
public BooleanBiValue(RubyBoolean rubyValue) {
this.rubyValue = rubyValue;

View file

@ -6,7 +6,7 @@ import org.jruby.RubyFloat;
import java.io.ObjectStreamException;
public class DoubleBiValue extends BiValueCommon<RubyFloat, Double> implements BiValue<RubyFloat, Double> {
public class DoubleBiValue extends BiValue<RubyFloat, Double> {
public DoubleBiValue(RubyFloat rubyValue) {
this.rubyValue = rubyValue;

View file

@ -6,7 +6,7 @@ import org.jruby.RubyFloat;
import java.io.ObjectStreamException;
public class FloatBiValue extends BiValueCommon<RubyFloat, Float> implements BiValue<RubyFloat, Float> {
public class FloatBiValue extends BiValue<RubyFloat, Float> {
public FloatBiValue(RubyFloat rubyValue) {
this.rubyValue = rubyValue;

View file

@ -6,7 +6,7 @@ import org.jruby.javasupport.JavaUtil;
import java.io.ObjectStreamException;
public class IntegerBiValue extends BiValueCommon<RubyInteger, Integer> implements BiValue<RubyInteger, Integer> {
public class IntegerBiValue extends BiValue<RubyInteger, Integer> {
public IntegerBiValue(RubyInteger rubyValue) {
this.rubyValue = rubyValue;

View file

@ -6,7 +6,7 @@ import org.jruby.javasupport.JavaUtil;
import java.io.ObjectStreamException;
public class JavaProxyBiValue extends BiValueCommon<JavaProxy, Object> implements BiValue<JavaProxy, Object> {
public class JavaProxyBiValue extends BiValue<JavaProxy, Object> {
public JavaProxyBiValue(JavaProxy rubyValue) {
this.rubyValue = rubyValue;

View file

@ -6,7 +6,7 @@ import org.jruby.javasupport.JavaUtil;
import java.io.ObjectStreamException;
public class LongBiValue extends BiValueCommon<RubyInteger, Long> implements BiValue<RubyInteger, Long> {
public class LongBiValue extends BiValue<RubyInteger, Long> {
public LongBiValue(RubyInteger rubyValue) {
this.rubyValue = rubyValue;

View file

@ -5,8 +5,7 @@ import java.io.ObjectStreamException;
import org.jruby.Ruby;
import org.jruby.RubyNil;
public final class NullBiValue extends BiValueCommon<RubyNil, Object>
implements BiValue<RubyNil, Object> {
public final class NullBiValue extends BiValue<RubyNil, Object> {
private static final NullBiValue INSTANCE =
new NullBiValue((RubyNil) Ruby.getGlobalRuntime().getNil());

View file

@ -5,8 +5,7 @@ import java.io.ObjectStreamException;
import org.jruby.Ruby;
import org.jruby.RubyString;
public final class StringBiValue extends BiValueCommon<RubyString, String>
implements BiValue<RubyString, String> {
public final class StringBiValue extends BiValue<RubyString, String> {
public StringBiValue(RubyString rubyValue) {
this.rubyValue = rubyValue;
@ -30,7 +29,7 @@ public final class StringBiValue extends BiValueCommon<RubyString, String>
public boolean equals(Object o) {
if (this == o) return true;
if (o instanceof BiValue) {
final BiValueCommon<?, ?> other = (BiValueCommon<?, ?>) o;
final BiValue<?, ?> other = (BiValue<?, ?>) o;
return other.hasRubyValue() && other.rubyValueUnconverted().equals(rubyValue) ||
(other.hasJavaValue() && other.javaValue().equals(this.javaValue()));
} else {

View file

@ -5,7 +5,7 @@ import org.jruby.RubySymbol;
import java.io.ObjectStreamException;
public class SymbolBiValue extends BiValueCommon<RubySymbol, String> implements BiValue<RubySymbol, String> {
public class SymbolBiValue extends BiValue<RubySymbol, String> {
public SymbolBiValue(RubySymbol rubyValue) {
this.rubyValue = rubyValue;

View file

@ -7,7 +7,7 @@ import org.jruby.RubyTime;
import java.io.ObjectStreamException;
public class TimeBiValue extends BiValueCommon<RubyTime, DateTime> implements BiValue<RubyTime, DateTime> {
public class TimeBiValue extends BiValue<RubyTime, DateTime> {
public TimeBiValue(RubyTime rubyValue) {
this.rubyValue = rubyValue;

View file

@ -6,7 +6,7 @@ import org.jruby.Ruby;
import java.io.ObjectStreamException;
public class TimestampBiValue extends BiValueCommon<RubyTimestamp, Timestamp> implements BiValue<RubyTimestamp, Timestamp> {
public class TimestampBiValue extends BiValue<RubyTimestamp, Timestamp> {
public TimestampBiValue(RubyTimestamp rubyValue) {
this.rubyValue = rubyValue;