Cleanup use of Guava library
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / codec / BitsStringCodec.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.base.Joiner;
14 import com.google.common.base.Splitter;
15 import com.google.common.collect.Collections2;
16 import com.google.common.collect.ImmutableSet;
17 import java.util.ArrayList;
18 import java.util.List;
19 import java.util.Optional;
20 import java.util.Set;
21 import org.opendaylight.yangtools.yang.data.api.codec.BitsCodec;
22 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
23 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
24
25 /**
26  * Do not use this class outside of yangtools, its presence does not fall into the API stability contract.
27  */
28 @Beta
29 public final class BitsStringCodec extends TypeDefinitionAwareCodec<Set<String>, BitsTypeDefinition>
30         implements BitsCodec<String> {
31
32     private static final Joiner JOINER = Joiner.on(" ").skipNulls();
33     private static final Splitter SPLITTER = Splitter.on(' ').omitEmptyStrings().trimResults();
34
35     private final Set<String> validBits;
36
37     @SuppressWarnings("unchecked")
38     private BitsStringCodec(final Optional<BitsTypeDefinition> typeDef) {
39         super(typeDef, (Class<Set<String>>) (Class<?>) Set.class);
40         if (typeDef.isPresent()) {
41             validBits = ImmutableSet.copyOf(Collections2.transform(typeDef.get().getBits(), Bit::getName));
42         } else {
43             validBits = null;
44         }
45     }
46
47     public static BitsStringCodec from(final BitsTypeDefinition type) {
48         return new BitsStringCodec(Optional.of(type));
49     }
50
51     @Override
52     public String serialize(final Set<String> data) {
53         return data == null ? "" : JOINER.join(data);
54     }
55
56     @Override
57     public Set<String> deserialize(final String stringRepresentation) {
58         if (stringRepresentation == null) {
59             return ImmutableSet.of();
60         }
61
62         final Set<String> strings = ImmutableSet.copyOf(SPLITTER.split(stringRepresentation));
63         if (validBits == null) {
64             // No schema available, use what we have
65             return strings;
66         }
67
68         // Normalize strings to schema first, retaining definition order
69         final List<String> sorted = new ArrayList<>(strings.size());
70         for (final String bit : validBits) {
71             if (strings.contains(bit)) {
72                 sorted.add(bit);
73             }
74         }
75
76         // Check sizes, if the normalized set does not match non-normalized size, non-normalized strings contain
77         // an invalid bit.
78         if (sorted.size() != strings.size()) {
79             for (final String bit : strings) {
80                 checkArgument(validBits.contains(bit), "Invalid value '%s' for bits type. Allowed values are: %s", bit,
81                     validBits);
82             }
83         }
84
85         // In case all valid bits have been specified, retain the set we have created for this codec
86         return sorted.size() == validBits.size() ? validBits : ImmutableSet.copyOf(sorted);
87     }
88 }