Cleanup use of Guava library
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / builder / impl / ImmutableOrderedLeafSetNodeBuilder.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. 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.builder.impl;
9
10 import com.google.common.collect.Iterables;
11 import java.util.Collection;
12 import java.util.Collections;
13 import java.util.LinkedHashMap;
14 import java.util.Map;
15 import java.util.Optional;
16 import org.opendaylight.yangtools.concepts.Immutable;
17 import org.opendaylight.yangtools.util.UnmodifiableCollection;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
22 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
24 import org.opendaylight.yangtools.yang.data.api.schema.OrderedLeafSetNode;
25 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.ListNodeBuilder;
26 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
27 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedNode;
28
29 public class ImmutableOrderedLeafSetNodeBuilder<T> implements ListNodeBuilder<T, LeafSetEntryNode<T>> {
30
31     private Map<NodeWithValue, LeafSetEntryNode<T>> value;
32     private NodeIdentifier nodeIdentifier;
33     private boolean dirty;
34
35     protected ImmutableOrderedLeafSetNodeBuilder() {
36         value = new LinkedHashMap<>();
37         dirty = false;
38     }
39
40     protected ImmutableOrderedLeafSetNodeBuilder(final ImmutableOrderedLeafSetNode<T> node) {
41         nodeIdentifier = node.getIdentifier();
42         value = node.getChildren();
43         dirty = true;
44     }
45
46     public static <T> ListNodeBuilder<T, LeafSetEntryNode<T>> create() {
47         return new ImmutableOrderedLeafSetNodeBuilder<>();
48     }
49
50     public static <T> ListNodeBuilder<T, LeafSetEntryNode<T>> create(final LeafSetNode<T> node) {
51         if (!(node instanceof ImmutableOrderedLeafSetNode<?>)) {
52             throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
53         }
54
55         return new ImmutableOrderedLeafSetNodeBuilder<>((ImmutableOrderedLeafSetNode<T>) node);
56     }
57
58     private void checkDirty() {
59         if (dirty) {
60             value = new LinkedHashMap<>(value);
61             dirty = false;
62         }
63     }
64
65     @Override
66     public ListNodeBuilder<T, LeafSetEntryNode<T>> withChild(final LeafSetEntryNode<T> child) {
67         checkDirty();
68         this.value.put(child.getIdentifier(), child);
69         return this;
70     }
71
72     @Override
73     public ListNodeBuilder<T, LeafSetEntryNode<T>> withoutChild(final PathArgument key) {
74         checkDirty();
75         this.value.remove(key);
76         return this;
77     }
78
79     @Override
80     public OrderedLeafSetNode<T> build() {
81         dirty = true;
82         return new ImmutableOrderedLeafSetNode<>(nodeIdentifier, value);
83     }
84
85     @Override
86     public ListNodeBuilder<T, LeafSetEntryNode<T>> withNodeIdentifier(final NodeIdentifier nodeIdentifier) {
87         this.nodeIdentifier = nodeIdentifier;
88         return this;
89     }
90
91     @Override
92     public ListNodeBuilder<T, LeafSetEntryNode<T>> withValue(final Collection<LeafSetEntryNode<T>> value) {
93         checkDirty();
94         for (final LeafSetEntryNode<T> leafSetEntry : value) {
95             withChild(leafSetEntry);
96         }
97         return this;
98     }
99
100     @Override
101     public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T value, final Map<QName, String> attributes) {
102         final ImmutableLeafSetEntryNodeBuilder<T> b = ImmutableLeafSetEntryNodeBuilder.create();
103         b.withNodeIdentifier(new NodeWithValue<>(nodeIdentifier.getNodeType(), value));
104         b.withValue(value);
105         b.withAttributes(attributes);
106         return withChild(b.build());
107     }
108
109     @Override
110     public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T value) {
111         return withChildValue(value, Collections.emptyMap());
112     }
113
114     protected static final class ImmutableOrderedLeafSetNode<T> extends
115             AbstractImmutableNormalizedNode<NodeIdentifier, Collection<LeafSetEntryNode<T>>> implements
116             Immutable, OrderedLeafSetNode<T> {
117
118         private final Map<NodeWithValue, LeafSetEntryNode<T>> children;
119
120         ImmutableOrderedLeafSetNode(final NodeIdentifier nodeIdentifier,
121                 final Map<NodeWithValue, LeafSetEntryNode<T>> children) {
122             super(nodeIdentifier);
123             this.children = children;
124         }
125
126         @Override
127         public Optional<LeafSetEntryNode<T>> getChild(final NodeWithValue child) {
128             return Optional.ofNullable(children.get(child));
129         }
130
131         @Override
132         public LeafSetEntryNode<T> getChild(final int position) {
133             return Iterables.get(children.values(), position);
134         }
135
136         @Override
137         protected int valueHashCode() {
138             return children.hashCode();
139         }
140
141         private Map<NodeWithValue, LeafSetEntryNode<T>> getChildren() {
142             return Collections.unmodifiableMap(children);
143         }
144
145         @Override
146         protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
147             return children.equals(((ImmutableOrderedLeafSetNode<?>) other).children);
148         }
149
150         @Override
151         public int getSize() {
152             return children.size();
153         }
154
155         @Override
156         public Collection<LeafSetEntryNode<T>> getValue() {
157             return UnmodifiableCollection.create(children.values());
158         }
159     }
160
161     @Override
162     public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>> addChild(
163             final LeafSetEntryNode<T> child) {
164         return withChild(child);
165     }
166
167     @Override
168     public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>>
169             removeChild(final PathArgument key) {
170         return withoutChild(key);
171     }
172 }