66d55237a567c813d31b1d77d7e3505731ac17b8
[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.data.util.AbstractModuleStringIdentityrefCodec;
15 import org.opendaylight.yangtools.yang.model.api.Module;
16 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
17
18 final class JSONStringIdentityrefCodec extends AbstractModuleStringIdentityrefCodec {
19     private final SchemaContext context;
20
21     JSONStringIdentityrefCodec(final SchemaContext context) {
22         this.context = Preconditions.checkNotNull(context);
23     }
24
25     @Override
26     protected Module moduleForPrefix(final String prefix) {
27         return context.findModuleByName(prefix, null);
28     }
29
30     @Override
31     protected String prefixForNamespace(final URI namespace) {
32         final Module module = context.findModuleByNamespaceAndRevision(namespace, null);
33         return module == null ? null : module.getName();
34     }
35
36 }