Add ModificationPath.toString()
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / schema / tree / ModificationPath.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, s.r.o. 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.checkState;
11 import static java.util.Objects.requireNonNull;
12
13 import java.util.Arrays;
14 import javax.annotation.concurrent.NotThreadSafe;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.opendaylight.yangtools.concepts.Mutable;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
19
20 @NonNullByDefault
21 @NotThreadSafe
22 final class ModificationPath implements Mutable {
23     private static final int DEFAULT_ALLOC_SIZE = 8;
24     private static final int ALLOC_SIZE = Integer.getInteger(
25         "org.opendaylight.yangtools.yang.data.impl.schema.tree.ModificationPath.ALLOC_SIZE", DEFAULT_ALLOC_SIZE);
26
27     private final YangInstanceIdentifier root;
28
29     private PathArgument[] entries = new PathArgument[ALLOC_SIZE];
30     private int used;
31
32     ModificationPath(final YangInstanceIdentifier root) {
33         this.root = requireNonNull(root);
34     }
35
36     void push(final PathArgument arg) {
37         if (entries.length == used) {
38             final int grow = used <= 32 ? used : used / 2;
39             entries = Arrays.copyOf(entries, used + grow);
40         }
41         entries[used++] = requireNonNull(arg);
42     }
43
44     void pop() {
45         checkState(used > 0, "No elements left");
46         used--;
47     }
48
49     YangInstanceIdentifier toInstanceIdentifier() {
50         return YangInstanceIdentifier.builder(root).append(Arrays.asList(entries).subList(0, used)).build();
51     }
52
53     @Override
54     public String toString() {
55         return toInstanceIdentifier().toString();
56     }
57 }