Remove AttributesContainer
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / InterningLeafNodeBuilder.java
1 /*
2  * Copyright (c) 2019 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.data.impl.schema;
9
10 import static java.util.Objects.requireNonNull;
11
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;
21
22 final class InterningLeafNodeBuilder<T> extends ImmutableLeafNodeBuilder<T> {
23     private final Interner<LeafNode<T>> interner;
24
25     private InterningLeafNodeBuilder(final Interner<LeafNode<T>> interner) {
26         this.interner = requireNonNull(interner);
27     }
28
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());
34             }
35         }
36
37         return new ImmutableLeafNodeBuilder<>();
38     }
39
40     @Override
41     public LeafNode<T> build() {
42         return interner.intern(super.build());
43     }
44 }