2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.so.client.graphinventory.entities;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.List;
26 import java.util.stream.Collectors;
28 import org.onap.so.client.aai.entities.QueryStep;
29 import org.onap.so.client.graphinventory.GraphInventoryObjectName;
31 import com.google.common.base.Joiner;
34 public class DSLQueryBuilder<S, E> implements QueryStep {
36 private List<QueryStep> steps = new ArrayList<>();
37 private String suffix = "";
39 public DSLQueryBuilder() {
42 public DSLQueryBuilder(DSLNode node) {
46 public DSLQueryBuilder<S, DSLNode> node(DSLNode node) {
49 return (DSLQueryBuilder<S, DSLNode>) this;
51 public DSLQueryBuilder<S, E> output() {
52 if (steps.get(steps.size() -1) instanceof DSLNode) {
53 ((DSLNode)steps.get(steps.size() -1)).output();
58 public <E2> DSLQueryBuilder<S, E2> union(final DSLQueryBuilder<?, E2>... union) {
60 List<DSLQueryBuilder<?, ?>> unions = Arrays.asList(union);
62 StringBuilder query = new StringBuilder();
64 query.append("> [ ").append(
66 unions.stream().map(item -> item.build()).collect(Collectors.toList())))
68 return query.toString();
71 return (DSLQueryBuilder<S, E2>) this;
74 public DSLQueryBuilder<S, E> where(DSLQueryBuilder<?, ?> where) {
77 StringBuilder query = new StringBuilder();
78 query.append(where.build()).append(")");
79 String result = query.toString();
80 if (!result.startsWith(">")) {
81 result = "> " + result;
88 public DSLQueryBuilder<S, E> to(DSLQueryBuilder<?, ?> to) {
90 StringBuilder query = new StringBuilder();
92 query.append("> ").append(to.build());
93 return query.toString();
98 public DSLQueryBuilder<S, E> to(GraphInventoryObjectName name) {
99 return to(__.node(name));
102 public DSLQueryBuilder<S, E> to(GraphInventoryObjectName name, DSLNodeKey... key) {
103 return to(__.node(name, key));
106 public DSLQueryBuilder<S, E> limit(int limit) {
107 suffix = " LIMIT " + limit;
112 public String build() {
117 public String toString() {
122 public boolean equals(Object o) {
124 if (o instanceof QueryStep) {
125 return ((QueryStep)o).build().equals(this.build());
126 } else if (o instanceof String) {
127 return o.equals(this.build());
134 public int hashCode() {
136 return build().hashCode();
139 private String compile() {
140 return Joiner.on(" ").join(steps.stream().map(item -> item.build()).collect(Collectors.toList())) + suffix;
143 protected QueryStep getFirst() {