import com.google.common.base.CharMatcher;
import com.google.common.base.Optional;
+import com.google.common.collect.Range;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
+import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
abstract class AbstractIntegerStringCodec<N extends Number & Comparable<N>, T extends TypeDefinition<T>> extends TypeDefinitionAwareCodec<N, T>{
+ "\n - a hexadecimal number (prefix 0x)," + "%n - an octal number (prefix 0)."
+ "\nSigned values are allowed. Spaces between digits are NOT allowed.";
+
+ private final List<Range<N>> rangeConstraints;
+
+ protected AbstractIntegerStringCodec(final Optional<T> typeDefinition, final List<RangeConstraint> constraints , final Class<N> outputClass) {
+ super(typeDefinition, outputClass);
+ if(constraints.isEmpty()) {
+ rangeConstraints = Collections.emptyList();
+ } else {
+ final ArrayList<Range<N>> builder = new ArrayList<>(constraints.size());
+ for(final RangeConstraint yangConstraint : constraints) {
+ builder.add(createRange(yangConstraint.getMin(),yangConstraint.getMax()));
+ }
+ rangeConstraints = builder;
+ }
+
+ }
+
+ private Range<N> createRange(final Number yangMin, final Number yangMax) {
+ final N min = convertValue(yangMin);
+ final N max = convertValue(yangMax);
+ return Range.closed(min, max);
+ }
+
+ @Override
+ public final N deserialize(final String stringRepresentation) {
+ final int base = provideBase(stringRepresentation);
+ final N deserialized;
+ if (base == 16) {
+ deserialized = deserialize(normalizeHexadecimal(stringRepresentation),base);
+ } else {
+ deserialized = deserialize(stringRepresentation,base);
+ }
+ validate(deserialized);
+ return deserialized;
+ }
+
+
+ private final void validate(final N value) {
+ if (rangeConstraints.isEmpty()) {
+ return;
+ }
+ for (final Range<N> constraint : rangeConstraints) {
+ if (constraint.contains(value)) {
+ return;
+ }
+ }
+ // FIXME: Provide better error report.
+ throw new IllegalArgumentException("Value is not in required range.");
+ }
+
+ /**
+ * Deserializes value from supplied string representation
+ * is supplied radix.
+ *
+ * See {@link Integer#parseInt(String, int)} for in-depth
+ * description about string and radix relationship.
+ *
+ * @param stringRepresentation String representation
+ * @param radix numeric base.
+ * @return Deserialized value.
+ */
+ protected abstract N deserialize(String stringRepresentation, int radix);
+
+ protected abstract N convertValue(Number value);
+
+
+ protected static List<RangeConstraint> extractRange(final IntegerTypeDefinition type) {
+ if(type == null) {
+ return Collections.emptyList();
+ }
+ return type.getRangeConstraints();
+ }
+
+ protected static List<RangeConstraint> extractRange(final UnsignedIntegerTypeDefinition type) {
+ if(type == null) {
+ return Collections.emptyList();
+ }
+ return type.getRangeConstraints();
+ }
+
private static final int provideBase(final String integer) {
if (integer == null) {
throw new IllegalArgumentException("String representing integer number cannot be NULL");
return X_MATCHER.removeFrom(hexInt);
}
-
- protected AbstractIntegerStringCodec(final Optional<T> typeDefinition, final Class<N> outputClass) {
- super(typeDefinition, outputClass);
-
- }
-
- @Override
- public final N deserialize(final String stringRepresentation) {
- final int base = provideBase(stringRepresentation);
- if (base == 16) {
- return deserialize(normalizeHexadecimal(stringRepresentation),base);
- }
- return deserialize(stringRepresentation,base);
- }
-
- protected abstract N deserialize(String stringRepresentation, int base);
-
}
/*
- * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
+ * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
*
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
+ * This program and the accompanying materials are made available under the terms of the Eclipse
+ * Public License v1.0 which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
*/
package org.opendaylight.yangtools.yang.data.impl.codec;
import org.opendaylight.yangtools.yang.data.api.codec.Int16Codec;
import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition;
-class Int16StringCodec extends AbstractIntegerStringCodec<Short, IntegerTypeDefinition> implements
- Int16Codec<String> {
+class Int16StringCodec extends AbstractIntegerStringCodec<Short, IntegerTypeDefinition> implements Int16Codec<String> {
protected Int16StringCodec(final Optional<IntegerTypeDefinition> typeDef) {
- super(typeDef, Short.class);
+ super(typeDef, extractRange(typeDef.orNull()), Short.class);
}
+
+
@Override
public final Short deserialize(final String stringRepresentation, final int base) {
return Short.valueOf(stringRepresentation, base);
public final String serialize(final Short data) {
return data == null ? "" : data.toString();
}
-}
\ No newline at end of file
+
+ @Override
+ protected Short convertValue(final Number value) {
+ return value.shortValue();
+ }
+}
/*
- * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
+ * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
*
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
+ * This program and the accompanying materials are made available under the terms of the Eclipse
+ * Public License v1.0 which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
*/
package org.opendaylight.yangtools.yang.data.impl.codec;
import org.opendaylight.yangtools.yang.data.api.codec.Int32Codec;
import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition;
-class Int32StringCodec extends AbstractIntegerStringCodec<Integer, IntegerTypeDefinition> implements
- Int32Codec<String> {
+class Int32StringCodec extends AbstractIntegerStringCodec<Integer, IntegerTypeDefinition> implements Int32Codec<String> {
protected Int32StringCodec(final Optional<IntegerTypeDefinition> typeDef) {
- super(typeDef, Integer.class);
+ super(typeDef, extractRange(typeDef.orNull()), Integer.class);
}
@Override
public final String serialize(final Integer data) {
return data == null ? "" : data.toString();
}
-}
\ No newline at end of file
+
+ @Override
+ protected Integer convertValue(final Number value) {
+ return value.intValue();
+ }
+}
/*
- * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
+ * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
*
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
+ * This program and the accompanying materials are made available under the terms of the Eclipse
+ * Public License v1.0 which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
*/
package org.opendaylight.yangtools.yang.data.impl.codec;
import org.opendaylight.yangtools.yang.data.api.codec.Int64Codec;
import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition;
-class Int64StringCodec extends AbstractIntegerStringCodec<Long, IntegerTypeDefinition> implements
- Int64Codec<String> {
+class Int64StringCodec extends AbstractIntegerStringCodec<Long, IntegerTypeDefinition> implements Int64Codec<String> {
protected Int64StringCodec(final Optional<IntegerTypeDefinition> typeDef) {
- super(typeDef, Long.class);
+ super(typeDef, extractRange(typeDef.orNull()), Long.class);
}
@Override
public final String serialize(final Long data) {
return data == null ? "" : data.toString();
}
-}
\ No newline at end of file
+
+ @Override
+ protected Long convertValue(final Number value) {
+ return value.longValue();
+ }
+}
/*
- * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
+ * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
*
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
+ * This program and the accompanying materials are made available under the terms of the Eclipse
+ * Public License v1.0 which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
*/
package org.opendaylight.yangtools.yang.data.impl.codec;
import org.opendaylight.yangtools.yang.data.api.codec.Int8Codec;
import org.opendaylight.yangtools.yang.model.api.type.IntegerTypeDefinition;
-class Int8StringCodec extends AbstractIntegerStringCodec<Byte, IntegerTypeDefinition> implements
- Int8Codec<String> {
+class Int8StringCodec extends AbstractIntegerStringCodec<Byte, IntegerTypeDefinition> implements Int8Codec<String> {
protected Int8StringCodec(final Optional<IntegerTypeDefinition> typeDef) {
- super(typeDef, Byte.class);
+ super(typeDef, extractRange(typeDef.orNull()), Byte.class);
}
@Override
public String serialize(final Byte data) {
return data == null ? "" : data.toString();
}
-}
\ No newline at end of file
+
+ @Override
+ protected Byte convertValue(final Number value) {
+ return value.byteValue();
+ }
+}
/*
- * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
+ * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
*
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
+ * This program and the accompanying materials are made available under the terms of the Eclipse
+ * Public License v1.0 which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
*/
package org.opendaylight.yangtools.yang.data.impl.codec;
import org.opendaylight.yangtools.yang.data.api.codec.Uint16Codec;
import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
-class Uint16StringCodec extends AbstractIntegerStringCodec<Integer, UnsignedIntegerTypeDefinition>
- implements Uint16Codec<String> {
+class Uint16StringCodec extends AbstractIntegerStringCodec<Integer, UnsignedIntegerTypeDefinition> implements
+ Uint16Codec<String> {
protected Uint16StringCodec(final Optional<UnsignedIntegerTypeDefinition> typeDef) {
- super(typeDef, Integer.class);
+ super(typeDef, extractRange(typeDef.orNull()), Integer.class);
}
@Override
- public Integer deserialize(final String stringRepresentation, final int base) {
+ public final Integer deserialize(final String stringRepresentation, final int base) {
return Integer.valueOf(stringRepresentation, base);
}
@Override
- public String serialize(final Integer data) {
+ public final String serialize(final Integer data) {
return data == null ? "" : data.toString();
}
-}
\ No newline at end of file
+
+ @Override
+ protected Integer convertValue(final Number value) {
+ return value.intValue();
+ }
+}
/*
- * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
+ * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
*
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
+ * This program and the accompanying materials are made available under the terms of the Eclipse
+ * Public License v1.0 which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
*/
package org.opendaylight.yangtools.yang.data.impl.codec;
import org.opendaylight.yangtools.yang.data.api.codec.Uint32Codec;
import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
-class Uint32StringCodec extends AbstractIntegerStringCodec<Long, UnsignedIntegerTypeDefinition>
- implements Uint32Codec<String> {
+class Uint32StringCodec extends AbstractIntegerStringCodec<Long, UnsignedIntegerTypeDefinition> implements
+ Uint32Codec<String> {
protected Uint32StringCodec(final Optional<UnsignedIntegerTypeDefinition> typeDef) {
- super(typeDef, Long.class);
+ super(typeDef, extractRange(typeDef.orNull()), Long.class);
}
@Override
public final String serialize(final Long data) {
return data == null ? "" : data.toString();
}
-}
\ No newline at end of file
+
+ @Override
+ protected Long convertValue(final Number value) {
+ return value.longValue();
+ }
+}
/*
- * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
+ * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
*
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
+ * This program and the accompanying materials are made available under the terms of the Eclipse
+ * Public License v1.0 which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
*/
package org.opendaylight.yangtools.yang.data.impl.codec;
import org.opendaylight.yangtools.yang.data.api.codec.Uint64Codec;
import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
-class Uint64StringCodec extends AbstractIntegerStringCodec<BigInteger, UnsignedIntegerTypeDefinition> implements Uint64Codec<String> {
+class Uint64StringCodec extends AbstractIntegerStringCodec<BigInteger, UnsignedIntegerTypeDefinition> implements
+ Uint64Codec<String> {
protected Uint64StringCodec(final Optional<UnsignedIntegerTypeDefinition> typeDef) {
- super(typeDef, BigInteger.class);
+ super(typeDef, extractRange(typeDef.orNull()), BigInteger.class);
}
@Override
public final String serialize(final BigInteger data) {
return data == null ? "" : data.toString();
}
-}
\ No newline at end of file
+
+ @Override
+ protected BigInteger convertValue(final Number value) {
+ if (value instanceof BigInteger) {
+ return (BigInteger) value;
+ }
+ return BigInteger.valueOf(value.longValue());
+ }
+}
/*
- * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
+ * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
*
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
+ * This program and the accompanying materials are made available under the terms of the Eclipse
+ * Public License v1.0 which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
*/
package org.opendaylight.yangtools.yang.data.impl.codec;
import org.opendaylight.yangtools.yang.data.api.codec.Uint8Codec;
import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
-class Uint8StringCodec extends AbstractIntegerStringCodec<Short, UnsignedIntegerTypeDefinition>
- implements Uint8Codec<String> {
+class Uint8StringCodec extends AbstractIntegerStringCodec<Short, UnsignedIntegerTypeDefinition> implements
+ Uint8Codec<String> {
protected Uint8StringCodec(final Optional<UnsignedIntegerTypeDefinition> typeDef) {
- super(typeDef, Short.class);
+ super(typeDef, extractRange(typeDef.orNull()), Short.class);
}
@Override
}
@Override
- public final Short deserialize(final String stringRepresentation,final int base) {
+ public final Short deserialize(final String stringRepresentation, final int base) {
return Short.valueOf(stringRepresentation, base);
}
-}
\ No newline at end of file
+
+ @Override
+ protected Short convertValue(final Number value) {
+ return value.shortValue();
+ }
+}