Cleanup use of Guava library
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / codec / StringStringCodec.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.Preconditions.checkArgument;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.collect.ImmutableRangeSet;
14 import com.google.common.collect.Range;
15 import com.google.common.collect.RangeSet;
16 import com.google.common.collect.TreeRangeSet;
17 import java.util.Collection;
18 import java.util.Objects;
19 import java.util.Optional;
20 import org.opendaylight.yangtools.yang.data.api.codec.StringCodec;
21 import org.opendaylight.yangtools.yang.model.api.type.LengthConstraint;
22 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
23
24 /**
25  * Do not use this class outside of yangtools, its presence does not fall into the API stability contract.
26  */
27 @Beta
28 public class StringStringCodec extends TypeDefinitionAwareCodec<String, StringTypeDefinition>
29         implements StringCodec<String> {
30
31     private final RangeSet<Integer> lengths;
32
33     StringStringCodec(final StringTypeDefinition typeDef) {
34         super(Optional.of(typeDef), String.class);
35
36         final Collection<LengthConstraint> constraints = typeDef.getLengthConstraints();
37         if (!constraints.isEmpty()) {
38             final RangeSet<Integer> tmp = TreeRangeSet.create();
39             for (LengthConstraint c : constraints) {
40                 tmp.add(Range.closed(c.getMin().intValue(), c.getMax().intValue()));
41             }
42
43             lengths = ImmutableRangeSet.copyOf(tmp);
44         } else {
45             lengths = null;
46         }
47     }
48
49     public static StringStringCodec from(final StringTypeDefinition normalizedType) {
50         if (normalizedType.getPatternConstraints().isEmpty()) {
51             return new StringStringCodec(normalizedType);
52         }
53
54         return new StringPatternCheckingCodec(normalizedType);
55     }
56
57     @Override
58     public final String deserialize(final String stringRepresentation) {
59         if (stringRepresentation == null) {
60             // FIXME: These seems buggy, but someone may be using this behaviour
61             return "";
62         }
63         validate(stringRepresentation);
64         return stringRepresentation;
65     }
66
67     @Override
68     public final String serialize(final String data) {
69         return Objects.toString(data, "");
70     }
71
72     void validate(final String str) {
73         if (lengths != null) {
74             checkArgument(lengths.contains(str.length()), "String '%s' does not match allowed lengths %s", lengths);
75         }
76     }
77 }