1 package org.simantics.scl.compiler.environment.filter;
\r
3 import gnu.trove.set.hash.THashSet;
\r
5 import org.simantics.scl.compiler.elaboration.expressions.EVar;
\r
6 import org.simantics.scl.compiler.module.ImportDeclaration.ImportSpec;
\r
8 public class NamespaceFilters {
\r
9 public static NamespaceFilter createFromSpec(ImportSpec spec) {
\r
11 if(spec.values.length == 0)
\r
12 return AcceptAllNamespaceFilter.INSTANCE;
\r
13 THashSet<String> names = new THashSet<String>(spec.values.length);
\r
14 for(EVar value : spec.values)
\r
15 names.add(value.name);
\r
16 return new NegativeNamespaceFilter(names);
\r
19 THashSet<String> names = new THashSet<String>(spec.values.length);
\r
20 for(EVar value : spec.values)
\r
21 names.add(value.name);
\r
22 return new PositiveNamespaceFilter(names);
\r
26 public static NamespaceFilter union(NamespaceFilter a, NamespaceFilter b) {
\r
27 if(a == AcceptAllNamespaceFilter.INSTANCE || b == AcceptAllNamespaceFilter.INSTANCE)
\r
28 return AcceptAllNamespaceFilter.INSTANCE;
\r
29 if(a instanceof PositiveNamespaceFilter) {
\r
30 if(b instanceof PositiveNamespaceFilter)
\r
31 return unionImpl((PositiveNamespaceFilter)a, (PositiveNamespaceFilter)b);
\r
32 else if(b instanceof NegativeNamespaceFilter)
\r
33 return unionImpl((NegativeNamespaceFilter)b, (PositiveNamespaceFilter)a);
\r
35 else if(a instanceof NegativeNamespaceFilter) {
\r
36 if(b instanceof PositiveNamespaceFilter)
\r
37 return unionImpl((NegativeNamespaceFilter)a, (PositiveNamespaceFilter)b);
\r
38 else if(b instanceof NegativeNamespaceFilter)
\r
39 return unionImpl((NegativeNamespaceFilter)a, (NegativeNamespaceFilter)b);
\r
41 return new NamespaceFilterUnion(a, b);
\r
44 private static NamespaceFilter unionImpl(PositiveNamespaceFilter a, PositiveNamespaceFilter b) {
\r
45 THashSet<String> includedValues = new THashSet<String>(a.includedValues.size() + b.includedValues.size());
\r
46 includedValues.addAll(a.includedValues);
\r
47 includedValues.addAll(b.includedValues);
\r
48 if(includedValues.size() == a.includedValues.size())
\r
50 if(includedValues.size() == b.includedValues.size())
\r
52 return new PositiveNamespaceFilter(includedValues);
\r
55 private static NamespaceFilter unionImpl(NegativeNamespaceFilter a, PositiveNamespaceFilter b) {
\r
56 THashSet<String> excludedValues = new THashSet<String>(a.excludedValues);
\r
57 excludedValues.removeAll(b.includedValues);
\r
58 if(excludedValues.size() == a.excludedValues.size())
\r
60 if(excludedValues.isEmpty())
\r
61 return AcceptAllNamespaceFilter.INSTANCE;
\r
62 return new NegativeNamespaceFilter(excludedValues);
\r
65 private static NamespaceFilter unionImpl(NegativeNamespaceFilter a, NegativeNamespaceFilter b) {
\r
66 THashSet<String> excludedValues = new THashSet<String>(a.excludedValues);
\r
67 excludedValues.retainAll(b.excludedValues);
\r
68 if(excludedValues.size() == a.excludedValues.size())
\r
70 if(excludedValues.size() == b.excludedValues.size())
\r
72 if(excludedValues.isEmpty())
\r
73 return AcceptAllNamespaceFilter.INSTANCE;
\r
74 return new NegativeNamespaceFilter(excludedValues);
\r
77 public static NamespaceFilter intersection(NamespaceFilter a, NamespaceFilter b) {
\r
78 if(a == AcceptAllNamespaceFilter.INSTANCE)
\r
80 if(b == AcceptAllNamespaceFilter.INSTANCE)
\r
82 if(a instanceof PositiveNamespaceFilter) {
\r
83 if(b instanceof PositiveNamespaceFilter)
\r
84 return intersectionImpl((PositiveNamespaceFilter)a, (PositiveNamespaceFilter)b);
\r
85 else if(b instanceof NegativeNamespaceFilter)
\r
86 return intersectionImpl((PositiveNamespaceFilter)a, (NegativeNamespaceFilter)b);
\r
88 else if(a instanceof NegativeNamespaceFilter) {
\r
89 if(b instanceof PositiveNamespaceFilter)
\r
90 return intersectionImpl((PositiveNamespaceFilter)b, (NegativeNamespaceFilter)a);
\r
91 else if(b instanceof NegativeNamespaceFilter)
\r
92 return intersectionImpl((NegativeNamespaceFilter)a, (NegativeNamespaceFilter)b);
\r
94 return new NamespaceFilterIntersection(a, b);
\r
97 private static NamespaceFilter intersectionImpl(NegativeNamespaceFilter a,
\r
98 NegativeNamespaceFilter b) {
\r
99 THashSet<String> excludedValues = new THashSet<String>(a.excludedValues.size() + b.excludedValues.size());
\r
100 excludedValues.addAll(a.excludedValues);
\r
101 excludedValues.addAll(b.excludedValues);
\r
102 if(excludedValues.size() == a.excludedValues.size())
\r
104 if(excludedValues.size() == b.excludedValues.size())
\r
106 return new NegativeNamespaceFilter(excludedValues);
\r
109 private static NamespaceFilter intersectionImpl(PositiveNamespaceFilter a,
\r
110 NegativeNamespaceFilter b) {
\r
111 THashSet<String> includedValues = new THashSet<String>(a.includedValues);
\r
112 includedValues.removeAll(b.excludedValues);
\r
113 if(includedValues.size() == a.includedValues.size())
\r
115 return new PositiveNamespaceFilter(includedValues);
\r
118 private static NamespaceFilter intersectionImpl(PositiveNamespaceFilter a,
\r
119 PositiveNamespaceFilter b) {
\r
120 THashSet<String> includedValues = new THashSet<String>(a.includedValues);
\r
121 includedValues.retainAll(b.includedValues);
\r
122 if(includedValues.size() == a.includedValues.size())
\r
124 if(includedValues.size() == b.includedValues.size())
\r
126 return new PositiveNamespaceFilter(includedValues);
\r