Cleanup use of Guava library
[yangtools.git] / yang / yang-data-util / src / main / java / org / opendaylight / yangtools / yang / data / util / AbstractStringUnionCodec.java
1 /*
2  * Copyright (c) 2016 Intel Corporation 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
9 package org.opendaylight.yangtools.yang.data.util;
10
11 import static java.util.Objects.requireNonNull;
12
13 import org.opendaylight.yangtools.concepts.Codec;
14 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
15 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
16 import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 public abstract class AbstractStringUnionCodec implements Codec<String, Object> {
21     private static final Logger LOG = LoggerFactory.getLogger(AbstractStringUnionCodec.class);
22
23     protected final DataSchemaNode schema;
24     protected final UnionTypeDefinition typeDefinition;
25
26     protected AbstractStringUnionCodec(final DataSchemaNode schema, final UnionTypeDefinition typeDefinition) {
27         this.schema = requireNonNull(schema);
28         this.typeDefinition = requireNonNull(typeDefinition);
29     }
30
31     protected abstract Codec<String, Object> codecFor(TypeDefinition<?> type);
32
33     @Override
34     @SuppressWarnings("checkstyle:illegalCatch")
35     public final String serialize(final Object data) {
36         for (final TypeDefinition<?> type : typeDefinition.getTypes()) {
37             Codec<String, Object> codec = codecFor(type);
38             if (codec == null) {
39                 LOG.debug("no codec found for {}", type);
40                 continue;
41             }
42             try {
43                 return codec.serialize(data);
44             } catch (final Exception e) {
45                 LOG.debug("Data {} did not match for {}", data, type, e);
46                 // invalid - try the next union type.
47             }
48         }
49         throw new IllegalArgumentException("Invalid data \"" + data + "\" for union type.");
50     }
51
52     @Override
53     @SuppressWarnings("checkstyle:illegalCatch")
54     public Object deserialize(final String stringRepresentation) {
55         if (stringRepresentation == null) {
56             return null;
57         }
58
59         Object returnValue = null;
60         for (final TypeDefinition<?> type : typeDefinition.getTypes()) {
61             Codec<String, Object> codec = codecFor(type);
62             if (codec == null) {
63                 /*
64                  * This is a type for which we have no codec (eg identity ref) so we'll say it's
65                  * valid
66                  */
67                 returnValue = stringRepresentation;
68                 continue;
69             }
70             try {
71                 final Object deserialized = codec.deserialize(stringRepresentation);
72                 if (deserialized != null) {
73                     return deserialized;
74                 }
75                 returnValue = stringRepresentation;
76             } catch (final Exception e) {
77                 LOG.debug("Value {} did not matched representation for {}", stringRepresentation, type, e);
78                 // invalid - try the next union type.
79             }
80         }
81         if (returnValue != null) {
82             return returnValue;
83         }
84         throw new IllegalArgumentException("Invalid value \"" + stringRepresentation + "\" for union type.");
85     }
86 }