Replaced all tabs with spaces in java and pom.xml
[so.git] / common / src / main / java / org / onap / so / client / graphinventory / entities / DSLQueryBuilder.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.client.graphinventory.entities;
22
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;
30
31
32 public class DSLQueryBuilder<S, E> implements QueryStep {
33
34     private List<QueryStep> steps = new ArrayList<>();
35     private String suffix = "";
36
37     public DSLQueryBuilder() {
38
39     }
40
41     public DSLQueryBuilder(DSLNode node) {
42         steps.add(node);
43     }
44
45     public DSLQueryBuilder<S, DSLNode> node(DSLNode node) {
46         steps.add(node);
47
48         return (DSLQueryBuilder<S, DSLNode>) this;
49     }
50
51     public DSLQueryBuilder<S, E> output() {
52         if (steps.get(steps.size() - 1) instanceof DSLNode) {
53             ((DSLNode) steps.get(steps.size() - 1)).output();
54         }
55         return this;
56     }
57
58     public <E2> DSLQueryBuilder<S, E2> union(final DSLQueryBuilder<?, E2>... union) {
59
60         List<DSLQueryBuilder<?, ?>> unions = Arrays.asList(union);
61         steps.add(() -> {
62             StringBuilder query = new StringBuilder();
63
64             query.append("> [ ").append(
65                     Joiner.on(", ").join(unions.stream().map(item -> item.build()).collect(Collectors.toList())))
66                     .append(" ]");
67             return query.toString();
68         });
69
70         return (DSLQueryBuilder<S, E2>) this;
71     }
72
73     public DSLQueryBuilder<S, E> where(DSLQueryBuilder<?, ?> where) {
74
75         steps.add(() -> {
76             StringBuilder query = new StringBuilder();
77             query.append(where.build()).append(")");
78             String result = query.toString();
79             if (!result.startsWith(">")) {
80                 result = "> " + result;
81             }
82             return "(" + result;
83         });
84         return this;
85     }
86
87     public DSLQueryBuilder<S, E> to(DSLQueryBuilder<?, ?> to) {
88         steps.add(() -> {
89             StringBuilder query = new StringBuilder();
90
91             query.append("> ").append(to.build());
92             return query.toString();
93         });
94         return this;
95     }
96
97     public DSLQueryBuilder<S, E> to(GraphInventoryObjectName name) {
98         return to(__.node(name));
99     }
100
101     public DSLQueryBuilder<S, E> to(GraphInventoryObjectName name, DSLNodeKey... key) {
102         return to(__.node(name, key));
103     }
104
105     public DSLQueryBuilder<S, E> limit(int limit) {
106         suffix = " LIMIT " + limit;
107         return this;
108     }
109
110     @Override
111     public String build() {
112         return compile();
113     }
114
115     @Override
116     public String toString() {
117         return build();
118     }
119
120     @Override
121     public boolean equals(Object o) {
122         if (o != null) {
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());
127             }
128         }
129         return false;
130     }
131
132     @Override
133     public int hashCode() {
134
135         return build().hashCode();
136     }
137
138     private String compile() {
139         return Joiner.on(" ").join(steps.stream().map(item -> item.build()).collect(Collectors.toList())) + suffix;
140     }
141
142     protected QueryStep getFirst() {
143         return steps.get(0);
144     }
145 }