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.yangtools.sal.java.api.generator;
10 import java.util.Collection;
11 import javax.annotation.Nonnull;
12 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
14 abstract class AbstractBigRangeGenerator<T extends Number & Comparable<T>> extends AbstractRangeGenerator<T> {
15 protected AbstractBigRangeGenerator(final Class<T> typeClass) {
19 private String itemType() {
20 final StringBuilder sb = new StringBuilder("com.google.common.collect.Range<");
21 sb.append(getTypeName()).append('>');
26 private String arrayType() {
27 return new StringBuilder(itemType()).append("[]").toString();
31 protected final String generateRangeCheckerImplementation(final String checkerName, @Nonnull final Collection<RangeConstraint> restrictions) {
32 final StringBuilder sb = new StringBuilder();
34 // Field to hold the Range objects in an array
35 sb.append("private static final ").append(arrayType()).append(' ').append(checkerName).append(";\n");
37 // Static initializer block for the array
38 sb.append("static {\n");
39 sb.append(" @SuppressWarnings(\"unchecked\")\n");
40 sb.append(" final ").append(arrayType()).append(" a = (").append(arrayType())
41 .append(") java.lang.reflect.Array.newInstance(com.google.common.collect.Range.class, ").append(restrictions.size()).append(");\n");
44 for (RangeConstraint r : restrictions) {
45 final String min = format(getValue(r.getMin()));
46 final String max = format(getValue(r.getMax()));
48 sb.append(" a[").append(i++).append("] = com.google.common.collect.Range.closed(").append(min).append(", ").append(max).append(");\n");
51 sb.append(" ").append(checkerName).append(" = a;\n");
54 // Static enforcement method
55 sb.append("private static void ").append(checkerName).append("(final ").append(getTypeName()).append(" value) {\n");
56 sb.append(" for (").append(itemType()).append(" r : ").append(checkerName).append(") {\n");
57 sb.append(" if (r.contains(value)) {\n");
58 sb.append(" return;\n");
62 sb.append(" throw new IllegalArgumentException(String.format(\"Invalid value %s, expected: %s.\", value, java.util.Arrays.asList(").append(checkerName).append(")));\n");