Drop unneeded generic type specifiers
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / impl / SchemaContextImpl.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.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;
15 import java.net.URI;
16 import java.util.Collection;
17 import java.util.Map;
18 import java.util.Set;
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;
25
26 @Immutable
27 final class SchemaContextImpl extends AbstractSchemaContext {
28
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;
33
34     SchemaContextImpl(final Set<Module> modules, final Map<ModuleIdentifier, String> identifiersToSources) {
35         this.identifiersToSources = ImmutableMap.copyOf(identifiersToSources);
36
37          /*
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.
40          */
41         this.modules = ImmutableSet.copyOf(ModuleDependencySort.sort(modules.toArray(new Module[modules.size()])));
42
43          /*
44          * The most common lookup is from Namespace->Module.
45          *
46          * RESTCONF performs lookups based on module name only, where it wants
47          * to receive the latest revision
48          *
49          * Invest some quality time in building up lookup tables for both.
50          */
51         final SetMultimap<URI, Module> nsMap = Multimaps.newSetMultimap(
52                 new TreeMap<>(), MODULE_SET_SUPPLIER);
53         final SetMultimap<String, Module> nameMap = Multimaps.newSetMultimap(
54                 new TreeMap<>(), MODULE_SET_SUPPLIER);
55
56         for (Module m : modules) {
57             nameMap.put(m.getName(), m);
58             nsMap.put(m.getNamespace(), m);
59         }
60
61         namespaceToModules = ImmutableSetMultimap.copyOf(nsMap);
62         nameToModules = ImmutableSetMultimap.copyOf(nameMap);
63     }
64
65     @Override
66     protected Map<ModuleIdentifier, String> getIdentifiersToSources(){
67
68         return identifiersToSources;
69     }
70
71     @Override
72     public Set<Module> getModules(){
73
74         return modules;
75     }
76
77     @Override
78     protected SetMultimap<URI, Module> getNamespaceToModules() {
79
80         return namespaceToModules;
81     }
82
83     @Override
84     protected SetMultimap<String, Module> getNameToModules() {
85
86         return nameToModules;
87     }
88
89     @Override
90     public String toString() {
91
92         return String.format("SchemaContextImpl{modules=%s}", modules);
93     }
94 }