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 com.google.common.base.Preconditions;
11 import com.google.common.collect.Range;
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.List;
15 import javax.annotation.Nonnull;
16 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
20 abstract class AbstractPrimitiveRangeGenerator<T extends Number & Comparable<T>> extends AbstractRangeGenerator<T> {
21 private static final Logger LOG = LoggerFactory.getLogger(AbstractPrimitiveRangeGenerator.class);
22 private final String primitiveName;
23 private final T minValue;
24 private final T maxValue;
26 protected AbstractPrimitiveRangeGenerator(final Class<T> typeClass, final String primitiveName, final T minValue, final T maxValue) {
28 this.primitiveName = Preconditions.checkNotNull(primitiveName);
29 this.minValue = Preconditions.checkNotNull(minValue);
30 this.maxValue = Preconditions.checkNotNull(maxValue);
34 * Return the name of the primitive type, as known by the Java language.
36 * @return Primitive type name
38 @Nonnull protected final String getPrimitiveName() {
42 private boolean needsMaximumEnforcement(final T maxToEnforce) {
43 return maxValue.compareTo(maxToEnforce) > 0;
46 private boolean needsMinimumEnforcement(final T minToEnforce) {
47 return minValue.compareTo(minToEnforce) < 0;
50 private Collection<String> createExpressions(final Collection<RangeConstraint> constraints) {
51 final Collection<String> ret = new ArrayList<>(constraints.size());
53 for (RangeConstraint r : constraints) {
54 final T min = getValue(r.getMin());
55 final boolean needMin = needsMinimumEnforcement(min);
57 final T max = getValue(r.getMax());
58 final boolean needMax = needsMaximumEnforcement(max);
60 if (!needMin && !needMax) {
61 LOG.debug("Type {} indicates [{}, {}] does not require enforcement", getTypeName(), min, max);
65 final StringBuilder sb = new StringBuilder();
67 sb.append("value >= ").append(format(min));
73 sb.append("value <= ").append(format(max));
76 ret.add(sb.toString());
82 private String createRangeString(final Collection<RangeConstraint> constraints) {
83 final List<Range<T>> ranges = new ArrayList<>(constraints.size());
85 for (RangeConstraint c : constraints) {
86 ranges.add(Range.closed(getValue(c.getMin()), getValue(c.getMax())));
89 return ranges.toString();
93 protected final String generateRangeCheckerImplementation(final String checkerName, final Collection<RangeConstraint> constraints) {
94 final StringBuilder sb = new StringBuilder();
95 final Collection<String> expressions = createExpressions(constraints);
97 sb.append("private static void ").append(checkerName).append("(final ").append(primitiveName).append(" value) {\n");
99 if (!expressions.isEmpty()) {
100 for (String exp : expressions) {
101 sb.append(" if (").append(exp).append(") {\n");
102 sb.append(" return;\n");
106 sb.append(" throw new IllegalArgumentException(String.format(\"Invalid range: %s, expected: ")
107 .append(createRangeString(constraints)).append(".\", value));\n");
112 return sb.toString();