2 * Copyright (c) 2019 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.data.impl.schema;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.collect.Interner;
13 import java.util.Optional;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.eclipse.jdt.annotation.Nullable;
16 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
17 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
18 import org.opendaylight.yangtools.yang.data.util.LeafInterner;
19 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
20 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
22 final class InterningLeafNodeBuilder<T> extends ImmutableLeafNodeBuilder<T> {
23 private final Interner<LeafNode<T>> interner;
25 private InterningLeafNodeBuilder(final Interner<LeafNode<T>> interner) {
26 this.interner = requireNonNull(interner);
29 static <T> @NonNull ImmutableLeafNodeBuilder<T> forSchema(final @Nullable DataSchemaNode schema) {
30 if (schema instanceof LeafSchemaNode) {
31 final Optional<Interner<LeafNode<T>>> interner = LeafInterner.forSchema((LeafSchemaNode)schema);
32 if (interner.isPresent()) {
33 return new InterningLeafNodeBuilder<>(interner.get());
37 return new ImmutableLeafNodeBuilder<>();
41 public LeafNode<T> build() {
42 return interner.intern(super.build());