2b85ee83fa0a6d184e87f6492562d7d3a42f86e7
[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 package org.opendaylight.yangtools.yang.data.util;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import java.net.URI;
14 import java.util.Iterator;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.yangtools.yang.common.QNameModule;
17 import org.opendaylight.yangtools.yang.model.api.Module;
18 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
19 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
20
21 /**
22  * Base class for implementing identityref codecs on based on module names.
23  */
24 @Beta
25 public abstract class ModuleStringIdentityrefCodec extends AbstractModuleStringIdentityrefCodec
26         implements SchemaContextProvider {
27     private final SchemaContext context;
28     private final QNameModule parentModule;
29
30     protected ModuleStringIdentityrefCodec(final @NonNull SchemaContext context,
31             final @NonNull QNameModule parentModule) {
32         this.context = requireNonNull(context);
33         this.parentModule = requireNonNull(parentModule);
34     }
35
36     @Override
37     public final SchemaContext getSchemaContext() {
38         return context;
39     }
40
41     protected final QNameModule getParentModule() {
42         return parentModule;
43     }
44
45     @Override
46     protected String prefixForNamespace(final URI namespace) {
47         final Iterator<? extends Module> modules = context.findModules(namespace).iterator();
48         return modules.hasNext() ? modules.next().getName() : null;
49     }
50 }