Merge "Added Nonnull annotation for get operation"
[yangtools.git] / yang / yang-data-codec-gson / src / main / java / org / opendaylight / yangtools / yang / data / codec / gson / JSONStringIdentityrefCodec.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.base.Preconditions;
11
12 import java.net.URI;
13
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.data.util.AbstractModuleStringIdentityrefCodec;
16 import org.opendaylight.yangtools.yang.model.api.Module;
17 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
18
19 final class JSONStringIdentityrefCodec extends AbstractModuleStringIdentityrefCodec implements JSONCodec<QName> {
20     private final SchemaContext context;
21
22     JSONStringIdentityrefCodec(final SchemaContext context) {
23         this.context = Preconditions.checkNotNull(context);
24     }
25
26     @Override
27     protected Module moduleForPrefix(final String prefix) {
28         return context.findModuleByName(prefix, null);
29     }
30
31     @Override
32     protected String prefixForNamespace(final URI namespace) {
33         final Module module = context.findModuleByNamespaceAndRevision(namespace, null);
34         return module == null ? null : module.getName();
35     }
36
37     @Override
38     public boolean needQuotes() {
39         return true;
40     }
41 }