2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.impl.schema.builder.impl;
10 import java.util.Collections;
11 import java.util.LinkedHashMap;
12 import java.util.List;
15 import org.opendaylight.yangtools.concepts.Immutable;
16 import org.opendaylight.yangtools.yang.common.QName;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.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;
27 import com.google.common.base.Optional;
28 import com.google.common.collect.Iterables;
30 public class ImmutableOrderedLeafSetNodeBuilder<T> implements ListNodeBuilder<T, LeafSetEntryNode<T>> {
32 private Map<YangInstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> value;
33 private YangInstanceIdentifier.NodeIdentifier nodeIdentifier;
34 private boolean dirty;
36 protected ImmutableOrderedLeafSetNodeBuilder() {
37 value = new LinkedHashMap<>();
41 protected ImmutableOrderedLeafSetNodeBuilder(final ImmutableOrderedLeafSetNode<T> node) {
42 nodeIdentifier = node.getIdentifier();
43 value = node.getChildren();
47 public static <T> ListNodeBuilder<T, LeafSetEntryNode<T>> create() {
48 return new ImmutableOrderedLeafSetNodeBuilder<>();
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()));
56 return new ImmutableOrderedLeafSetNodeBuilder<T>((ImmutableOrderedLeafSetNode<T>) node);
59 private void checkDirty() {
61 value = new LinkedHashMap<>(value);
67 public ListNodeBuilder<T, LeafSetEntryNode<T>> withChild(final LeafSetEntryNode<T> child) {
69 this.value.put(child.getIdentifier(), child);
74 public ListNodeBuilder<T, LeafSetEntryNode<T>> withoutChild(final PathArgument key) {
76 this.value.remove(key);
81 public OrderedLeafSetNode<T> build() {
83 return new ImmutableOrderedLeafSetNode<>(nodeIdentifier, value);
87 public ListNodeBuilder<T, LeafSetEntryNode<T>> withNodeIdentifier(
88 final YangInstanceIdentifier.NodeIdentifier nodeIdentifier) {
89 this.nodeIdentifier = nodeIdentifier;
94 public ListNodeBuilder<T, LeafSetEntryNode<T>> withValue(final List<LeafSetEntryNode<T>> value) {
96 for (final LeafSetEntryNode<T> leafSetEntry : value) {
97 withChild(leafSetEntry);
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 YangInstanceIdentifier.NodeWithValue(nodeIdentifier.getNodeType(), value));
108 b.withAttributes(attributes);
109 return withChild(b.build());
113 public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T value) {
114 return withChildValue(value, Collections.<QName,String>emptyMap());
117 protected final static class ImmutableOrderedLeafSetNode<T> extends
118 AbstractImmutableNormalizedNode<YangInstanceIdentifier.NodeIdentifier, Iterable<LeafSetEntryNode<T>>> implements
119 Immutable, OrderedLeafSetNode<T> {
121 private final Map<YangInstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> children;
123 ImmutableOrderedLeafSetNode(final YangInstanceIdentifier.NodeIdentifier nodeIdentifier,
124 final Map<YangInstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> children) {
125 super(nodeIdentifier);
126 this.children = children;
130 public Optional<LeafSetEntryNode<T>> getChild(final YangInstanceIdentifier.NodeWithValue child) {
131 return Optional.fromNullable(children.get(child));
135 protected int valueHashCode() {
136 return children.hashCode();
139 private Map<YangInstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> getChildren() {
140 return Collections.unmodifiableMap(children);
144 protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
145 return children.equals(((ImmutableOrderedLeafSetNode<?>) other).children);
149 public LeafSetEntryNode<T> getChild(final int position) {
150 return Iterables.get(getValue(), position);
154 public int getSize() {
155 // TODO Auto-generated method stub
160 public Iterable<LeafSetEntryNode<T>> getValue() {
161 return Iterables.unmodifiableIterable(children.values());
166 public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>> addChild(
167 final LeafSetEntryNode<T> child) {
168 return withChild(child);
172 public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>> removeChild(
173 final PathArgument key) {
174 return withoutChild(key);