3b4bbfc0bb090ed3667d8e2209464936f5056750
[yangtools.git] / yang / yang-data-util / src / main / java / org / opendaylight / yangtools / yang / data / util / ModuleStringIdentityrefCodec.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 java.net.URI;
13 import javax.annotation.Nonnull;
14
15 import org.opendaylight.yangtools.yang.common.QNameModule;
16 import org.opendaylight.yangtools.yang.data.api.codec.IdentityrefCodec;
17 import org.opendaylight.yangtools.yang.model.api.Module;
18 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
19
20 public abstract class ModuleStringIdentityrefCodec
21         extends AbstractModuleStringIdentityrefCodec
22         implements IdentityrefCodec<String> {
23     protected final SchemaContext context;
24     protected final QNameModule parentModuleQname;
25
26     public ModuleStringIdentityrefCodec(@Nonnull final SchemaContext context, @Nonnull final QNameModule parentModule) {
27         this.context = Preconditions.checkNotNull(context);
28         this.parentModuleQname = Preconditions.checkNotNull(parentModule);
29     }
30
31     @Override
32     protected String prefixForNamespace(@Nonnull final URI namespace) {
33         final Module module = context.findModuleByNamespaceAndRevision(namespace, null);
34         return module == null ? null : module.getName();
35     }
36 }