2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.codec.gson;
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;
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;
25 * This class is implementation-internal and subject to change. Please do not use it.
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>() {
33 public Object deserialize(final String input) {
38 public String serialize(final Object input) {
43 public boolean needQuotes() {
48 private static TypeDefinition<?> resolveBaseTypeFrom(final TypeDefinition<?> type) {
49 TypeDefinition<?> superType = type;
50 while (superType.getBaseType() != null) {
51 superType = superType.getBaseType();
56 private final LoadingCache<TypeDefinition<?>, JSONCodec<Object>> codecs =
57 CacheBuilder.newBuilder().softValues().build(new CacheLoader<TypeDefinition<?>, JSONCodec<Object>>() {
58 @SuppressWarnings("unchecked")
60 public JSONCodec<Object> load(final TypeDefinition<?> key) throws Exception {
61 final TypeDefinition<?> type = resolveBaseTypeFrom(key);
63 if (type instanceof InstanceIdentifierType) {
64 return (JSONCodec<Object>) iidCodec;
66 if (type instanceof IdentityrefType) {
67 return (JSONCodec<Object>) idrefCodec;
69 if (type instanceof LeafrefTypeDefinition) {
70 return LEAFREF_DEFAULT_CODEC;
73 final TypeDefinitionAwareCodec<Object, ? extends TypeDefinition<?>> codec = TypeDefinitionAwareCodec.from(type);
75 LOG.debug("Codec for type \"{}\" is not implemented yet.", type.getQName().getLocalName());
79 return AbstractJSONCodec.create(codec);
83 private final JSONCodec<?> iidCodec;
84 private final JSONCodec<?> idrefCodec;
86 private CodecFactory(final SchemaContext context) {
87 iidCodec = new JSONStringInstanceIdentifierCodec(context);
88 idrefCodec = new JSONStringIdentityrefCodec(context);
91 public static CodecFactory create(final SchemaContext context) {
92 return new CodecFactory(context);
95 public final JSONCodec<Object> codecFor(final TypeDefinition<?> typeDefinition) {
96 return codecs.getUnchecked(typeDefinition);