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;
18 import java.util.TreeMap;
19 import javax.annotation.concurrent.Immutable;
20 import org.opendaylight.yangtools.yang.model.api.Module;
21 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
22 import org.opendaylight.yangtools.yang.model.util.AbstractSchemaContext;
23 import org.opendaylight.yangtools.yang.parser.util.ModuleDependencySort;
26 final class SchemaContextImpl extends AbstractSchemaContext {
28 private final Map<ModuleIdentifier, String> identifiersToSources;
29 private final SetMultimap<URI, Module> namespaceToModules;
30 private final SetMultimap<String, Module> nameToModules;
31 private final Set<Module> modules;
33 SchemaContextImpl(final Set<Module> modules, final Map<ModuleIdentifier, String> identifiersToSources) {
34 this.identifiersToSources = ImmutableMap.copyOf(identifiersToSources);
37 * Instead of doing this on each invocation of getModules(), pre-compute
38 * it once and keep it around -- better than the set we got in.
40 this.modules = ImmutableSet.copyOf(ModuleDependencySort.sort(modules.toArray(new Module[modules.size()])));
43 * The most common lookup is from Namespace->Module.
45 * RESTCONF performs lookups based on module name only, where it wants
46 * to receive the latest revision
48 * Invest some quality time in building up lookup tables for both.
50 final SetMultimap<URI, Module> nsMap = Multimaps.newSetMultimap(
51 new TreeMap<>(), MODULE_SET_SUPPLIER);
52 final SetMultimap<String, Module> nameMap = Multimaps.newSetMultimap(
53 new TreeMap<>(), MODULE_SET_SUPPLIER);
55 for (Module m : modules) {
56 nameMap.put(m.getName(), m);
57 nsMap.put(m.getNamespace(), m);
60 namespaceToModules = ImmutableSetMultimap.copyOf(nsMap);
61 nameToModules = ImmutableSetMultimap.copyOf(nameMap);
65 protected Map<ModuleIdentifier, String> getIdentifiersToSources(){
67 return identifiersToSources;
71 public Set<Module> getModules(){
77 protected SetMultimap<URI, Module> getNamespaceToModules() {
79 return namespaceToModules;
83 protected SetMultimap<String, Module> getNameToModules() {
89 public String toString() {
91 return String.format("SchemaContextImpl{modules=%s}", modules);