Add utility wrappers for instantiating builders/nodes
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / LeafSetModificationStrategy.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 java.util.Optional;
11 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
12 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeWithValue;
13 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
14 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
15 import org.opendaylight.yangtools.yang.data.api.schema.OrderedLeafSetNode;
16 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeConfiguration;
17 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableLeafSetNodeBuilder;
18 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableOrderedLeafSetNodeBuilder;
19 import org.opendaylight.yangtools.yang.data.impl.schema.tree.NormalizedNodeContainerSupport.Single;
20 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
21
22 final class LeafSetModificationStrategy extends AbstractNodeContainerModificationStrategy {
23     @SuppressWarnings({ "unchecked", "rawtypes" })
24     private static final Single<NodeIdentifier, OrderedLeafSetNode<?>> ORDERED_SUPPORT =
25             new Single(OrderedLeafSetNode.class, ChildTrackingPolicy.ORDERED,
26                 foo -> ImmutableOrderedLeafSetNodeBuilder.create((OrderedLeafSetNode<?>)foo),
27                 ImmutableOrderedLeafSetNodeBuilder::create);
28     @SuppressWarnings({ "unchecked", "rawtypes" })
29     private static final Single<NodeIdentifier, LeafSetNode<?>> UNORDERED_SUPPORT =
30             new Single(LeafSetNode.class,
31                 foo -> ImmutableLeafSetNodeBuilder.create((LeafSetNode<?>)foo),
32                 ImmutableLeafSetNodeBuilder::create);
33
34     private final Optional<ModificationApplyOperation> entryStrategy;
35
36     LeafSetModificationStrategy(final LeafListSchemaNode schema, final DataTreeConfiguration treeConfig) {
37         super(schema.isUserOrdered() ? ORDERED_SUPPORT : UNORDERED_SUPPORT, treeConfig);
38         entryStrategy = Optional.of(new LeafSetEntryModificationStrategy(schema));
39     }
40
41     @Override
42     public Optional<ModificationApplyOperation> getChild(final PathArgument identifier) {
43         return identifier instanceof NodeWithValue ? entryStrategy : Optional.empty();
44     }
45 }