2 * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.javav2.dom.codec.generator.spi.source;
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;
16 import org.opendaylight.mdsal.binding.javav2.dom.codec.generator.impl.StaticBindingProperty;
17 import org.opendaylight.mdsal.binding.javav2.model.api.Type;
20 * Base class for preparing types and constants for writer.
24 abstract class AbstractSource {
26 private final Set<StaticBindingProperty> staticConstants = new HashSet<>();
29 * Create new static constant of specific type with value.
34 * - specific type of constant
40 final <T> void staticConstant(final String name, final Class<T> type, final T value) {
41 this.staticConstants.add(new StaticBindingProperty(name, type, value));
45 * Get set of static constants.
47 * @return unmodifiable view of set of static constants
49 public final Set<StaticBindingProperty> getStaticConstants() {
50 return Collections.unmodifiableSet(this.staticConstants);
54 * Prepare common part of invoke of method on object.
57 * - object for invoke method
59 * - method name to be invoked
60 * @return base part of invoking method on object as String
62 private static StringBuilder prepareCommonInvokePart(final CharSequence object, final String methodName) {
63 final StringBuilder sb = new StringBuilder();
68 return sb.append(methodName).append('(');
72 * Prepare invoking method on object with an argument.
75 * - object for invoke method
77 * - method name to be invoked
79 * - argument of method
80 * @return invoking method on object with an argument as String
82 static final CharSequence invoke(final CharSequence object, final String methodName, final Object arg) {
83 return prepareCommonInvokePart(object, methodName).append(arg).append(')');
87 * Prepare invoking method on object with more arguments.
90 * - object for invoke method
92 * - method name to be invoked
94 * - arguments of method
95 * @return invoking method on object with more arguments as String
97 protected static final CharSequence invoke(final CharSequence object, final String methodName,
98 final Object... args) {
99 final StringBuilder sb = prepareCommonInvokePart(object, methodName);
101 final UnmodifiableIterator<Object> iterator = Iterators.forArray(args);
102 while (iterator.hasNext()) {
103 sb.append(iterator.next());
104 if (iterator.hasNext()) {
108 return sb.append(')');
112 * Assign of value to variable.
117 * - value of variable
118 * @return assigned value to variable as char sequence
120 static final CharSequence assign(final String var, final CharSequence value) {
121 return assign((String) null, var, value);
125 * Assign of value to variable of specific type.
128 * - specific type of 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
137 static final CharSequence assign(final String type, final String var, final CharSequence value) {
138 final StringBuilder sb = new StringBuilder();
143 return sb.append(var).append(" = ").append(value);
147 * Assign of value to variable of specific type.
150 * - specific type of 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
159 static final CharSequence assign(final Type type, final String var, final CharSequence value) {
160 return assign(type.getFullyQualifiedName(), var, value);
164 * Cast value to specific type.
170 * @return casted value to specifc type as char sequence
172 static final CharSequence cast(final Type type, final CharSequence value) {
173 return cast(type.getFullyQualifiedName(), value);
177 * Create loop through iterable object with specific body.
181 * @param iteratorName
182 * - name of iterator variable of iterable object
184 * - type of iterable item
186 * - name of variable of iterable item
188 * - specific body for porcess of iterable item
189 * @return loop through iterable object with specific body as String
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")))));
198 return sb.append("\n}\n");
202 * Cast value to specific type.
208 * @return casted value to specifc type as char sequence
210 static final CharSequence cast(final String type, final CharSequence value) {
211 return "((" + type + ") " + value + ')';
215 * Create new Java statement.
218 * - input for creating new Java statement
219 * @return java statement
221 static final CharSequence statement(final CharSequence statement) {
222 return new StringBuilder(statement).append(";\n");