BUG-1745, BUG-1746: remove needless quotes
[yangtools.git] / yang / yang-data-codec-gson / src / main / java / org / opendaylight / yangtools / yang / data / codec / gson / CodecFactory.java
1 /*
2  * Copyright (c) 2014 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.codec.gson;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.cache.CacheBuilder;
12 import com.google.common.cache.CacheLoader;
13 import com.google.common.cache.LoadingCache;
14
15 import org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec;
16 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
17 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
18 import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition;
19 import org.opendaylight.yangtools.yang.model.util.IdentityrefType;
20 import org.opendaylight.yangtools.yang.model.util.InstanceIdentifierType;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * This class is implementation-internal and subject to change. Please do not use it.
26  */
27 @Beta
28 final class CodecFactory {
29     private static final Logger LOG = LoggerFactory.getLogger(CodecFactory.class);
30     private static final JSONCodec<Object> LEAFREF_DEFAULT_CODEC = new JSONLeafrefCodec();
31     private static final JSONCodec<Object> NULL_CODEC = new JSONCodec<Object>() {
32         @Override
33         public Object deserialize(final String input) {
34             return null;
35         }
36
37         @Override
38         public String serialize(final Object input) {
39             return null;
40         }
41
42         @Override
43         public boolean needQuotes() {
44             return false;
45         }
46     };
47
48     private static TypeDefinition<?> resolveBaseTypeFrom(final TypeDefinition<?> type) {
49         TypeDefinition<?> superType = type;
50         while (superType.getBaseType() != null) {
51             superType = superType.getBaseType();
52         }
53         return superType;
54     }
55
56     private final LoadingCache<TypeDefinition<?>, JSONCodec<Object>> codecs =
57             CacheBuilder.newBuilder().softValues().build(new CacheLoader<TypeDefinition<?>, JSONCodec<Object>>() {
58         @SuppressWarnings("unchecked")
59         @Override
60         public JSONCodec<Object> load(final TypeDefinition<?> key) throws Exception {
61             final TypeDefinition<?> type = resolveBaseTypeFrom(key);
62
63             if (type instanceof InstanceIdentifierType) {
64                 return (JSONCodec<Object>) iidCodec;
65             }
66             if (type instanceof IdentityrefType) {
67                 return (JSONCodec<Object>) idrefCodec;
68             }
69             if (type instanceof LeafrefTypeDefinition) {
70                 return LEAFREF_DEFAULT_CODEC;
71             }
72
73             final TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> codec = TypeDefinitionAwareCodec.from(type);
74             if (codec == null) {
75                 LOG.debug("Codec for type \"{}\" is not implemented yet.", type.getQName().getLocalName());
76                 return NULL_CODEC;
77             }
78
79             return AbstractJSONCodec.create(codec);
80         }
81     });
82
83     private final JSONCodec<?> iidCodec;
84     private final JSONCodec<?> idrefCodec;
85
86     private CodecFactory(final SchemaContext context) {
87         iidCodec = new JSONStringInstanceIdentifierCodec(context);
88         idrefCodec = new JSONStringIdentityrefCodec(context);
89     }
90
91     public static CodecFactory create(final SchemaContext context) {
92         return new CodecFactory(context);
93     }
94
95     public final JSONCodec<Object> codecFor(final TypeDefinition<?> typeDefinition) {
96         return codecs.getUnchecked(typeDefinition);
97     }
98 }