Rename binding-runtime-dynamic to binding-loader
[yangtools.git] / common / yang-common / src / main / java / org / opendaylight / yangtools / yang / common / YangDataName.java
1 /*
2  * Copyright (c) 2023 PANTHEON.tech, s.r.o. 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.common;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.collect.Interner;
14 import com.google.common.collect.Interners;
15 import java.io.DataInput;
16 import java.io.DataOutput;
17 import java.io.IOException;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.opendaylight.yangtools.concepts.Identifier;
20 import org.opendaylight.yangtools.concepts.WritableObject;
21
22 /**
23  * Identifier of a RESTCONF {@code yang-data} extension template instantiation.
24  *
25  * @param module namespace of defining module
26  * @param name template name
27  */
28 @NonNullByDefault
29 public record YangDataName(QNameModule module, String name) implements Identifier, WritableObject {
30     @java.io.Serial
31     private static final long serialVersionUID = 1L;
32     private static final Interner<YangDataName> INTERNER = Interners.newWeakInterner();
33
34     public YangDataName {
35         requireNonNull(module);
36         checkArgument(!name.isEmpty(), "name must not be empty");
37     }
38
39     /**
40      * Intern this instance.
41      *
42      * @return An interned instance.
43      */
44     public YangDataName intern() {
45         final var cacheMod = module.intern();
46
47         // Identity comparison is here on purpose, as we are deciding whether to potentially store 'module' into the
48         // interner. It is important that it does not hold user-supplied reference (such a String instance from
49         // parsing of an XML document).
50         final var template = cacheMod == module ? this : new YangDataName(cacheMod, name.intern());
51
52         return INTERNER.intern(template);
53     }
54
55     public static YangDataName readFrom(final DataInput in) throws IOException {
56         return new YangDataName(QNameModule.readFrom(in), in.readUTF());
57     }
58
59     @Override
60     public void writeTo(final DataOutput out) throws IOException {
61         module.writeTo(out);
62         out.writeUTF(name);
63     }
64 }