28df15b1acb449916eba405ea76c4778b7a9b76a
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / codec / AbstractIntegerStringCodec.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.data.impl.codec;
9
10 import static com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
12
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.RpcError.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;
35
36 /**
37  * Do not use this class outside of yangtools, its presence does not fall into the API stability contract.
38  */
39 @Beta
40 public abstract class AbstractIntegerStringCodec<N extends Number & Comparable<N>,
41         T extends RangeRestrictedTypeDefinition<T, N>> extends TypeDefinitionAwareCodec<N, T> {
42
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]*$");
46
47     // For up to two characters, this is very fast
48     private static final CharMatcher X_MATCHER = CharMatcher.anyOf("xX");
49
50     private final RangeConstraint<N> rangeConstraint;
51
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);
56     }
57
58     public static @NonNull AbstractIntegerStringCodec<Byte, Int8TypeDefinition> from(final Int8TypeDefinition type) {
59         return new Int8StringCodec(type);
60     }
61
62     public static @NonNull AbstractIntegerStringCodec<Short, Int16TypeDefinition> from(final Int16TypeDefinition type) {
63         return new Int16StringCodec(type);
64     }
65
66     public static @NonNull AbstractIntegerStringCodec<Integer, Int32TypeDefinition> from(
67             final Int32TypeDefinition type) {
68         return new Int32StringCodec(type);
69     }
70
71     public static @NonNull AbstractIntegerStringCodec<Long, Int64TypeDefinition> from(final Int64TypeDefinition type) {
72         return new Int64StringCodec(type);
73     }
74
75     public static @NonNull AbstractIntegerStringCodec<Uint8, Uint8TypeDefinition> from(final Uint8TypeDefinition type) {
76         return new Uint8StringCodec(type);
77     }
78
79     public static @NonNull AbstractIntegerStringCodec<Uint16, Uint16TypeDefinition> from(
80             final Uint16TypeDefinition type) {
81         return new Uint16StringCodec(type);
82     }
83
84     public static @NonNull AbstractIntegerStringCodec<Uint32, Uint32TypeDefinition> from(
85             final Uint32TypeDefinition type) {
86         return new Uint32StringCodec(type);
87     }
88
89     public static @NonNull AbstractIntegerStringCodec<Uint64, Uint64TypeDefinition> from(
90             final Uint64TypeDefinition type) {
91         return new Uint64StringCodec(type);
92     }
93
94     @Override
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.PROTOCOL, rangeConstraint,
103                     "Value '" + deserialized + "'  is not in required ranges " + ranges);
104             }
105         }
106         return deserialized;
107     }
108
109     @Override
110     protected final String serializeImpl(final N input) {
111         return input.toString();
112     }
113
114     /**
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.
117      *
118      * @param stringRepresentation String representation
119      * @param radix numeric base.
120      * @return Deserialized value.
121      */
122     protected abstract @NonNull N deserialize(@NonNull String stringRepresentation, int radix);
123
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();
127     }
128
129     private static int provideBase(final String integer) {
130         if (integer.length() == 1 && integer.charAt(0) == '0') {
131             return 10;
132         } else if (INT_PATTERN.matcher(integer).matches()) {
133             return 10;
134         } else if (HEX_PATTERN.matcher(integer).matches()) {
135             return 16;
136         } else if (OCT_PATTERN.matcher(integer).matches()) {
137             return 8;
138         } else {
139             throw new NumberFormatException("Incorrect lexical representation of integer value: " + integer + ".\n"
140                         + "An integer value can be defined as:\n"
141                         + "  - a decimal number,\n"
142                         + "  - a hexadecimal number (prefix 0x)," + "%n  - an octal number (prefix 0).\n"
143                         + "Signed values are allowed. Spaces between digits are NOT allowed.");
144         }
145     }
146 }