summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/dcae/commonFunction/AnyNode.java
diff options
context:
space:
mode:
authorPawelSzalapski <pawel.szalapski@nokia.com>2018-06-26 15:16:41 +0200
committerPawelSzalapski <pawel.szalapski@nokia.com>2018-07-02 09:52:44 +0200
commitcd66181b35300f020f197bb411d6bdf6ad2514fb (patch)
treee4decd179d56ed2f64e5ab97c31c148cf2178aec /src/main/java/org/onap/dcae/commonFunction/AnyNode.java
parent943a47187dbb1393d720b2fdf0019d48270edb4d (diff)
Prepare codebase for dynamic DMaaP configuration
From now on, there is only one single place where we can create whole app core concerning sending events and it has a single entry point, based on DMaaP configuration. It can be used to rebuild part of app that is responsible for sending events dynamically. Changes are in scope for the dynamic DMaaP config feature. + bumped up code coverage a bit Change-Id: Iecc8c4e534ae9b781f47e3616409271ba83169c8 Signed-off-by: PawelSzalapski <pawel.szalapski@nokia.com> Issue-ID: DCAEGEN2-517
Diffstat (limited to 'src/main/java/org/onap/dcae/commonFunction/AnyNode.java')
-rw-r--r--src/main/java/org/onap/dcae/commonFunction/AnyNode.java157
1 files changed, 44 insertions, 113 deletions
diff --git a/src/main/java/org/onap/dcae/commonFunction/AnyNode.java b/src/main/java/org/onap/dcae/commonFunction/AnyNode.java
index 267c87a9..97d73ddd 100644
--- a/src/main/java/org/onap/dcae/commonFunction/AnyNode.java
+++ b/src/main/java/org/onap/dcae/commonFunction/AnyNode.java
@@ -19,164 +19,95 @@
*/
package org.onap.dcae.commonFunction;
+import static io.vavr.API.Set;
+
+import io.vavr.collection.List;
+import io.vavr.collection.Set;
+import io.vavr.control.Option;
+import java.util.stream.StreamSupport;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
-import org.json.JSONTokener;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.io.FileNotFoundException;
-import java.io.FileReader;
-import java.io.IOException;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Optional;
-import java.util.Set;
-import java.util.stream.Collectors;
-import java.util.stream.Stream;
-import java.util.stream.StreamSupport;
/**
- * This class is a wrapper for 2 most used entities of org.json lib: JSONArray and JSONObject and
- * comprises utility methods for fast access of json structures without need to explicitly coerce between them.
- * While using this, bear in mind it does not contain exception handling - it is assumed that when using, the parsed json structure is known.
+ * This class is a wrapper for 2 most used entities of org.json lib: JSONArray and JSONObject and comprises utility
+ * methods for fast access of json structures without need to explicitly coerce between them. While using this, bear in
+ * mind it does not contain exception handling - it is assumed that when using, the parsed json structure is known.
*
* @author koblosz
*/
public class AnyNode {
- private final Object obj;
- private static final Logger log = LoggerFactory.getLogger(AnyNode.class);
- public static AnyNode parse(String filePath) throws IOException {
- try (FileReader fr = new FileReader(filePath)) {
- return new AnyNode(new JSONObject(new JSONTokener(fr)));
- } catch (FileNotFoundException | JSONException e1) {
- log.error("Could not find or parse file under path %s due to: %s", filePath, e1.toString());
- e1.printStackTrace();
- throw e1;
- }
- }
+ private Object obj;
- /**
- * Returns key set of underlying object. It is assumed that underlying object is of type org.json.JSONObject.
- *
- * @return Set of string keys present in underlying JSONObject
- */
- public Set<String> getKeys() {
- return asJsonObject().keySet();
+ private AnyNode(Object object) {
+ this.obj = object;
}
- /**
- * Returns value associated with specified key wrapped with AnyValue object. It is assumed that this is of type org.json.JSONObject.
- *
- * @param key A key string
- * @return The AnyNode object associated with given key.
- */
- public AnyNode get(String key) {
- return new AnyNode(asJsonObject().get(key));
+ public static AnyNode fromString(String content) {
+ return new AnyNode(new JSONObject(content));
}
/**
- * Returns value under specified index wrapped with AnyValue object. It is assumed that this is of type org.json.JSONArray.
- *
- * @param idx An index of JSONArray
- * @return The AnyNode object associated with given index.
+ * Returns key set of underlying object. It is assumed that underlying object is of type org.json.JSONObject.
*/
- public AnyNode get(int idx) {
- return new AnyNode(asJsonArray().get(idx));
+ public Set<String> keys() {
+ return Set(asJsonObject().keySet().toArray(new String[]{}));
}
/**
- * Returns int assuming this can be coerced to int.
+ * Returns value associated with specified key wrapped with AnyValue object. It is assumed that this is of type
+ * org.json.JSONObject.
*/
- public int asInt() {
- return (int) this.obj;
+ public AnyNode get(String key) {
+ return new AnyNode(asJsonObject().get(key));
}
/**
- * Returns string representation of this. If it happens to have null, the value is treated as org.json.JSONObject.NULL and "null" string is returned then.
- *
- * @return A String
+ * Returns string representation of this. If it happens to have null, the value is treated as
+ * org.json.JSONObject.NULL and "null" string is returned then.
*/
- public String asString() {
- return this.obj != JSONObject.NULL ? (String) this.obj : JSONObject.NULL.toString();
- }
-
public String toString() {
return this.obj.toString();
}
/**
- * Converts underlying object to String-to-Object map. It is assumed that underlying object is of type org.json.JSONObject.
- *
- * @return A map.
+ * Returns optional of object under specified key, wrapped with AnyNode object.
+ * If underlying object is not of type org.json.JSONObject
+ * or underlying object has no given key
+ * or given key is null
+ * then Optional.empty will be returned.
*/
- public Map<String, Object> asRawMap() {
- return asJsonObject().toMap();
- }
-
- /**
- * Returns optional of object under specified key, wrapped with AnyNode object. If underlying object is not of type org.json.JSONObject, then Optional.empty will be returned.
- *
- * @param key A key string
- */
- public Optional<AnyNode> getAsOptional(String key) {
- AnyNode result = null;
+ public Option<AnyNode> getAsOption(String key) {
try {
- result = get(key);
- } catch (JSONException ignored) {
+ AnyNode value = get(key);
+ if (value.toString().equals("null")) {
+ return Option.none();
+ }
+ return Option.some(value);
+ } catch (JSONException ex) {
+ return Option.none();
}
- return Optional.ofNullable(result);
- }
-
- private JSONObject asJsonObject() {
- return (JSONObject) this.obj;
- }
-
- /**
- * Converts underlying object to map representation with map values wrapped with AnyNode object. It is assumed that underlying object is of type org.json.JSONObject.
- */
- public Map<String, AnyNode> asMap() {
- Map<String, AnyNode> map = new HashMap<>();
- getKeys().forEach(key -> map.put(key, get(key)));
- return map;
- }
-
- /**
- * Converts underlying object to map representation with map values wrapped with AnyNode object. It is assumed that underlying object is of type org.json.JSONObject.
- */
- public java.util.List<AnyNode> asList() {
- return asStream().collect(Collectors.toList());
}
/**
- * Converts this object to stream of underlying objects wrapped with AnyNode class. It is assumed that this is of type JSONArray.
+ * Converts underlying object to map representation with map values wrapped with AnyNode object. It is assumed that
+ * underlying object is of type org.json.JSONObject.
*/
- private Stream<AnyNode> asStream() {
- return StreamSupport.stream(((JSONArray) this.obj).spliterator(), false).map(AnyNode::new);
+ public List<AnyNode> toList() {
+ return List.ofAll(StreamSupport.stream(((JSONArray) this.obj).spliterator(), false).map(AnyNode::new));
}
/**
* Checks if specified key is present in this. It is assumed that this is of type JSONObject.
*/
- boolean hasKey(String key) {
- return getAsOptional(key).isPresent();
- }
-
- /**
- * Returns empty AnyNode (with null inside)
- */
- public static AnyNode nullValue() {
- return new AnyNode(JSONObject.NULL.toString());
+ public boolean has(String key) {
+ return !getAsOption(key).isEmpty();
}
- private JSONArray asJsonArray() {
- return (JSONArray) this.obj;
+ private JSONObject asJsonObject() {
+ return (JSONObject) this.obj;
}
- private AnyNode(Object object) {
- this.obj = object;
- }
}