2 * Copyright (c) 2023 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.common;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
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;
23 * Identifier of a RESTCONF {@code yang-data} extension template instantiation.
25 * @param module namespace of defining module
26 * @param name template name
29 public record YangDataName(QNameModule module, String name) implements Identifier, WritableObject {
31 private static final long serialVersionUID = 1L;
32 private static final Interner<YangDataName> INTERNER = Interners.newWeakInterner();
35 requireNonNull(module);
36 checkArgument(!name.isEmpty(), "name must not be empty");
40 * Intern this instance.
42 * @return An interned instance.
44 public YangDataName intern() {
45 final var cacheMod = module.intern();
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());
52 return INTERNER.intern(template);
55 public static YangDataName readFrom(final DataInput in) throws IOException {
56 return new YangDataName(QNameModule.readFrom(in), in.readUTF());
60 public void writeTo(final DataOutput out) throws IOException {