2 * Copyright (c) 2017 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.javav2.java.api.generator.range_generators;
10 import com.google.common.collect.Range;
12 import javax.annotation.Nonnull;
13 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
16 * Abstract base for generators which require instantiation of boundary values to check.
17 * These are implemented by generating an array constant within the class, which contains
18 * {@link Range} instances, which hold pre-instantiated boundary values.
20 * @param <T> type of the class
22 abstract class AbstractBigRangeGenerator<T extends Number & Comparable<T>> extends AbstractRangeGenerator<T> {
23 private static final String RANGE = Range.class.getName();
25 protected AbstractBigRangeGenerator(final Class<T> typeClass) {
29 private StringBuilder itemType() {
30 return new StringBuilder(RANGE).append('<').append(getTypeName()).append('>');
33 private StringBuilder arrayType() {
34 return new StringBuilder(itemType()).append("[]");
38 protected final String generateRangeCheckerImplementation(final String checkerName,
39 @Nonnull final RangeConstraint<?> constraint) {
40 final Set<? extends Range<? extends Number>> constraints = constraint.getAllowedRanges().asRanges();
41 final String fieldName = checkerName.toUpperCase() + "_RANGES";
42 final StringBuilder sb = new StringBuilder();
44 // Field to hold the Range objects in an array
45 sb.append("private static final ").append(arrayType()).append(' ').append(fieldName).append(";\n");
47 // Static initializer block for the array
48 sb.append("static {\n");
49 sb.append(" @SuppressWarnings(\"unchecked\")\n");
50 sb.append(" final ").append(arrayType()).append(" a = (").append(arrayType())
51 .append(") java.lang.reflect.Array.newInstance(").append(RANGE).append(".class, ")
52 .append(constraints.size()).append(");\n");
55 for (Range<? extends Number> r : constraints) {
56 final String min = format(getValue(r.lowerEndpoint()));
57 final String max = format(getValue(r.upperEndpoint()));
58 sb.append(" a[").append(count++).append("] = ").append(RANGE).append(".closed(").append(min).append(", ")
59 .append(max).append(");\n");
62 sb.append(" ").append(fieldName).append(" = a;\n");
65 // Static enforcement method
66 sb.append("private static void ").append(checkerName).append("(final ").append(getTypeName())
67 .append(" value) {\n");
68 sb.append(" for (").append(itemType()).append(" r : ").append(fieldName).append(") {\n");
69 sb.append(" if (r.contains(value)) {\n");
70 sb.append(" return;\n");
73 sb.append(" throw new IllegalArgumentException(String.format(\"Invalid range: %s, expected: %s.\", "
74 + "value, java.util.Arrays.asList(").append(fieldName).append(")));\n");