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