Integrate aai-schema-ingest library into aai-core
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / serialization / queryformats / MultiFormatMapper.java
index 8ee52a1..944c14c 100644 (file)
-/**\r
- * ============LICENSE_START=======================================================\r
- * org.onap.aai\r
- * ================================================================================\r
- * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.\r
- * ================================================================================\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- *    http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- * ============LICENSE_END=========================================================\r
- */\r
-package org.onap.aai.serialization.queryformats;\r
-\r
-import java.util.Iterator;\r
-import java.util.List;\r
-\r
-import org.apache.tinkerpop.gremlin.process.traversal.Path;\r
-import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;\r
-import org.apache.tinkerpop.gremlin.structure.Vertex;\r
-import org.onap.aai.serialization.queryformats.exceptions.AAIFormatQueryResultFormatNotSupported;\r
-import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;\r
-\r
-import com.google.gson.JsonArray;\r
-import com.google.gson.JsonObject;\r
-\r
-public abstract class MultiFormatMapper implements FormatMapper {\r
-\r
-       @Override\r
-       public JsonObject formatObject(Object input) throws AAIFormatVertexException, AAIFormatQueryResultFormatNotSupported {\r
-               if (input instanceof Vertex) {\r
-                       return this.getJsonFromVertex((Vertex) input);\r
-               } else if (input instanceof Tree) {\r
-                       return this.getJsonFomTree((Tree<?>) input);\r
-               } else if (input instanceof Path) {\r
-                       return this.getJsonFromPath((Path) input);\r
-               } else {\r
-                       throw new AAIFormatQueryResultFormatNotSupported();\r
-               }\r
-       }\r
-\r
-       protected abstract JsonObject getJsonFromVertex(Vertex input) throws AAIFormatVertexException;\r
-\r
-       protected JsonObject getJsonFromPath(Path input) throws AAIFormatVertexException {\r
-               List<Object> path = input.objects();\r
-\r
-               JsonObject jo = new JsonObject();\r
-               JsonArray ja = new JsonArray();\r
-               \r
-               for (Object o : path) {\r
-                       if (o instanceof Vertex) {\r
-                               ja.add(this.getJsonFromVertex((Vertex)o));\r
-                       }\r
-               }\r
-               \r
-               jo.add("path", ja);\r
-               return jo;\r
-       }\r
-\r
-       protected JsonObject getJsonFomTree(Tree<?> tree) throws AAIFormatVertexException {\r
-               \r
-               if (tree.isEmpty()) {\r
-                       return new JsonObject();\r
-               }\r
-               \r
-               JsonObject t = new JsonObject();\r
-               JsonArray ja = this.getNodesArray(tree);\r
-               if (ja.size() > 0) {\r
-                       t.add("nodes", ja);\r
-               }\r
-               \r
-               return t;\r
-       }\r
-       \r
-       private JsonArray getNodesArray (Tree<?> tree) throws AAIFormatVertexException {\r
-               \r
-               JsonArray nodes = new JsonArray();\r
-               Iterator<?> it = tree.keySet().iterator();\r
-\r
-               while (it.hasNext()) {\r
-                       Object o = it.next();\r
-                       JsonObject me = new JsonObject();\r
-                       if (o instanceof Vertex) {\r
-                               me = this.getJsonFromVertex((Vertex) o);\r
-                       }\r
-                       JsonArray ja = this.getNodesArray((Tree<?>) tree.get(o));\r
-                       if (ja.size() > 0) {\r
-                               me.add("nodes", ja);\r
-                       }\r
-                       nodes.add(me);\r
-               }\r
-               return nodes;\r
-       }\r
-       \r
-       \r
-       @Override\r
-       public int parallelThreshold() {\r
-               return 100;\r
-       }\r
-\r
-}\r
+/**
+ * ============LICENSE_START=======================================================
+ * org.onap.aai
+ * ================================================================================
+ * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+package org.onap.aai.serialization.queryformats;
+
+import com.google.gson.JsonArray;
+import com.google.gson.JsonObject;
+import org.apache.tinkerpop.gremlin.process.traversal.Path;
+import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
+import org.apache.tinkerpop.gremlin.structure.Vertex;
+import org.onap.aai.serialization.queryformats.exceptions.AAIFormatQueryResultFormatNotSupported;
+import org.onap.aai.serialization.queryformats.exceptions.AAIFormatVertexException;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.Optional;
+
+public abstract class MultiFormatMapper implements FormatMapper {
+
+       @Override
+       public Optional<JsonObject> formatObject(Object input) throws AAIFormatVertexException, AAIFormatQueryResultFormatNotSupported {
+               if (input instanceof Vertex) {
+                       return this.getJsonFromVertex((Vertex) input);
+               } else if (input instanceof Tree) {
+                       return this.getJsonFomTree((Tree<?>) input);
+               } else if (input instanceof Path) {
+                       return this.getJsonFromPath((Path) input);
+               } else {
+                       throw new AAIFormatQueryResultFormatNotSupported();
+               }
+       }
+
+       protected abstract Optional<JsonObject> getJsonFromVertex(Vertex input) throws AAIFormatVertexException;
+
+       protected Optional<JsonObject> getJsonFromPath(Path input) throws AAIFormatVertexException {
+               List<Object> path = input.objects();
+
+               JsonObject jo = new JsonObject();
+               JsonArray ja = new JsonArray();
+               
+               for (Object o : path) {
+                       if (o instanceof Vertex) {
+                               ja.add(this.getJsonFromVertex((Vertex)o).get());
+                       }
+               }
+               
+               jo.add("path", ja);
+               return Optional.of(jo);
+       }
+
+       protected Optional<JsonObject> getJsonFomTree(Tree<?> tree) throws AAIFormatVertexException {
+               
+               if (tree.isEmpty()) {
+                       return Optional.of(new JsonObject());
+               }
+               
+               JsonObject t = new JsonObject();
+               JsonArray ja = this.getNodesArray(tree);
+               if (ja.size() > 0) {
+                       t.add("nodes", ja);
+               }
+               
+               return Optional.of(t);
+       }
+       
+       private JsonArray getNodesArray (Tree<?> tree) throws AAIFormatVertexException {
+               
+               JsonArray nodes = new JsonArray();
+               Iterator<?> it = tree.keySet().iterator();
+
+               while (it.hasNext()) {
+                       Object o = it.next();
+                       JsonObject me = new JsonObject();
+                       if (o instanceof Vertex) {
+                               me = this.getJsonFromVertex((Vertex) o).get();
+                       }
+                       JsonArray ja = this.getNodesArray((Tree<?>) tree.get(o));
+                       if (ja.size() > 0) {
+                               me.add("nodes", ja);
+                       }
+                       nodes.add(me);
+               }
+               return nodes;
+       }
+       
+       
+       @Override
+       public int parallelThreshold() {
+               return 100;
+       }
+
+}