Permalink
Browse files

exhaustive JOM test cases

  • Loading branch information...
1 parent f5b9bef commit 93275196606ad243e55afed5d4e66554d50347b8 @ropalka ropalka committed Sep 20, 2012
@@ -22,6 +22,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
+import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
@@ -41,8 +42,11 @@
private final List< JsonValue > list;
+ private final List< JsonValue > userView;
+
JsonArrayImpl() {
list = new ArrayList< JsonValue >();
+ userView = Collections.unmodifiableList( list );
}
public boolean add( final String value ) {
@@ -108,14 +112,16 @@ public boolean contains( final Object value ) {
return contains( ( Number ) value );
} else if ( value instanceof Boolean ) {
return contains( ( Boolean ) value );
- } else if ( value instanceof JsonValue ) {
+ } else {
return contains( ( JsonValue ) value );
}
- return false;
}
public boolean containsAll( final Collection< ? > values ) {
- return list.containsAll( values );
+ for ( final Object o : values ) {
+ if ( !contains( o ) ) return false;
+ }
+ return true;
}
public int indexOf( final String value ) {
@@ -141,10 +147,9 @@ public int indexOf( final Object value ) {
return indexOf( ( Number ) value );
} else if ( value instanceof Boolean ) {
return indexOf( ( Boolean ) value );
- } else if ( value instanceof JsonValue ) {
+ } else {
return indexOf( ( JsonValue ) value );
}
- return -1;
}
public int lastIndexOf( final String value ) {
@@ -170,14 +175,13 @@ public int lastIndexOf( final Object value ) {
return lastIndexOf( ( Number ) value );
} else if ( value instanceof Boolean ) {
return lastIndexOf( ( Boolean ) value );
- } else if ( value instanceof JsonValue ) {
+ } else {
return lastIndexOf( ( JsonValue ) value );
}
- return -1;
}
public Iterator< JsonValue > iterator() {
- return list.iterator();
+ return userView.iterator();
}
public JsonValue get( final int index ) {
@@ -207,18 +211,17 @@ public boolean remove( final Object value ) {
return remove( ( Number ) value );
} else if ( value instanceof Boolean ) {
return remove( ( Boolean ) value );
- } else if ( value instanceof JsonValue ) {
+ } else {
return remove( ( JsonValue ) value );
}
- return false;
}
public JsonValue remove( final int index ) {
return list.remove( index );
}
public boolean removeAll( final Collection< ? > values ) {
- return list.removeAll( values );
+ return list.removeAll( toJsonValuesCollection( values ) );
}
public int size() {
@@ -250,11 +253,11 @@ public JsonValue set( final int index, final JsonValue value ) {
}
public ListIterator< JsonValue > listIterator() {
- return list.listIterator();
+ return userView.listIterator();
}
public ListIterator< JsonValue > listIterator( final int index ) {
- return list.listIterator( index );
+ return userView.listIterator( index );
}
public JsonValue[] toArray() {
@@ -266,11 +269,11 @@ public JsonValue set( final int index, final JsonValue value ) {
}
public boolean retainAll( final Collection< ? > values ) {
- return list.retainAll( values );
+ return list.retainAll( toJsonValuesCollection( values ) );
}
public List< JsonValue > subList( final int fromIndex, final int toIndex ) {
- return list.subList( fromIndex, toIndex );
+ return userView.subList( fromIndex, toIndex );
}
@Override
@@ -21,6 +21,7 @@
import java.io.IOException;
import java.util.Collection;
+import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
@@ -39,8 +40,11 @@
private final Map< String, JsonValue > map;
+ private final Map< String, JsonValue > userView;
+
JsonObjectImpl() {
map = new TreeMap< String, JsonValue >();
+ userView = Collections.unmodifiableMap( map );
}
public JsonValue put( final String key, final String value ) {
@@ -60,11 +64,11 @@ public JsonValue put( final String key, final JsonValue value ) {
}
public boolean containsKey( final Object key ) {
- return key instanceof String ? containsKey( ( String ) key ) : false;
+ return containsKey( ( String ) key );
}
public boolean containsKey( final String key ) {
- return key != null ? map.containsKey( key ) : false;
+ return map.containsKey( key );
}
public boolean containsValue( final Object value ) {
@@ -74,10 +78,9 @@ public boolean containsValue( final Object value ) {
return containsValue( ( Number ) value );
} else if ( value instanceof Boolean ) {
return containsValue( ( Boolean ) value );
- } else if ( value instanceof JsonValue ) {
+ } else {
return containsValue( ( JsonValue ) value );
}
- return false;
}
public boolean containsValue( final Boolean value ) {
@@ -97,43 +100,35 @@ public boolean containsValue( final JsonValue value ) {
}
public Collection< JsonValue > values() {
- return map.values();
+ return userView.values();
}
public Set< Entry< String, JsonValue >> entrySet() {
- return map.entrySet();
+ return userView.entrySet();
}
public Set< String > keySet() {
- return map.keySet();
+ return userView.keySet();
}
public JsonValue get( final String key ) {
return map.get( key );
}
public JsonValue get( final Object key ) {
- return key instanceof String ? get( ( String ) key ) : null;
+ return get( ( String ) key );
}
public JsonValue remove( final String key ) {
return map.remove( key );
}
public JsonValue remove( final Object key ) {
- return key instanceof String ? remove( ( String ) key ) : null;
- }
-
- public void putAll( final JsonObject jsonObject ) {
- if ( jsonObject != null ) {
- map.putAll( jsonObject );
- }
+ return remove( ( String ) key );
}
public void putAll( final Map< ? extends String, ? extends JsonValue > jsonObject ) {
- if ( jsonObject != null ) {
- map.putAll( jsonObject );
- }
+ map.putAll( jsonObject );
}
public void clear() {
@@ -22,6 +22,9 @@
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Iterator;
import org.fossnova.json.JsonValue;
import org.fossnova.json.stream.JsonWriter;
@@ -89,6 +92,26 @@ protected final JsonNumberImpl toJsonNumber( final Number value ) {
throw new IllegalStateException();
}
+ protected final Collection< JsonValue > toJsonValuesCollection( final Collection< ? > values ) {
+ if ( values == null ) return null;
+ final HashSet< JsonValue > jsonValues = new HashSet< JsonValue >();
+ final Iterator< ? > i = values.iterator();
+ Object value = null;
+ while ( i.hasNext() ) {
+ value = i.next();
+ if ( value instanceof String ) {
+ jsonValues.add( toJsonString( ( String ) value ) );
+ } else if ( value instanceof Number ) {
+ jsonValues.add( toJsonNumber( ( Number ) value ) );
+ } else if ( value instanceof Boolean ) {
+ jsonValues.add( toJsonBoolean( ( Boolean ) value ) );
+ } else {
+ jsonValues.add( ( JsonValue ) value );
+ }
+ }
+ return jsonValues;
+ }
+
@Override
public JsonStructureImpl clone() {
throw new UnsupportedOperationException();
@@ -59,17 +59,13 @@ public JsonArrayImpl newJsonArray() {
@Override
public JsonBooleanImpl newJsonBoolean( final Boolean value ) {
- if ( value == null ) {
- return null;
- }
+ assertNotNullParameter( value );
return new JsonBooleanImpl( value );
}
@Override
public JsonNumberImpl newJsonNumber( final Number value ) {
- if ( value == null ) {
- return null;
- }
+ assertNotNullParameter( value );
if ( value instanceof Byte ) {
return new JsonNumberImpl( ( Byte ) value );
} else if ( value instanceof Short ) {
@@ -92,26 +88,24 @@ public JsonNumberImpl newJsonNumber( final Number value ) {
@Override
public JsonStringImpl newJsonString( final String value ) {
- if ( value == null ) {
- return null;
- }
+ assertNotNullParameter( value );
return new JsonStringImpl( value );
}
@Override
public JsonValue readFrom( final JsonReader jsonReader ) throws IOException {
- if ( jsonReader == null ) {
- throw new IllegalArgumentException( "JSON reader cannot be null" );
- }
+ assertNotNullParameter( jsonReader );
return readFrom( ( JsonReaderImpl ) jsonReader );
}
private JsonValue readFrom( final JsonReaderImpl jsonReader ) throws IOException {
- if ( jsonReader.next() == OBJECT_START ) {
+ final JsonEvent jsonEvent = jsonReader.next();
+ if ( jsonEvent == OBJECT_START ) {
return readJsonObjectFrom( jsonReader );
- } else {
+ } else if ( jsonEvent == ARRAY_START ) {
return readJsonArrayFrom( jsonReader );
}
+ throw new IllegalStateException( "JSON reader have to point to array or object" );
}
private JsonObjectImpl readJsonObjectFrom( final JsonReaderImpl jsonReader ) throws IOException {
@@ -168,4 +162,10 @@ private JsonArrayImpl readJsonArrayFrom( final JsonReaderImpl jsonReader ) throw
}
return jsonArray;
}
+
+ private static void assertNotNullParameter( final Object o ) {
+ if ( o == null ) {
+ throw new IllegalArgumentException( "Parameter cannot be null" );
+ }
+ }
}
Oops, something went wrong.

0 comments on commit 9327519

Please sign in to comment.