/* * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.yangtools.yang.data.impl.schema.builder.impl; import com.google.common.collect.Iterables; import java.util.Collection; import java.util.Collections; import java.util.LinkedHashMap; import java.util.Map; import org.eclipse.jdt.annotation.NonNull; import org.opendaylight.yangtools.util.UnmodifiableCollection; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument; import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode; import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode; import org.opendaylight.yangtools.yang.data.api.schema.UserLeafSetNode; import org.opendaylight.yangtools.yang.data.api.schema.builder.ListNodeBuilder; import org.opendaylight.yangtools.yang.data.spi.node.AbstractNormalizedNode; import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode; public class ImmutableUserLeafSetNodeBuilder implements ListNodeBuilder> { private Map> value; private NodeIdentifier nodeIdentifier; private boolean dirty; ImmutableUserLeafSetNodeBuilder() { value = new LinkedHashMap<>(); dirty = false; } ImmutableUserLeafSetNodeBuilder(final ImmutableUserLeafSetNode node) { nodeIdentifier = node.getIdentifier(); value = node.getChildren(); dirty = true; } public static @NonNull ListNodeBuilder> create() { return new ImmutableUserLeafSetNodeBuilder<>(); } public static @NonNull ListNodeBuilder> create( final UserLeafSetNode node) { if (!(node instanceof ImmutableUserLeafSetNode)) { throw new UnsupportedOperationException(String.format("Cannot initialize from class %s", node.getClass())); } return new ImmutableUserLeafSetNodeBuilder<>((ImmutableUserLeafSetNode) node); } @Deprecated(since = "6.0.7", forRemoval = true) public static @NonNull ListNodeBuilder> create(final LeafListSchemaNode schema) { return new SchemaAwareImmutableOrderedLeafSetNodeBuilder<>(schema); } @Deprecated(since = "6.0.7", forRemoval = true) public static @NonNull ListNodeBuilder> create(final LeafListSchemaNode schema, final LeafSetNode node) { if (node instanceof ImmutableUserLeafSetNode) { return new SchemaAwareImmutableOrderedLeafSetNodeBuilder<>(schema, (ImmutableUserLeafSetNode) node); } throw new UnsupportedOperationException("Cannot initialize from class " + node.getClass()); } private void checkDirty() { if (dirty) { value = new LinkedHashMap<>(value); dirty = false; } } @Override public ImmutableUserLeafSetNodeBuilder withChild(final LeafSetEntryNode child) { checkDirty(); this.value.put(child.getIdentifier(), child); return this; } @Override public ImmutableUserLeafSetNodeBuilder withoutChild(final PathArgument key) { checkDirty(); this.value.remove(key); return this; } @Override public UserLeafSetNode build() { dirty = true; return new ImmutableUserLeafSetNode<>(nodeIdentifier, value); } @Override public ImmutableUserLeafSetNodeBuilder withNodeIdentifier(final NodeIdentifier withNodeIdentifier) { this.nodeIdentifier = withNodeIdentifier; return this; } @Override public ImmutableUserLeafSetNodeBuilder withValue(final Collection> withValue) { checkDirty(); for (final LeafSetEntryNode leafSetEntry : withValue) { withChild(leafSetEntry); } return this; } @Override public ImmutableUserLeafSetNodeBuilder withChildValue(final T childValue) { return withChild(ImmutableLeafSetEntryNodeBuilder.create() .withNodeIdentifier(new NodeWithValue<>(nodeIdentifier.getNodeType(), childValue)) .withValue(childValue).build()); } protected static final class ImmutableUserLeafSetNode extends AbstractNormalizedNode> implements UserLeafSetNode { private final Map> children; ImmutableUserLeafSetNode(final NodeIdentifier nodeIdentifier, final Map> children) { super(nodeIdentifier); this.children = children; } @Override public LeafSetEntryNode childByArg(final NodeWithValue child) { return children.get(child); } @Override public LeafSetEntryNode childAt(final int position) { return Iterables.get(children.values(), position); } @Override public int size() { return children.size(); } @Override public Collection> body() { return UnmodifiableCollection.create(children.values()); } @Override protected Class> implementedType() { return (Class) UserLeafSetNode.class; } @Override protected int valueHashCode() { return children.hashCode(); } @Override protected boolean valueEquals(final UserLeafSetNode other) { return children.equals(((ImmutableUserLeafSetNode) other).children); } private Map> getChildren() { return Collections.unmodifiableMap(children); } } @Override public ImmutableUserLeafSetNodeBuilder addChild(final LeafSetEntryNode child) { return withChild(child); } @Override public ImmutableUserLeafSetNodeBuilder removeChild(final PathArgument key) { return withoutChild(key); } }