Propagate @Nonnull and @Nullable annotations
[yangtools.git] / yang / yang-data-util / src / main / java / org / opendaylight / yangtools / yang / data / util / AbstractModuleStringInstanceIdentifierCodec.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.util;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.Preconditions;
12
13 import javax.annotation.Nonnull;
14
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.model.api.Module;
17
18 /**
19  * Extension of {@link AbstractStringInstanceIdentifierCodec}, which instantiates
20  * QNames by first resolving the namespace and then looking the target namespace
21  * in the list of currently-subscribed modules.
22  */
23 @Beta
24 public abstract class AbstractModuleStringInstanceIdentifierCodec extends AbstractStringInstanceIdentifierCodec {
25     /**
26      * Resolve a string prefix into the corresponding module.
27      *
28      * @param prefix
29      * @return module mapped to prefix, or null if the module cannot be resolved
30      */
31     protected abstract Module moduleForPrefix(@Nonnull String prefix);
32
33     @Override
34     protected final QName createQName(@Nonnull final String prefix, @Nonnull final String localName) {
35         final Module module = moduleForPrefix(prefix);
36         Preconditions.checkArgument(module != null, "Failed to lookup prefix %s", prefix);
37         return QName.create(module.getQNameModule(), localName);
38     }
39 }