Home
Decluttering JAVA 8 lamda programing and stream API
class Yourclass implements com.kinnarastudio.commons.Declutter {
public yourMethod() {
Collection<String> myStringCollection = ...
// turns this
myStringCollection.forEach(s -> {
try {
// some code that throws exceptions
} catch (Exception e) {
}
});
// to this
myStringCollection.forEach(Try.onConsumer(s -> {
// some code that throws exceptions
});
}
}
Try.onBiConsumer(<BiConsumer Callback>)
Try.onBiFunction(<BiFunction Callback>)
Try.onConsumer(<Consumer Callback>)
Try.onFunction(<Function Callback>)
Try.onPredicate(<Predicate Callback>)
Try.onSupplier(<Supplier Callback>)
Create a java.util.Stream
instance of json :
-
JSONStream.of(<object>, <valueExtractor>)
-
JSONStream.of(<array>, <valueExtractor>)
Stream<JSONObjectEntry<String>> jsonObjectStream = JSONStream.of(jsonObject, JSONObject::getString);
Stream<String> jsonArrayStream = JSONStream.of(jsonArray, JSONArray::getString);
Collect java.util.Stream
into json object or json array
// result = {"a":"A", "b":"B", "c":"C"}
JSONObject result = Stream.of("a", "b", "c")
.collect(JSONCollectors.toJSONObject(String::toLowerCase, String::toUpperCase));
// result = ["a", "b", "c"]
JSONArray result = Stream.of("a", "b", "c")
.collect(JSONCollectors.toJSONArray());
Combine json object object1
with object2
into json object result
.
<result> = JSONMapper.combine(<object1>, <object2>)
Concatenate json array array1
with array2
into json array result
.
<result> = JSONMapper.concat(<array1>, <array2>)
By using custom adapters, you can implement your own support for other json objects. To implement custom adapter
- Create adapter class
MyCustomAdapter
which implements either interfacecom.kinnarastudio.commons.jsonstream.adapter.ObjectAdapter
orcom.kinnarastudio.commons.jsonstream.adapter.ArrayAdapter
- Implements required methods.
- Use the adapter on your code
JSONStream.of(myObjectAdapter, jsonObject);
JSONStream.of(myArrayAdapter, jsonArray);
- Change architecture to use adapter
- Add support for
org.codehaus.jettison.json.*
- Add implementation of
Try.toNegate
- Remove implementation of
org.codehaus.jettison
- Support for package
org.codehaus.jettison
- Add
flatten
methods inJSONStream