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.yang.data.impl.codec;
10 import static com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.annotations.Beta;
14 import com.google.common.base.CharMatcher;
15 import com.google.common.collect.RangeSet;
16 import java.util.Optional;
17 import java.util.regex.Pattern;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.yangtools.yang.common.ErrorType;
20 import org.opendaylight.yangtools.yang.common.Uint16;
21 import org.opendaylight.yangtools.yang.common.Uint32;
22 import org.opendaylight.yangtools.yang.common.Uint64;
23 import org.opendaylight.yangtools.yang.common.Uint8;
24 import org.opendaylight.yangtools.yang.data.api.codec.YangInvalidValueException;
25 import org.opendaylight.yangtools.yang.model.api.type.Int16TypeDefinition;
26 import org.opendaylight.yangtools.yang.model.api.type.Int32TypeDefinition;
27 import org.opendaylight.yangtools.yang.model.api.type.Int64TypeDefinition;
28 import org.opendaylight.yangtools.yang.model.api.type.Int8TypeDefinition;
29 import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
30 import org.opendaylight.yangtools.yang.model.api.type.RangeRestrictedTypeDefinition;
31 import org.opendaylight.yangtools.yang.model.api.type.Uint16TypeDefinition;
32 import org.opendaylight.yangtools.yang.model.api.type.Uint32TypeDefinition;
33 import org.opendaylight.yangtools.yang.model.api.type.Uint64TypeDefinition;
34 import org.opendaylight.yangtools.yang.model.api.type.Uint8TypeDefinition;
37 * Do not use this class outside of yangtools, its presence does not fall into the API stability contract.
40 public abstract class AbstractIntegerStringCodec<N extends Number & Comparable<N>,
41 T extends RangeRestrictedTypeDefinition<T, N>> extends TypeDefinitionAwareCodec<N, T> {
43 private static final Pattern INT_PATTERN = Pattern.compile("[+-]?[1-9][0-9]*$");
44 private static final Pattern HEX_PATTERN = Pattern.compile("[+-]?0[xX][0-9a-fA-F]+");
45 private static final Pattern OCT_PATTERN = Pattern.compile("[+-]?0[1-7][0-7]*$");
47 // For up to two characters, this is very fast
48 private static final CharMatcher X_MATCHER = CharMatcher.anyOf("xX");
50 private final RangeConstraint<N> rangeConstraint;
52 AbstractIntegerStringCodec(final T typeDefinition, final Optional<RangeConstraint<N>> constraint,
53 final Class<N> outputClass) {
54 super(requireNonNull(typeDefinition), outputClass);
55 rangeConstraint = constraint.orElse(null);
58 public static @NonNull AbstractIntegerStringCodec<Byte, Int8TypeDefinition> from(final Int8TypeDefinition type) {
59 return new Int8StringCodec(type);
62 public static @NonNull AbstractIntegerStringCodec<Short, Int16TypeDefinition> from(final Int16TypeDefinition type) {
63 return new Int16StringCodec(type);
66 public static @NonNull AbstractIntegerStringCodec<Integer, Int32TypeDefinition> from(
67 final Int32TypeDefinition type) {
68 return new Int32StringCodec(type);
71 public static @NonNull AbstractIntegerStringCodec<Long, Int64TypeDefinition> from(final Int64TypeDefinition type) {
72 return new Int64StringCodec(type);
75 public static @NonNull AbstractIntegerStringCodec<Uint8, Uint8TypeDefinition> from(final Uint8TypeDefinition type) {
76 return new Uint8StringCodec(type);
79 public static @NonNull AbstractIntegerStringCodec<Uint16, Uint16TypeDefinition> from(
80 final Uint16TypeDefinition type) {
81 return new Uint16StringCodec(type);
84 public static @NonNull AbstractIntegerStringCodec<Uint32, Uint32TypeDefinition> from(
85 final Uint32TypeDefinition type) {
86 return new Uint32StringCodec(type);
89 public static @NonNull AbstractIntegerStringCodec<Uint64, Uint64TypeDefinition> from(
90 final Uint64TypeDefinition type) {
91 return new Uint64StringCodec(type);
95 protected final N deserializeImpl(final String product) {
96 final int base = provideBase(product);
97 final String stringRepresentation = base != 16 ? product : X_MATCHER.removeFrom(product);
98 final N deserialized = verifyNotNull(deserialize(stringRepresentation, base));
99 if (rangeConstraint != null) {
100 final RangeSet<N> ranges = rangeConstraint.getAllowedRanges();
101 if (!ranges.contains(deserialized)) {
102 throw new YangInvalidValueException(ErrorType.APPLICATION, rangeConstraint,
103 "Value '" + deserialized + "' is not in required ranges " + ranges);
110 protected final String serializeImpl(final N input) {
111 return input.toString();
115 * Deserializes value from supplied string representation is supplied radix. See
116 * {@link Integer#parseInt(String, int)} for in-depth description about string and radix relationship.
118 * @param stringRepresentation String representation
119 * @param radix numeric base.
120 * @return Deserialized value.
122 protected abstract @NonNull N deserialize(@NonNull String stringRepresentation, int radix);
124 protected static <N extends Number & Comparable<N>> Optional<RangeConstraint<N>> extractRange(
125 final RangeRestrictedTypeDefinition<?, N> type) {
126 return type == null ? Optional.empty() : type.getRangeConstraint();
129 private static int provideBase(final String integer) {
130 if (integer.length() == 1 && integer.charAt(0) == '0' || INT_PATTERN.matcher(integer).matches()) {
132 } else if (HEX_PATTERN.matcher(integer).matches()) {
134 } else if (OCT_PATTERN.matcher(integer).matches()) {
137 throw new NumberFormatException("Incorrect lexical representation of integer value: " + integer + ".\n"
138 + "An integer value can be defined as:\n"
139 + " - a decimal number,\n"
140 + " - a hexadecimal number (prefix 0x)," + "%n - an octal number (prefix 0).\n"
141 + "Signed values are allowed. Spaces between digits are NOT allowed.");