Permalink
Browse files

mark JsonValue objects Cloneable

  • Loading branch information...
ropalka committed Sep 17, 2012
1 parent d18211e commit ce0bca34a407be46ee0887a590832934b0608375
@@ -92,5 +92,7 @@
JsonValue[] toArray();
void writeTo( JsonWriter writer ) throws IOException;
+
+ JsonArray clone();
}
@@ -23,7 +23,11 @@
* @author <a href="mailto:opalka dot richard at gmail dot com">Richard Opalka</a>
*/
public interface JsonBoolean extends JsonValue {
+
+ void setBoolean( boolean b );
boolean getBoolean();
+
+ JsonBoolean clone();
}
@@ -26,21 +26,39 @@
* @author <a href="mailto:opalka dot richard at gmail dot com">Richard Opalka</a>
*/
public interface JsonNumber extends JsonValue {
+
+ void setByte( byte b );
byte getByte();
+
+ void setShort( short s );
short getShort();
+ void setInt( int i );
+
int getInt();
+
+ void setLong( long l );
long getLong();
+
+ void setFloat( float f );
float getFloat();
+
+ void setDouble( double d );
double getDouble();
+
+ void setBigInteger( BigInteger bi );
BigInteger getBigInteger();
+
+ void setBigDecimal( BigDecimal bd );
BigDecimal getBigDecimal();
+
+ JsonNumber clone();
}
@@ -50,5 +50,7 @@
JsonValue remove( final String key );
void writeTo( JsonWriter writer ) throws IOException;
+
+ JsonObject clone();
}
@@ -24,6 +24,10 @@
*/
public interface JsonString extends JsonValue {
+ void setString( String s );
+
String getString();
+
+ JsonString clone();
}
@@ -22,4 +22,8 @@
/**
* @author <a href="mailto:opalka dot richard at gmail dot com">Richard Opalka</a>
*/
-public interface JsonValue {}
+public interface JsonValue extends Cloneable {
+
+ JsonValue clone();
+
+}
@@ -81,7 +81,7 @@ public boolean addAll( final Collection< ? extends JsonValue > values ) {
public boolean addAll( final int index, final Collection< ? extends JsonValue > values ) {
return list.addAll( index, values );
}
-
+
public boolean contains( final String value ) {
return list.contains( toJsonString( value ) );
}
@@ -285,6 +285,15 @@ public boolean equals( final Object o ) {
return list.equals( a.list );
}
+ @Override
+ public JsonArrayImpl clone() {
+ final JsonArrayImpl retVal = new JsonArrayImpl();
+ for ( final JsonValue jsonValue : list ) {
+ retVal.add( jsonValue != null ? jsonValue.clone() : null );
+ }
+ return retVal;
+ }
+
@Override
public String toString() {
return list.toString();
@@ -315,4 +324,5 @@ protected void writeTo( final JsonWriterImpl jsonWriter ) throws IOException {
boolean addInternal( final JsonValue value ) {
return list.add( value );
}
+
}
@@ -19,30 +19,33 @@
*/
package com.fossnova.json;
+import static java.lang.Boolean.FALSE;
+import static java.lang.Boolean.TRUE;
+
import org.fossnova.json.JsonBoolean;
/**
* @author <a href="mailto:opalka dot richard at gmail dot com">Richard Opalka</a>
*/
final class JsonBooleanImpl implements JsonBoolean {
- static final JsonBooleanImpl TRUE = new JsonBooleanImpl( true );
-
- static final JsonBooleanImpl FALSE = new JsonBooleanImpl( false );
-
- private final Boolean value;
+ private boolean value;
JsonBooleanImpl( final boolean value ) {
this.value = value;
}
+
+ public void setBoolean( final boolean value ) {
+ this.value = value;
+ }
public boolean getBoolean() {
return value;
}
-
+
@Override
public int hashCode() {
- return value.hashCode();
+ return value ? TRUE.hashCode() : FALSE.hashCode();
}
@Override
@@ -55,6 +58,11 @@ public boolean equals( final Object o ) {
@Override
public String toString() {
- return value.toString();
+ return value ? TRUE.toString() : FALSE.toString();
+ }
+
+ @Override
+ public JsonBooleanImpl clone() {
+ return new JsonBooleanImpl( value );
}
}
@@ -29,7 +29,7 @@
*/
final class JsonNumberImpl implements JsonNumber {
- private final String value;
+ private String value;
JsonNumberImpl( final String value ) {
this.value = value;
@@ -67,6 +67,44 @@
value = String.valueOf( data );
}
+ public void setByte( final byte value ) {
+ this.value = String.valueOf( value );
+ }
+
+ public void setShort( final short value ) {
+ this.value = String.valueOf( value );
+ }
+
+ public void setInt( final int value ) {
+ this.value = String.valueOf( value );
+ }
+
+ public void setLong( final long value ) {
+ this.value = String.valueOf( value );
+ }
+
+ public void setFloat( final float value ) {
+ this.value = String.valueOf( value );
+ }
+
+ public void setDouble( final double value ) {
+ this.value = String.valueOf( value );
+ }
+
+ public void setBigInteger( final BigInteger value ) {
+ if ( value == null ) {
+ throw new IllegalArgumentException( "Parameter cannot be null ");
+ }
+ this.value = String.valueOf( value );
+ }
+
+ public void setBigDecimal( final BigDecimal value ) {
+ if ( value == null ) {
+ throw new IllegalArgumentException( "Parameter cannot be null ");
+ }
+ this.value = String.valueOf( value );
+ }
+
public byte getByte() {
return Byte.parseByte( value );
}
@@ -116,4 +154,9 @@ public boolean equals( final Object o ) {
public String toString() {
return value;
}
+
+ @Override
+ public JsonNumberImpl clone() {
+ return new JsonNumberImpl( value );
+ }
}
@@ -164,6 +164,17 @@ public String toString() {
return map.toString();
}
+ @Override
+ public JsonObjectImpl clone() {
+ final JsonObjectImpl retVal = new JsonObjectImpl();
+ JsonValue jsonValue = null;
+ for ( final String jsonKey : map.keySet() ) {
+ jsonValue = map.get( jsonKey );
+ retVal.put( jsonKey, jsonValue != null ? jsonValue.clone() : null );
+ }
+ return retVal;
+ }
+
@Override
protected void writeTo( final JsonWriterImpl jsonWriter ) throws IOException {
jsonWriter.writeObjectStart();
@@ -26,11 +26,18 @@
*/
final class JsonStringImpl implements JsonString {
- private final String value;
+ private String value;
JsonStringImpl( final String value ) {
this.value = value;
}
+
+ public void setString( final String value ) {
+ if ( value == null ) {
+ throw new IllegalArgumentException( "Parameter cannot be null" );
+ }
+ this.value = value;
+ }
public String getString() {
return value;
@@ -53,4 +60,9 @@ public boolean equals( final Object o ) {
public String toString() {
return value;
}
+
+ @Override
+ public JsonStringImpl clone() {
+ return new JsonStringImpl( value );
+ }
}
@@ -53,7 +53,7 @@ protected final JsonBooleanImpl toJsonBoolean( final Boolean value ) {
if ( value == null ) {
return null;
}
- return value.booleanValue() ? JsonBooleanImpl.TRUE : JsonBooleanImpl.FALSE;
+ return new JsonBooleanImpl( value );
}
protected final JsonNumberImpl toJsonNumber( final Number value ) {
@@ -86,4 +86,8 @@ protected final JsonNumberImpl toJsonNumber( final Number value ) {
}
throw new IllegalStateException();
}
+
+ public JsonStructureImpl clone() {
+ throw new UnsupportedOperationException();
+ }
}
@@ -59,7 +59,7 @@ public JsonBooleanImpl newJsonBoolean( final Boolean value ) {
if ( value == null ) {
return null;
}
- return value ? JsonBooleanImpl.TRUE : JsonBooleanImpl.FALSE;
+ return new JsonBooleanImpl( value );
}
@Override
@@ -122,7 +122,7 @@ private JsonObjectImpl readJsonObjectFrom( final JsonReaderImpl jsonReader ) thr
if ( jsonEvent == NULL ) {
jsonValue = null;
} else if ( jsonEvent == BOOLEAN ) {
- jsonValue = jsonReader.getBoolean() ? JsonBooleanImpl.TRUE : JsonBooleanImpl.FALSE;
+ jsonValue = jsonReader.getBoolean() ? new JsonBooleanImpl( true ) : new JsonBooleanImpl( false );
} else if ( jsonEvent == NUMBER ) {
jsonValue = new JsonNumberImpl( jsonReader.getNumber() );
} else if ( jsonEvent == STRING ) {
@@ -148,7 +148,7 @@ private JsonArrayImpl readJsonArrayFrom( final JsonReaderImpl jsonReader ) throw
if ( jsonEvent == NULL ) {
jsonValue = null;
} else if ( jsonEvent == BOOLEAN ) {
- jsonValue = jsonReader.getBoolean() ? JsonBooleanImpl.TRUE : JsonBooleanImpl.FALSE;
+ jsonValue = jsonReader.getBoolean() ? new JsonBooleanImpl( true ) : new JsonBooleanImpl( false );
} else if ( jsonEvent == NUMBER ) {
jsonValue = new JsonNumberImpl( jsonReader.getNumber() );
} else if ( jsonEvent == STRING ) {

0 comments on commit ce0bca3

Please sign in to comment.