0d44cc4280ddef3f8fcd8499416c9e31bc5cdc4a
[mdsal.git] / binding2 / mdsal-binding2-dom-codec / src / main / java / org / opendaylight / mdsal / binding / javav2 / dom / codec / generator / spi / source / AbstractSource.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.mdsal.binding.javav2.dom.codec.generator.spi.source;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.collect.Iterators;
12 import com.google.common.collect.UnmodifiableIterator;
13 import java.util.Collections;
14 import java.util.HashSet;
15 import java.util.Set;
16 import org.opendaylight.mdsal.binding.javav2.dom.codec.generator.impl.StaticBindingProperty;
17 import org.opendaylight.mdsal.binding.javav2.model.api.Type;
18
19 /**
20  * Base class for preparing types and constants for writer.
21  *
22  */
23 @Beta
24 abstract class AbstractSource {
25
26     private final Set<StaticBindingProperty> staticConstants = new HashSet<>();
27
28     /**
29      * Create new static constant of specific type with value.
30      *
31      * @param name
32      *            - name of constant
33      * @param type
34      *            - specific type of constant
35      * @param value
36      *            - value of constant
37      * @param <T>
38      *            - type of constant
39      */
40     final <T> void staticConstant(final String name, final Class<T> type, final T value) {
41         this.staticConstants.add(new StaticBindingProperty(name, type, value));
42     }
43
44     /**
45      * Get set of static constants.
46      *
47      * @return unmodifiable view of set of static constants
48      */
49     public final Set<StaticBindingProperty> getStaticConstants() {
50         return Collections.unmodifiableSet(this.staticConstants);
51     }
52
53     /**
54      * Prepare common part of invoke of method on object.
55      *
56      * @param object
57      *            - object for invoke method
58      * @param methodName
59      *            - method name to be invoked
60      * @return base part of invoking method on object as String
61      */
62     private static StringBuilder prepareCommonInvokePart(final CharSequence object, final String methodName) {
63         final StringBuilder sb = new StringBuilder();
64         if (object != null) {
65             sb.append(object);
66             sb.append('.');
67         }
68         return sb.append(methodName).append('(');
69     }
70
71     /**
72      * Prepare invoking method on object with an argument.
73      *
74      * @param object
75      *            - object for invoke method
76      * @param methodName
77      *            - method name to be invoked
78      * @param arg
79      *            - argument of method
80      * @return invoking method on object with an argument as String
81      */
82     static final CharSequence invoke(final CharSequence object, final String methodName, final Object arg) {
83         return prepareCommonInvokePart(object, methodName).append(arg).append(')');
84     }
85
86     /**
87      * Prepare invoking method on object with more arguments.
88      *
89      * @param object
90      *            - object for invoke method
91      * @param methodName
92      *            - method name to be invoked
93      * @param args
94      *            - arguments of method
95      * @return invoking method on object with more arguments as String
96      */
97     static final CharSequence invoke(final CharSequence object, final String methodName,
98             final Object... args) {
99         final StringBuilder sb = prepareCommonInvokePart(object, methodName);
100
101         final UnmodifiableIterator<Object> iterator = Iterators.forArray(args);
102         while (iterator.hasNext()) {
103             sb.append(iterator.next());
104             if (iterator.hasNext()) {
105                 sb.append(',');
106             }
107         }
108         return sb.append(')');
109     }
110
111     /**
112      * Assign of value to variable.
113      *
114      * @param var
115      *            - name of variable
116      * @param value
117      *            - value of variable
118      * @return assigned value to variable as char sequence
119      */
120     static final CharSequence assign(final String var, final CharSequence value) {
121         return assign((String) null, var, value);
122     }
123
124     /**
125      * Assign of value to variable of specific type.
126      *
127      * @param type
128      *            - specific type of value
129      * @param var
130      *            - name of variable
131      * @param value
132      *            - value of variable
133      * @return assigned value to variable of specific type as char sequence, if
134      *         type is null then there is not added type to final string of
135      *         assigned value
136      */
137     static final CharSequence assign(final String type, final String var, final CharSequence value) {
138         final StringBuilder sb = new StringBuilder();
139         if (type != null) {
140             sb.append(type);
141             sb.append(' ');
142         }
143         return sb.append(var).append(" = ").append(value);
144     }
145
146     /**
147      * Assign of value to variable of specific type.
148      *
149      * @param type
150      *            - specific type of value
151      * @param var
152      *            - name of variable
153      * @param value
154      *            - value of variable
155      * @return assigned value to variable of specific type as char sequence, if
156      *         type is null then there is not added type to final string of
157      *         assigned value
158      */
159     static final CharSequence assign(final Type type, final String var, final CharSequence value) {
160         return assign(type.getFullyQualifiedName(), var, value);
161     }
162
163     /**
164      * Cast value to specific type.
165      *
166      * @param type
167      *            - specific type
168      * @param value
169      *            - value for cast
170      * @return casted value to specifc type as char sequence
171      */
172     static final CharSequence cast(final Type type, final CharSequence value) {
173         return cast(type.getFullyQualifiedName(), value);
174     }
175
176     /**
177      * Create loop through iterable object with specific body.
178      *
179      * @param iterable
180      *            - iterable object
181      * @param iteratorName
182      *            - name of iterator variable of iterable object
183      * @param valueType
184      *            - type of iterable item
185      * @param valueName
186      *            - name of variable of iterable item
187      * @param body
188      *            - specific body for porcess of iterable item
189      * @return loop through iterable object with specific body as String
190      */
191     static final CharSequence forEach(final String iterable, final String iteratorName,
192             final String valueType, final String valueName, final CharSequence body) {
193         final StringBuilder sb = new StringBuilder();
194         sb.append(statement(assign(java.util.Iterator.class.getName(), iteratorName, invoke(iterable, "iterator"))));
195         sb.append("while (").append(invoke(iteratorName, "hasNext")).append(") {\n");
196         sb.append(statement(assign(valueType, valueName, cast(valueType, invoke(iteratorName, "next")))));
197         sb.append(body);
198         return sb.append("\n}\n");
199     }
200
201     /**
202      * Cast value to specific type.
203      *
204      * @param type
205      *            - specific type
206      * @param value
207      *            - value for cast
208      * @return casted value to specifc type as char sequence
209      */
210     static final CharSequence cast(final String type, final CharSequence value) {
211         return "((" + type + ") " + value + ')';
212     }
213
214     /**
215      * Create new Java statement.
216      *
217      * @param statement
218      *            - input for creating new Java statement
219      * @return java statement
220      */
221     static final CharSequence statement(final CharSequence statement) {
222         return new StringBuilder(statement).append(";\n");
223     }
224 }