67eb342fabaf1ae5e31073d068bace381a555b13
[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.base.Optional;
11 import com.google.common.collect.Iterables;
12 import java.util.Collection;
13 import java.util.Collections;
14 import java.util.LinkedHashMap;
15 import java.util.Map;
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;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
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<YangInstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> value;
32     private YangInstanceIdentifier.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<T>((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(
87             final YangInstanceIdentifier.NodeIdentifier nodeIdentifier) {
88         this.nodeIdentifier = nodeIdentifier;
89         return this;
90     }
91
92     @Override
93     public ListNodeBuilder<T, LeafSetEntryNode<T>> withValue(final Collection<LeafSetEntryNode<T>> value) {
94         checkDirty();
95         for (final LeafSetEntryNode<T> leafSetEntry : value) {
96             withChild(leafSetEntry);
97         }
98         return this;
99     }
100
101     @Override
102     public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T value, final Map<QName, String> attributes) {
103         final ImmutableLeafSetEntryNodeBuilder<T> b = ImmutableLeafSetEntryNodeBuilder.create();
104         b.withNodeIdentifier(new YangInstanceIdentifier.NodeWithValue(nodeIdentifier.getNodeType(), value));
105         b.withValue(value);
106         b.withAttributes(attributes);
107         return withChild(b.build());
108     }
109
110     @Override
111     public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T value) {
112         return withChildValue(value, Collections.<QName,String>emptyMap());
113     }
114
115     protected static final class ImmutableOrderedLeafSetNode<T> extends
116             AbstractImmutableNormalizedNode<YangInstanceIdentifier.NodeIdentifier, Collection<LeafSetEntryNode<T>>> implements
117             Immutable, OrderedLeafSetNode<T> {
118
119         private final Map<YangInstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> children;
120
121         ImmutableOrderedLeafSetNode(final YangInstanceIdentifier.NodeIdentifier nodeIdentifier,
122                 final Map<YangInstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> children) {
123             super(nodeIdentifier);
124             this.children = children;
125         }
126
127         @Override
128         public Optional<LeafSetEntryNode<T>> getChild(final YangInstanceIdentifier.NodeWithValue child) {
129             return Optional.fromNullable(children.get(child));
130         }
131
132         @Override
133         protected int valueHashCode() {
134             return children.hashCode();
135         }
136
137         private Map<YangInstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> getChildren() {
138             return Collections.unmodifiableMap(children);
139         }
140
141         @Override
142         protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
143             return children.equals(((ImmutableOrderedLeafSetNode<?>) other).children);
144         }
145
146         @Override
147         public LeafSetEntryNode<T> getChild(final int position) {
148             return Iterables.get(children.values(), position);
149         }
150
151         @Override
152         public int getSize() {
153             return children.size();
154         }
155
156         @Override
157         public Collection<LeafSetEntryNode<T>> getValue() {
158             return UnmodifiableCollection.create(children.values());
159         }
160     }
161
162     @Override
163     public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>> addChild(
164             final LeafSetEntryNode<T> child) {
165         return withChild(child);
166     }
167
168     @Override
169     public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>> removeChild(
170             final PathArgument key) {
171         return withoutChild(key);
172     }
173
174 }