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.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.impl.schema.builder.api.ListNodeBuilder;
23 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
24 import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedNode;
26 import com.google.common.base.Optional;
27 import com.google.common.collect.Iterables;
29 public class ImmutableLeafSetNodeBuilder<T> implements ListNodeBuilder<T, LeafSetEntryNode<T>> {
31 private Map<InstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> value;
32 private InstanceIdentifier.NodeIdentifier nodeIdentifier;
33 private boolean dirty;
35 protected ImmutableLeafSetNodeBuilder() {
36 value = new LinkedHashMap<>();
40 protected ImmutableLeafSetNodeBuilder(final ImmutableLeafSetNode<T> node) {
41 value = node.getChildren();
42 nodeIdentifier = node.getIdentifier();
46 public static <T> ListNodeBuilder<T, LeafSetEntryNode<T>> create() {
47 return new ImmutableLeafSetNodeBuilder<>();
50 public static <T> ListNodeBuilder<T, LeafSetEntryNode<T>> create(final LeafSetNode<T> node) {
51 if (!(node instanceof ImmutableLeafSetNode<?>)) {
52 throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass()));
55 return new ImmutableLeafSetNodeBuilder<T>((ImmutableLeafSetNode<T>) node);
58 private void checkDirty() {
60 value = new LinkedHashMap<>(value);
66 public ListNodeBuilder<T, LeafSetEntryNode<T>> withChild(final LeafSetEntryNode<T> child) {
68 this.value.put(child.getIdentifier(), child);
73 public ListNodeBuilder<T, LeafSetEntryNode<T>> withoutChild(final PathArgument key) {
75 this.value.remove(key);
80 public LeafSetNode<T> build() {
82 return new ImmutableLeafSetNode<>(nodeIdentifier, value);
86 public ListNodeBuilder<T, LeafSetEntryNode<T>> withNodeIdentifier(
87 final InstanceIdentifier.NodeIdentifier nodeIdentifier) {
88 this.nodeIdentifier = nodeIdentifier;
93 public ListNodeBuilder<T, LeafSetEntryNode<T>> withValue(final List<LeafSetEntryNode<T>> value) {
95 for (final LeafSetEntryNode<T> leafSetEntry : value) {
96 withChild(leafSetEntry);
103 public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T value, final Map<QName, String> attributes) {
104 final ImmutableLeafSetEntryNodeBuilder<T> b = ImmutableLeafSetEntryNodeBuilder.create();
105 b.withNodeIdentifier(new InstanceIdentifier.NodeWithValue(nodeIdentifier.getNodeType(), value));
107 b.withAttributes(attributes);
108 return withChild(b.build());
112 public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T value) {
113 return withChildValue(value, Collections.<QName,String>emptyMap());
116 protected final static class ImmutableLeafSetNode<T> extends
117 AbstractImmutableNormalizedNode<InstanceIdentifier.NodeIdentifier, Iterable<LeafSetEntryNode<T>>> implements
118 Immutable, LeafSetNode<T> {
120 private final Map<InstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> children;
122 ImmutableLeafSetNode(final InstanceIdentifier.NodeIdentifier nodeIdentifier,
123 final Map<InstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> children) {
124 super(nodeIdentifier, Iterables.unmodifiableIterable(children.values()));
125 this.children = children;
129 public Optional<LeafSetEntryNode<T>> getChild(final InstanceIdentifier.NodeWithValue child) {
130 return Optional.fromNullable(children.get(child));
134 protected int valueHashCode() {
135 return children.hashCode();
138 private Map<InstanceIdentifier.NodeWithValue, LeafSetEntryNode<T>> getChildren() {
139 return Collections.unmodifiableMap(children);
143 protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
144 return children.equals(((ImmutableLeafSetNode<?>) other).children);
149 public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>> addChild(
150 final LeafSetEntryNode<T> child) {
151 return withChild(child);
155 public NormalizedNodeContainerBuilder<NodeIdentifier, PathArgument, LeafSetEntryNode<T>, LeafSetNode<T>> removeChild(
156 final PathArgument key) {
157 return withoutChild(key);