222a52897e916fa75efae115cced24e6c7d84f50
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / codec / Uint64StringCodec.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 terms of the Eclipse
5  * Public License v1.0 which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.data.impl.codec;
9
10 import com.google.common.base.Optional;
11 import java.math.BigInteger;
12 import org.opendaylight.yangtools.yang.data.api.codec.Uint64Codec;
13 import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
14
15 class Uint64StringCodec extends AbstractIntegerStringCodec<BigInteger, UnsignedIntegerTypeDefinition> implements
16         Uint64Codec<String> {
17
18     protected Uint64StringCodec(final Optional<UnsignedIntegerTypeDefinition> typeDef) {
19         super(typeDef, extractRange(typeDef.orNull()), BigInteger.class);
20     }
21
22     @Override
23     public final BigInteger deserialize(final String stringRepresentation, final int base) {
24         return new BigInteger(stringRepresentation, base);
25     }
26
27     @Override
28     public final String serialize(final BigInteger data) {
29         return data == null ? "" : data.toString();
30     }
31
32     @Override
33     protected BigInteger convertValue(final Number value) {
34         if (value instanceof BigInteger) {
35             return (BigInteger) value;
36         }
37         return BigInteger.valueOf(value.longValue());
38     }
39 }