2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.impl;
10 import com.google.common.collect.ImmutableMap;
11 import com.google.common.collect.ImmutableSet;
12 import com.google.common.collect.ImmutableSetMultimap;
13 import com.google.common.collect.Multimaps;
14 import com.google.common.collect.SetMultimap;
16 import java.util.Collection;
19 import java.util.TreeMap;
20 import javax.annotation.concurrent.Immutable;
21 import org.opendaylight.yangtools.yang.model.api.Module;
22 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
23 import org.opendaylight.yangtools.yang.model.util.AbstractSchemaContext;
24 import org.opendaylight.yangtools.yang.parser.util.ModuleDependencySort;
27 final class SchemaContextImpl extends AbstractSchemaContext {
29 private final Map<ModuleIdentifier, String> identifiersToSources;
30 private final SetMultimap<URI, Module> namespaceToModules;
31 private final SetMultimap<String, Module> nameToModules;
32 private final Set<Module> modules;
34 SchemaContextImpl(final Set<Module> modules, final Map<ModuleIdentifier, String> identifiersToSources) {
35 this.identifiersToSources = ImmutableMap.copyOf(identifiersToSources);
38 * Instead of doing this on each invocation of getModules(), pre-compute
39 * it once and keep it around -- better than the set we got in.
41 this.modules = ImmutableSet.copyOf(ModuleDependencySort.sort(modules.toArray(new Module[modules.size()])));
44 * The most common lookup is from Namespace->Module.
46 * RESTCONF performs lookups based on module name only, where it wants
47 * to receive the latest revision
49 * Invest some quality time in building up lookup tables for both.
51 final SetMultimap<URI, Module> nsMap = Multimaps.newSetMultimap(
52 new TreeMap<URI, Collection<Module>>(), MODULE_SET_SUPPLIER);
53 final SetMultimap<String, Module> nameMap = Multimaps.newSetMultimap(
54 new TreeMap<String, Collection<Module>>(), MODULE_SET_SUPPLIER);
56 for (Module m : modules) {
57 nameMap.put(m.getName(), m);
58 nsMap.put(m.getNamespace(), m);
61 namespaceToModules = ImmutableSetMultimap.copyOf(nsMap);
62 nameToModules = ImmutableSetMultimap.copyOf(nameMap);
66 protected Map<ModuleIdentifier, String> getIdentifiersToSources(){
68 return identifiersToSources;
72 public Set<Module> getModules(){
78 protected SetMultimap<URI, Module> getNamespaceToModules() {
80 return namespaceToModules;
84 protected SetMultimap<String, Module> getNameToModules() {
90 public String toString() {
92 return String.format("SchemaContextImpl{modules=%s}", modules);