]> gerrit.simantics Code Review - simantics/platform.git/blob
57159b861d7d42254266dda514db36410137157f
[simantics/platform.git] /
1 package org.simantics.scl.compiler.environment.filter;\r
2 \r
3 import gnu.trove.set.hash.THashSet;\r
4 \r
5 import org.simantics.scl.compiler.elaboration.expressions.EVar;\r
6 import org.simantics.scl.compiler.module.ImportDeclaration.ImportSpec;\r
7 \r
8 public class NamespaceFilters {\r
9     public static NamespaceFilter createFromSpec(ImportSpec spec) {\r
10         if(spec.hiding) {\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
17         }\r
18         else {\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
23         }\r
24     }\r
25     \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
34         }\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
40         }\r
41         return new NamespaceFilterUnion(a, b);\r
42     }\r
43     \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
49             return a;\r
50         if(includedValues.size() == b.includedValues.size())\r
51             return b;\r
52         return new PositiveNamespaceFilter(includedValues);\r
53     }\r
54     \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
59             return a;\r
60         if(excludedValues.isEmpty())\r
61             return AcceptAllNamespaceFilter.INSTANCE;\r
62         return new NegativeNamespaceFilter(excludedValues);\r
63     }\r
64     \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
69             return a;\r
70         if(excludedValues.size() == b.excludedValues.size())\r
71             return b;\r
72         if(excludedValues.isEmpty())\r
73             return AcceptAllNamespaceFilter.INSTANCE;\r
74         return new NegativeNamespaceFilter(excludedValues);\r
75     }\r
76     \r
77     public static NamespaceFilter intersection(NamespaceFilter a, NamespaceFilter b) {\r
78         if(a == AcceptAllNamespaceFilter.INSTANCE)\r
79             return b;\r
80         if(b == AcceptAllNamespaceFilter.INSTANCE)\r
81             return a;\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
87         }\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
93         }\r
94         return new NamespaceFilterIntersection(a, b);\r
95     }\r
96 \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
103             return a;\r
104         if(excludedValues.size() == b.excludedValues.size())\r
105             return b;\r
106         return new NegativeNamespaceFilter(excludedValues);\r
107     }\r
108 \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
114             return a;\r
115         return new PositiveNamespaceFilter(includedValues);\r
116     }\r
117 \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
123             return a;\r
124         if(includedValues.size() == b.includedValues.size())\r
125             return b;\r
126         return new PositiveNamespaceFilter(includedValues);\r
127     }\r
128 }\r