public class OList extends ObjectList
An extension of LinkedList
, so all methods available to in that class are also available to this class.
Note that the use of this class is optional for generating JSON. The serializers will accept any objects that implement the
Collection
interface. But this class provides some useful additional functionality when working with JSON
models constructed from Java Collections Framework objects. For example, a constructor is provided for converting a
JSON array string directly into a List
. It also contains accessor methods for to avoid common typecasting
when accessing elements in a list.
This class is not thread safe.
Modifier and Type | Field and Description |
---|---|
static OList |
EMPTY_LIST
An empty read-only OList.
|
modCount
Constructor and Description |
---|
OList()
Construct an empty list.
|
OList(BeanSession session)
Construct an empty list with the specified bean context.
|
OList(CharSequence json)
Construct a list initialized with the specified JSON.
|
OList(CharSequence in,
Parser p)
Construct a list initialized with the specified string.
|
OList(Collection<?> copyFrom)
Construct a list initialized with the specified list.
|
OList(Object... entries)
Construct a list initialized with the contents.
|
OList(Reader json)
Construct a list initialized with the specified reader containing JSON.
|
OList(Reader in,
Parser p)
Construct a list initialized with the specified string.
|
Modifier and Type | Method and Description |
---|---|
OList |
a(Object... o)
Add.
|
OList |
a(Object o)
Add.
|
OList |
aa(Collection<?> c)
Add all.
|
OList |
aif(boolean b,
Object val)
Add if.
|
OList |
aif(Predicate<Object> p,
Object val)
Add if predicate matches.
|
OList |
aifne(String... o)
Add if not empty.
|
OList |
aifnn(Object... o)
Add if not null.
|
OList |
append(Object... o)
Add.
|
OList |
append(Object o)
Add.
|
OList |
appendAll(Collection<?> c)
Add all.
|
OList |
appendIf(boolean b,
Object val)
Add if.
|
OList |
appendIf(Predicate<Object> p,
Object val)
Add if predicate matches.
|
OList |
appendIfNotEmpty(String... o)
Add if not empty.
|
OList |
appendIfNotNull(Object... o)
Add if not null.
|
String |
asString()
Serialize this array to Simplified JSON.
|
String |
asString(WriterSerializer serializer)
Serialize this array to a string using the specified serializer.
|
Object |
cast(ClassMeta<?> cm)
Converts this object into the specified class type.
|
Object |
deleteAt(String path)
Similar to
remove(int) ,but the key is a slash-delimited path used to traverse entries in
this POJO. |
<E> Iterable<E> |
elements(Class<E> childType)
Creates an
Iterable with elements of the specified child type. |
<T> T |
get(int index,
Class<T> type)
Get the entry at the specified index, converted to the specified type.
|
<T> T |
get(int index,
Type type,
Type... args)
Get the entry at the specified index, converted to the specified type.
|
<T> T |
getAt(String path,
Class<T> type)
Same as
get(int,Class) , but the key is a slash-delimited path used to traverse entries in
this POJO. |
<T> T |
getAt(String path,
Type type,
Type... args)
Same as
getAt(String,Class) , but allows for conversion to complex maps and collections. |
BeanSession |
getBeanSession()
Returns the
BeanSession currently associated with this list. |
Boolean |
getBoolean(int index)
Shortcut for calling
get(index, Boolean. . |
ClassMeta<?> |
getClassMeta(int index)
Returns the
ClassMeta of the class of the object at the specified index. |
Integer |
getInt(int index)
Shortcut for calling
get(index, Integer. . |
OList |
getList(int index)
Shortcut for calling
get(index, OList. . |
Long |
getLong(int index)
Shortcut for calling
get(index, Long. . |
OMap |
getMap(int index)
Shortcut for calling
get(index, OMap. . |
String |
getString(int index)
Shortcut for calling
get(index, String. . |
boolean |
isUnmodifiable()
Returns
|
OList |
modifiable()
Returns a modifiable copy of this list if it's unmodifiable.
|
static OList |
of()
Construct an empty list.
|
static OList |
of(Object... entries)
Construct a list initialized with the specified key/value pairs.
|
static OList |
ofAll(Collection<?> in)
Construct a list initialized with the specified list.
|
static OList |
ofJson(CharSequence json)
Construct a list initialized with the specified JSON string.
|
static OList |
ofJson(Reader json)
Construct a list initialized with the specified reader containing JSON.
|
static OList |
ofText(CharSequence in,
Parser p)
Construct a list initialized with the specified string.
|
static OList |
ofText(Reader in,
Parser p)
Construct a list initialized with the specified string.
|
Object |
postAt(String path,
Object o)
Similar to
putAt(String,Object) , but used to append to collections and arrays. |
Object |
putAt(String path,
Object o)
Same as
set(int,Object) , but the key is a slash-delimited path used to traverse entries
in this POJO. |
OList |
session(BeanSession session)
Override the default bean session used for converting POJOs.
|
String |
toString()
Serialize this array to JSON using the
JsonSerializer.DEFAULT serializer. |
OList |
unmodifiable()
Returns an unmodifiable copy of this list if it's modifiable.
|
OList |
writeTo(Writer w)
Convenience method for serializing this OList to the specified Writer using the JsonSerializer.DEFAULT
serializer.
|
appendAll, getList, getMap, getObjectList, getObjectMap, parse, serializeTo, setBeanSession, toString
add, add, addAll, addAll, addFirst, addLast, clear, clone, contains, descendingIterator, element, get, getFirst, getLast, indexOf, lastIndexOf, listIterator, offer, offerFirst, offerLast, peek, peekFirst, peekLast, poll, pollFirst, pollLast, pop, push, remove, remove, remove, removeFirst, removeFirstOccurrence, removeLast, removeLastOccurrence, set, size, spliterator, toArray, toArray
iterator
equals, hashCode, listIterator, removeRange, subList
containsAll, isEmpty, removeAll, retainAll
finalize, getClass, notify, notifyAll, wait, wait, wait
containsAll, equals, hashCode, isEmpty, iterator, listIterator, removeAll, replaceAll, retainAll, sort, subList
parallelStream, removeIf, stream
public static final OList EMPTY_LIST
public OList()
public OList(BeanSession session)
session
- The bean session to use for creating beans.public OList(Collection<?> copyFrom)
copyFrom
- The list to copy.
public OList(CharSequence json) throws ParseException
json
- The JSON text to parse.
ParseException
- Malformed input encountered.public OList(CharSequence in, Parser p) throws ParseException
in
- The input being parsed.
p
- The parser to use to parse the input.
JsonParser
.ParseException
- Malformed input encountered.public OList(Reader json) throws ParseException
json
- The reader containing JSON text to parse.
ParseException
- Malformed input encountered.public OList(Reader in, Parser p) throws ParseException
in
- The reader containing the input being parsed.
p
- The parser to use to parse the input.
JsonParser
.ParseException
- Malformed input encountered.public static OList ofAll(Collection<?> in)
in
- The list to copy.
public static OList ofJson(CharSequence json) throws ParseException
json
- The JSON text to parse.
ParseException
- Malformed input encountered.public static OList ofText(CharSequence in, Parser p) throws ParseException
in
- The input being parsed.
p
- The parser to use to parse the input.
JsonParser
.ParseException
- Malformed input encountered.public static OList ofJson(Reader json) throws ParseException
json
- The reader containing JSON text to parse.
ParseException
- Malformed input encountered.public static OList ofText(Reader in, Parser p) throws ParseException
in
- The reader containing the input being parsed.
p
- The parser to use to parse the input.
JsonParser
.ParseException
- Malformed input encountered.public static OList of(Object... entries)
entries
- The entries to add to this list.public OList session(BeanSession session)
Default is BeanContext.DEFAULT
, which is sufficient in most cases.
Useful if you're serializing/parsing beans with transforms defined.
session
- The new bean session.public OList a(Object o)
Adds an entry to this list.
o
- The entry to add to this list.public OList append(Object o)
Same as a(Object)
o
- The entry to add to this list.public OList a(Object... o)
Adds multiple entries to this list.
o
- The entries to add to this list.public OList append(Object... o)
Same as a(Object...)
append
in class ObjectList
o
- The entries to add to this list.public OList aa(Collection<?> c)
Adds all the entries in the specified collection to this list.
c
- The collection to add to this list. Can be public OList appendAll(Collection<?> c)
Same as aa(Collection)
.
c
- The collection to add to this list. Can be public OList aif(boolean b, Object val)
Adds an entry to this list if the boolean flag is
b
- The boolean flag.val
- The value to add.public OList appendIf(boolean b, Object val)
Same as aif(boolean, Object)
.
b
- The boolean flag.val
- The value to add.public OList aifne(String... o)
Adds entries to this list skipping any empty or
o
- The objects to add to the list.public OList appendIfNotEmpty(String... o)
Same as aifne(String...)
.
appendIfNotEmpty
in class ObjectList
o
- The objects to add to the list.public OList aifnn(Object... o)
Adds entries to this list skipping
o
- The objects to add to the list.public OList appendIfNotNull(Object... o)
Same as aifnn(Object...)
.
appendIfNotNull
in class ObjectList
o
- The objects to add to the list.public OList aif(Predicate<Object> p, Object val)
p
- The predicate to match against.val
- The value to add if the predicate matches.public OList appendIf(Predicate<Object> p, Object val)
Same as aif(Predicate, Object)
.
p
- The predicate to match against.val
- The value to add if the predicate matches.public <T> T get(int index, Class<T> type)
This is the preferred get method for simple types.
OList l = OList.
See BeanSession.convertToType(Object, ClassMeta)
for the list of valid data conversions.
get
in class ObjectList
T
- The type of object to convert the entry to.index
- The index into this list.type
- The type of object to convert the entry to.public <T> T get(int index, Type type, Type... args)
The type can be a simple type (e.g. beans, strings, numbers) or parameterized type (collections/maps).
OList l = OList.
The array can be arbitrarily long to indicate arbitrarily complex data structures.
See BeanSession.convertToType(Object, ClassMeta)
for the list of valid data conversions.
get
in class ObjectList
T
- The type of object to convert the entry to.index
- The index into this list.type
- The type of object to convert the entry to.args
- The type arguments of the type to convert the entry to.public String getString(int index)
get(index, String.class )
.getString
in class ObjectList
index
- The index.public Integer getInt(int index)
get(index, Integer.class )
.getInt
in class ObjectList
index
- The index.InvalidDataConversionException
- If value cannot be converted.public Boolean getBoolean(int index)
get(index, Boolean.class )
.getBoolean
in class ObjectList
index
- The index.InvalidDataConversionException
- If value cannot be converted.public Long getLong(int index)
get(index, Long.class )
.getLong
in class ObjectList
index
- The index.InvalidDataConversionException
- If value cannot be converted.public OMap getMap(int index)
get(index, OMap.class )
.getMap
in class ObjectList
index
- The index.InvalidDataConversionException
- If value cannot be converted.public OList getList(int index)
get(index, OList.class )
.getList
in class ObjectList
index
- The index.InvalidDataConversionException
- If value cannot be converted.public <T> T getAt(String path, Class<T> type)
get(int,Class)
, but the key is a slash-delimited path used to traverse entries in
this POJO.
For example, the following code is equivalent:
OList ol = OList.
This method uses the PojoRest
class to perform the lookup, so the map can contain any of the various
class types that the PojoRest
class supports (e.g. beans, collections, arrays).
getAt
in class ObjectList
T
- The class type.path
- The path to the entry.type
- The class type.public <T> T getAt(String path, Type type, Type... args)
getAt(String,Class)
, but allows for conversion to complex maps and collections.getAt
in class ObjectList
T
- The class type.path
- The path to the entry.type
- The class type.args
- The class parameter types.public Object putAt(String path, Object o)
set(int,Object)
, but the key is a slash-delimited path used to traverse entries
in this POJO.
For example, the following code is equivalent:
OList ol = OList.
This method uses the PojoRest
class to perform the lookup, so the map can contain any of the various
class types that the PojoRest
class supports (e.g. beans, collections, arrays).
putAt
in class ObjectList
path
- The path to the entry.o
- The new value.public Object postAt(String path, Object o)
putAt(String,Object)
, but used to append to collections and arrays.
For example, the following code is equivalent:
OList ol = OList.
This method uses the PojoRest
class to perform the lookup, so the map can contain any of the various
class types that the PojoRest
class supports (e.g. beans, collections, arrays).
postAt
in class ObjectList
path
- The path to the entry.o
- The new value.public Object deleteAt(String path)
remove(int)
,but the key is a slash-delimited path used to traverse entries in
this POJO.
For example, the following code is equivalent:
OList ol = OList.
This method uses the PojoRest
class to perform the lookup, so the map can contain any of the various
class types that the PojoRest
class supports (e.g. beans, collections, arrays).
deleteAt
in class ObjectList
path
- The path to the entry.public BeanSession getBeanSession()
BeanSession
currently associated with this list.getBeanSession
in class ObjectList
BeanSession
currently associated with this list.public <E> Iterable<E> elements(Class<E> childType)
Iterable
with elements of the specified child type.
Attempts to convert the child objects to the correct type if they aren't already the correct type.
The InvalidDataConversionException
if
the next element cannot be converted to the specified type.
See BeanSession.convertToType(Object, ClassMeta)
for a description of valid conversions.
elements
in class ObjectList
E
- The child object type.childType
- The child object type.public ClassMeta<?> getClassMeta(int index)
ClassMeta
of the class of the object at the specified index.getClassMeta
in class ObjectList
index
- An index into this list, zero-based.public String asString(WriterSerializer serializer)
serializer
- The serializer to use to convert this object to a string.public String asString()
public boolean isUnmodifiable()
isUnmodifiable
in class ObjectList
public OList modifiable()
modifiable
in class ObjectList
public OList unmodifiable()
unmodifiable
in class ObjectList
public OList writeTo(Writer w) throws IOException, SerializeException
w
- The writer to send the serialized contents of this object.IOException
- If a problem occurred trying to write to the writer.SerializeException
- If a problem occurred trying to convert the output.public Object cast(ClassMeta<?> cm)
TODO - The current implementation is very inefficient.
cast
in class ObjectList
cm
- The class type to convert this object to.public String toString()
ObjectList
JsonSerializer.DEFAULT
serializer.toString
in class ObjectList
Copyright © 2016–2020 The Apache Software Foundation. All rights reserved.