260bf7b6e09bdf7e18ab2f1f36a3228fb50963d4
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / impl / SchemaPathStack.java
1 /*
2  * Copyright (c) 2013 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.parser.impl;
9
10 import com.google.common.base.Preconditions;
11
12 import java.util.Deque;
13 import java.util.LinkedList;
14
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
17
18 final class SchemaPathStack {
19     private final Deque<SchemaPath> paths = new LinkedList<>();
20
21     SchemaPath addNodeToPath(final QName name) {
22         SchemaPath sp = paths.pop();
23         sp = sp.createChild(name);
24         paths.push(sp);
25         return sp;
26     }
27
28     QName removeNodeFromPath() {
29         SchemaPath sp = paths.pop();
30         QName ret = sp.getLastComponent();
31         paths.push(Preconditions.checkNotNull(sp.getParent(), "Attempted to remove too many nodes from schemapath at stack %s", paths));
32         return ret;
33     }
34
35     SchemaPath currentSchemaPath() {
36         return paths.peek();
37     }
38
39     void pop() {
40         paths.pop();
41     }
42
43     void push() {
44         paths.push(SchemaPath.ROOT);
45     }
46 }