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