BUG-4295: instantiate MERGE operations lazily
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / UnorderedLeafSetModificationStrategy.java
1 /*
2  * Copyright (c) 2014 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.tree;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
12 import com.google.common.base.Optional;
13 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
14 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
15 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
17 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeContainerBuilder;
18 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
19 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
20
21 final class UnorderedLeafSetModificationStrategy extends AbstractNodeContainerModificationStrategy {
22     private final Optional<ModificationApplyOperation> entryStrategy;
23
24     @SuppressWarnings({ "unchecked", "rawtypes" })
25     UnorderedLeafSetModificationStrategy(final LeafListSchemaNode schema, final TreeType treeType) {
26         super((Class) LeafSetNode.class, treeType);
27         entryStrategy = Optional.<ModificationApplyOperation> of(new LeafSetEntryModificationStrategy(schema));
28     }
29
30     @SuppressWarnings("rawtypes")
31     @Override
32     protected NormalizedNodeContainerBuilder createBuilder(final NormalizedNode<?, ?> original) {
33         checkArgument(original instanceof LeafSetNode<?>);
34         return ImmutableLeafSetNodeBuilder.create((LeafSetNode<?>) original);
35     }
36
37     @Override
38     protected NormalizedNode<?, ?> createEmptyValue(NormalizedNode<?, ?> original) {
39         checkArgument(original instanceof LeafSetNode<?>);
40         return ImmutableLeafSetNodeBuilder.create().withNodeIdentifier(((LeafSetNode<?>) original).getIdentifier())
41                 .build();
42     }
43
44     @Override
45     public Optional<ModificationApplyOperation> getChild(final YangInstanceIdentifier.PathArgument identifier) {
46         if (identifier instanceof YangInstanceIdentifier.NodeWithValue) {
47             return entryStrategy;
48         }
49         return Optional.absent();
50     }
51 }