/* * 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 java.util.Collections; import java.util.List; import java.util.Map; import org.opendaylight.yangtools.concepts.Immutable; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier; import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier; import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.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.impl.schema.builder.api.ListNodeBuilder; import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder; import org.opendaylight.yangtools.yang.data.impl.schema.nodes.AbstractImmutableNormalizedNode; import com.google.common.base.Optional; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; import com.google.common.collect.Maps; public class ImmutableLeafSetNodeBuilder implements ListNodeBuilder> { protected Map> value = Maps.newLinkedHashMap(); protected InstanceIdentifier.NodeIdentifier nodeIdentifier; public static ListNodeBuilder> create() { return new ImmutableLeafSetNodeBuilder<>(); } @Override public ListNodeBuilder> withChild(final LeafSetEntryNode child) { this.value.put(child.getIdentifier(), child); return this; } @Override public LeafSetNode build() { return new ImmutableLeafSetNode<>(nodeIdentifier, value); } @Override public ListNodeBuilder> withNodeIdentifier( final InstanceIdentifier.NodeIdentifier nodeIdentifier) { this.nodeIdentifier = nodeIdentifier; return this; } @Override public ListNodeBuilder> withValue(final List> value) { for (final LeafSetEntryNode leafSetEntry : value) { withChild(leafSetEntry); } return this; } @Override public ListNodeBuilder> withChildValue(final T value, final Map attributes) { return withChild(new ImmutableLeafSetEntryNodeBuilder.ImmutableLeafSetEntryNode<>( new InstanceIdentifier.NodeWithValue(nodeIdentifier.getNodeType(), value), value, attributes)); } @Override public ListNodeBuilder> withChildValue(final T value) { return withChildValue(value, Collections.emptyMap()); } private final static class ImmutableLeafSetNode extends AbstractImmutableNormalizedNode>> implements Immutable, LeafSetNode { private final Map> mappedChildren; ImmutableLeafSetNode(final InstanceIdentifier.NodeIdentifier nodeIdentifier, final Map> children) { super(nodeIdentifier, ImmutableList.copyOf(children.values())); this.mappedChildren = children; } @Override public Optional> getChild(final InstanceIdentifier.NodeWithValue child) { return Optional.fromNullable(mappedChildren.get(child)); } @Override protected int valueHashCode() { return mappedChildren.hashCode(); } @Override protected boolean valueEquals(final AbstractImmutableNormalizedNode other) { return mappedChildren.equals(((ImmutableLeafSetNode) other).mappedChildren); } } @Override public NormalizedNodeContainerBuilder, LeafSetNode> addChild( final LeafSetEntryNode child) { return withChild(child); } }