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;
27 import org.onap.so.client.aai.entities.QueryStep;
28 import org.onap.so.client.graphinventory.GraphInventoryObjectName;
29 import com.google.common.base.Joiner;
32 public class DSLQueryBuilder<S, E> implements QueryStep {
34 private List<QueryStep> steps = new ArrayList<>();
35 private String suffix = "";
37 public DSLQueryBuilder() {
41 public DSLQueryBuilder(DSLNode node) {
45 public DSLQueryBuilder<S, DSLNode> node(DSLNode node) {
48 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(
65 Joiner.on(", ").join(unions.stream().map(item -> item.build()).collect(Collectors.toList())))
67 return query.toString();
70 return (DSLQueryBuilder<S, E2>) this;
73 public DSLQueryBuilder<S, E> where(DSLQueryBuilder<?, ?> where) {
76 StringBuilder query = new StringBuilder();
77 query.append(where.build()).append(")");
78 String result = query.toString();
79 if (!result.startsWith(">")) {
80 result = "> " + result;
87 public DSLQueryBuilder<S, E> to(DSLQueryBuilder<?, ?> to) {
89 StringBuilder query = new StringBuilder();
91 query.append("> ").append(to.build());
92 return query.toString();
97 public DSLQueryBuilder<S, E> to(GraphInventoryObjectName name) {
98 return to(__.node(name));
101 public DSLQueryBuilder<S, E> to(GraphInventoryObjectName name, DSLNodeKey... key) {
102 return to(__.node(name, key));
105 public DSLQueryBuilder<S, E> limit(int limit) {
106 suffix = " LIMIT " + limit;
111 public String build() {
116 public String toString() {
121 public boolean equals(Object o) {
123 if (o instanceof QueryStep) {
124 return ((QueryStep) o).build().equals(this.build());
125 } else if (o instanceof String) {
126 return o.equals(this.build());
133 public int hashCode() {
135 return build().hashCode();
138 private String compile() {
139 return Joiner.on(" ").join(steps.stream().map(item -> item.build()).collect(Collectors.toList())) + suffix;
142 protected QueryStep getFirst() {