Populate data/ hierarchy
[yangtools.git] / data / 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.Nullable;
15 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
16 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafNodeBuilder;
17 import org.opendaylight.yangtools.yang.data.util.LeafInterner;
18 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
20
21 final class InterningLeafNodeBuilder<T> extends ImmutableLeafNodeBuilder<T> {
22     private final Interner<LeafNode<T>> interner;
23
24     private InterningLeafNodeBuilder(final Interner<LeafNode<T>> interner) {
25         this.interner = requireNonNull(interner);
26     }
27
28     static <T> @Nullable InterningLeafNodeBuilder<T> forSchema(final @Nullable DataSchemaNode schema) {
29         if (schema instanceof LeafSchemaNode) {
30             final Optional<Interner<LeafNode<T>>> interner = LeafInterner.forSchema((LeafSchemaNode)schema);
31             if (interner.isPresent()) {
32                 return new InterningLeafNodeBuilder<>(interner.get());
33             }
34         }
35         return null;
36     }
37
38     @Override
39     public LeafNode<T> build() {
40         return interner.intern(super.build());
41     }
42 }