d11efb4f84ce708081b67b05585d537e6e1152e0
[yangtools.git] / yang / yang-data-util / src / main / java / org / opendaylight / yangtools / yang / data / util / DataSchemaContextTree.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.util;
9
10 import com.google.common.cache.CacheBuilder;
11 import com.google.common.cache.CacheLoader;
12 import com.google.common.cache.LoadingCache;
13 import java.util.Iterator;
14 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
16 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
17
18 public final class DataSchemaContextTree {
19
20     private static final LoadingCache<SchemaContext, DataSchemaContextTree> TREES = CacheBuilder.newBuilder()
21             .weakKeys()
22             .build(new CacheLoader<SchemaContext, DataSchemaContextTree>() {
23
24                 @Override
25                 public DataSchemaContextTree load(SchemaContext key) throws Exception {
26                     return new DataSchemaContextTree(key);
27                 }
28
29             });
30
31     private final DataSchemaContextNode<?> root;
32
33     private DataSchemaContextTree(final SchemaContext ctx) {
34         root = DataSchemaContextNode.from(ctx);
35     }
36
37
38     public static DataSchemaContextTree from(SchemaContext ctx) {
39         return TREES.getUnchecked(ctx);
40     }
41
42     public DataSchemaContextNode<?> getChild(final YangInstanceIdentifier path) {
43         DataSchemaContextNode<?> currentOp = root;
44         Iterator<PathArgument> arguments = path.getPathArguments().iterator();
45         while (arguments.hasNext()) {
46             currentOp = currentOp.getChild(arguments.next());
47         }
48         return currentOp;
49     }
50
51     public DataSchemaContextNode<?> getRoot() {
52         return root;
53     }
54
55 }