Use lambdas instead of anonymous classes
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / InMemoryDataTreeModificationCursor.java
1 /*
2  * Copyright (c) 2015 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 com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import java.util.ArrayDeque;
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.Deque;
16 import java.util.Iterator;
17 import javax.annotation.Nonnull;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModificationCursor;
22
23 final class InMemoryDataTreeModificationCursor extends AbstractCursor<InMemoryDataTreeModification> implements DataTreeModificationCursor {
24     private final Deque<OperationWithModification> stack = new ArrayDeque<>();
25
26     InMemoryDataTreeModificationCursor(final InMemoryDataTreeModification parent, final YangInstanceIdentifier rootPath, final OperationWithModification rootOp) {
27         super(parent, rootPath);
28         stack.push(rootOp);
29     }
30
31     private OperationWithModification resolveChildModification(final PathArgument child) {
32         getParent().upgradeIfPossible();
33
34         final OperationWithModification op = stack.peek();
35         final Optional<ModificationApplyOperation> potential = op.getApplyOperation().getChild(child);
36         if (potential.isPresent()) {
37             final ModificationApplyOperation operation = potential.get();
38             final ModifiedNode modification = op.getModification().modifyChild(child, operation,
39                 getParent().getVersion());
40
41             return OperationWithModification.from(operation, modification);
42         }
43
44         // Node not found, construct its path
45         final Collection<PathArgument> path = new ArrayList<>();
46         path.addAll(getRootPath().getPathArguments());
47
48         final Iterator<OperationWithModification> it = stack.descendingIterator();
49         // Skip the first entry, as it's already accounted for in rootPath
50         it.next();
51
52         while (it.hasNext()) {
53             path.add(it.next().getModification().getIdentifier());
54         }
55
56         throw new SchemaValidationFailedException(String.format("Child %s is not present in schema tree.", path));
57     }
58
59     @Override
60     public void enter(@Nonnull final PathArgument child) {
61         stack.push(resolveChildModification(child));
62     }
63
64     @Override
65     public void enter(@Nonnull final Iterable<PathArgument> path) {
66         int depth = 0;
67         for (PathArgument child : path) {
68             try {
69                 stack.push(resolveChildModification(child));
70             } catch (Exception e) {
71                 // Undo what we have done
72                 for (int i = 0; i < depth; ++i) {
73                     stack.pop();
74                 }
75                 throw new IllegalArgumentException(e);
76             }
77             depth++;
78         }
79     }
80
81     @Override
82     public void exit(final int depth) {
83         Preconditions.checkArgument(depth >= 0);
84         Preconditions.checkState(depth < stack.size());
85
86         for (int i = 0; i < depth; i++) {
87             stack.pop();
88         }
89     }
90
91     @Override
92     public Optional<NormalizedNode<?, ?>> readNode(@Nonnull final PathArgument child) {
93         return stack.peek().read(child, getParent().getVersion());
94     }
95
96     @Override
97     public void delete(final PathArgument child) {
98         ensureNotClosed();
99         resolveChildModification(child).delete();
100     }
101
102     @Override
103     public void merge(final PathArgument child, final NormalizedNode<?, ?> data) {
104         ensureNotClosed();
105         InMemoryDataTreeModification.checkIdentifierReferencesData(child, data);
106         resolveChildModification(child).merge(data, getParent().getVersion());
107     }
108
109     @Override
110     public void write(final PathArgument child, final NormalizedNode<?, ?> data) {
111         ensureNotClosed();
112         InMemoryDataTreeModification.checkIdentifierReferencesData(child, data);
113         resolveChildModification(child).write(data);
114     }
115 }