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