2 * Copyright (c) 2015 Cisco Systems, Inc. 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.java.api.generator;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.collect.ImmutableMap;
13 import com.google.common.collect.ImmutableMap.Builder;
14 import java.util.function.Function;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.mdsal.binding.model.api.ConcreteType;
17 import org.opendaylight.mdsal.binding.model.api.Type;
18 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
22 abstract class AbstractRangeGenerator<T extends Number & Comparable<T>> {
23 private static final Logger LOG = LoggerFactory.getLogger(AbstractRangeGenerator.class);
24 private static final ImmutableMap<String, AbstractRangeGenerator<?>> GENERATORS;
26 private static void addGenerator(final Builder<String, AbstractRangeGenerator<?>> builder,
27 final AbstractRangeGenerator<?> generator) {
28 builder.put(generator.getTypeClass().getCanonicalName(), generator);
32 final Builder<String, AbstractRangeGenerator<?>> b = ImmutableMap.<String, AbstractRangeGenerator<?>>builder();
33 addGenerator(b, new ByteRangeGenerator());
34 addGenerator(b, new ShortRangeGenerator());
35 addGenerator(b, new IntegerRangeGenerator());
36 addGenerator(b, new LongRangeGenerator());
37 addGenerator(b, new BigDecimalRangeGenerator());
38 addGenerator(b, new BigIntegerRangeGenerator());
39 GENERATORS = b.build();
42 private final Class<T> type;
44 protected AbstractRangeGenerator(final Class<T> typeClass) {
45 this.type = requireNonNull(typeClass);
48 static AbstractRangeGenerator<?> forType(final @NonNull Type type) {
49 final ConcreteType javaType = TypeUtils.getBaseYangType(type);
50 return GENERATORS.get(javaType.getFullyQualifiedName());
54 * Return the type's class.
56 * @return A class object
58 protected final @NonNull Class<T> getTypeClass() {
63 * Return the type's fully-qualified name.
65 * @return Fully-qualified name
67 protected final @NonNull String getTypeName() {
68 return type.getName();
72 * Return the value in the native type from a particular Number instance.
74 * @param value Value as a Number
75 * @return Value in native format.
77 protected final @NonNull T getValue(final Number value) {
78 if (type.isInstance(value)) {
79 return type.cast(value);
82 LOG.debug("Converting value {} from {} to {}", value, value.getClass(), type);
83 final T ret = convert(value);
85 // Check if the conversion lost any precision by performing conversion the other way around
86 final AbstractRangeGenerator<?> gen = GENERATORS.get(value.getClass().getName());
87 final Number check = gen.convert(ret);
88 if (!value.equals(check)) {
89 LOG.warn("Number class conversion from {} to {} truncated value {} to {}", value.getClass(), type, value,
96 // FIXME: Once BUG-3399 is fixed, we should never need this
98 protected abstract T convert(Number value);
101 * Format a value into a Java-compilable expression which results in the appropriate
104 * @param value Number value
105 * @return Java language string representation
107 protected abstract @NonNull String format(T value);
110 * Generate the checker method source code.
111 * @param checkerName Name of the checker method.
112 * @param constraints Restrictions which need to be applied.
113 * @return Method source code.
115 protected abstract @NonNull String generateRangeCheckerImplementation(@NonNull String checkerName,
116 @NonNull RangeConstraint<?> constraints, Function<Class<?>, String> classImporter);
118 private static String rangeCheckerName(final String member) {
119 return "check" + member + "Range";
122 String generateRangeChecker(final @NonNull String member, final @NonNull RangeConstraint<?> constraints,
123 final JavaFileTemplate template) {
124 return generateRangeCheckerImplementation(rangeCheckerName(member), constraints, template::importedName);
127 String generateRangeCheckerCall(final @NonNull String member, final @NonNull String valueReference) {
128 return rangeCheckerName(member) + '(' + valueReference + ");\n";