/*
 * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.yangtools.yang.data.util;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import java.util.Iterator;
import javax.annotation.Nonnull;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;

public final class DataSchemaContextTree {
    private static final LoadingCache<SchemaContext, DataSchemaContextTree> TREES = CacheBuilder.newBuilder()
            .weakKeys()
            .build(new CacheLoader<SchemaContext, DataSchemaContextTree>() {

                @Override
                public DataSchemaContextTree load(final SchemaContext key) throws Exception {
                    return new DataSchemaContextTree(key);
                }

            });

    private final DataSchemaContextNode<?> root;

    private DataSchemaContextTree(final SchemaContext ctx) {
        root = DataSchemaContextNode.from(ctx);
    }

    @Nonnull public static DataSchemaContextTree from(@Nonnull final SchemaContext ctx) {
        return TREES.getUnchecked(ctx);
    }

    public DataSchemaContextNode<?> getChild(final YangInstanceIdentifier path) {
        DataSchemaContextNode<?> currentOp = root;
        Iterator<PathArgument> arguments = path.getPathArguments().iterator();
        while (arguments.hasNext()) {
            currentOp = currentOp.getChild(arguments.next());
        }
        return currentOp;
    }

    public DataSchemaContextNode<?> getRoot() {
        return root;
    }
}

@@ -491,35 +491,8 @@
 he;%0A
-import java.util.Iterator;%0A
 impo
@@ -520,16 +520,16 @@
 onnull;%0A
+
 import o
@@ -918,29 +918,29 @@
 akKeys()
-%0A            
+.weakValues()
 .build(n
@@ -996,17 +996,16 @@
 ee%3E() %7B%0A
-%0A
         
@@ -1189,25 +1189,24 @@
           %7D%0A
-%0A
             
@@ -1658,17 +1658,13 @@
     
-Iterator%3C
+for (
 Path
@@ -1675,21 +1675,14 @@
 ment
-%3E
  arg
-uments =
+ :
  pat
@@ -1705,55 +1705,8 @@
 ts()
-.iterator();%0A        while (arguments.hasNext()
 ) %7B%0A
@@ -1755,21 +1755,8 @@
 (arg
-uments.next()
 );%0A 

enable DataSchemaContextTrees to be scavenged