Improve AbstractNamespaceCodec
[yangtools.git] / data / 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 org.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.yangtools.yang.common.QNameModule;
15 import org.opendaylight.yangtools.yang.common.XMLNamespace;
16 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
17 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextProvider;
18
19 /**
20  * Base class for implementing identityref codecs on based on module names.
21  */
22 @Beta
23 public abstract class ModuleStringIdentityrefCodec extends AbstractModuleStringIdentityrefCodec
24         implements EffectiveModelContextProvider {
25     private final @NonNull EffectiveModelContext context;
26     private final @NonNull QNameModule parentModule;
27
28     protected ModuleStringIdentityrefCodec(final @NonNull EffectiveModelContext context,
29             final @NonNull QNameModule parentModule) {
30         this.context = requireNonNull(context);
31         this.parentModule = requireNonNull(parentModule);
32     }
33
34     @Override
35     public final EffectiveModelContext getEffectiveModelContext() {
36         return context;
37     }
38
39     protected final QNameModule getParentModule() {
40         return parentModule;
41     }
42
43     @Override
44     protected String prefixForNamespace(final XMLNamespace namespace) {
45         final var modules = context.findModuleStatements(namespace).iterator();
46         return modules.hasNext() ? modules.next().argument().getLocalName() : null;
47     }
48 }