Package: SetUtil

SetUtil

nameinstructionbranchcomplexitylinemethod
SetUtil()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
diff(Object, Object)
M: 24 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
dinter(Object)
M: 33 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
dunion(Object)
M: 32 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 10 C: 0
0%
M: 1 C: 0
0%
inSet(Object, Object)
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
intersect(Object, Object)
M: 24 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
powerset(Object)
M: 93 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 24 C: 0
0%
M: 1 C: 0
0%
psubset(Object, Object)
M: 23 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
range(Object, Object)
M: 42 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
set()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
set(Object[])
M: 31 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 6 C: 0
0%
M: 1 C: 0
0%
subset(Object, Object)
M: 0 C: 14
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
union(Object, Object)
M: 24 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
validateSet(Object, String)
M: 23 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
validateSets(Object, Object, String)
M: 23 C: 7
23%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 2 C: 2
50%
M: 0 C: 1
100%

Coverage

1: /*
2: * #%~
3: * VDM Code Generator Runtime
4: * %%
5: * Copyright (C) 2008 - 2014 Overture
6: * %%
7: * This program is free software: you can redistribute it and/or modify
8: * it under the terms of the GNU General Public License as
9: * published by the Free Software Foundation, either version 3 of the
10: * License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program. If not, see
19: * <http://www.gnu.org/licenses/gpl-3.0.html>.
20: * #~%
21: */
22: package org.overture.codegen.runtime;
23:
24: public class SetUtil
25: {
26:         public static VDMSet set()
27:         {
28:                 return new VDMSet();
29:         }
30:
31:         @SuppressWarnings("unchecked")
32:         public static VDMSet set(Object... elements)
33:         {
34:•                if (elements == null)
35:                 {
36:                         throw new IllegalArgumentException("Cannot instantiate set from null");
37:                 }
38:
39:                 VDMSet set = set();
40:
41:•                for (Object element : elements)
42:                 {
43:                         set.add(element);
44:                 }
45:
46:                 return set;
47:         }
48:
49:         public static boolean inSet(Object elem, Object set)
50:         {
51:                 validateSet(set, "'in set'");
52:
53:                 VDMSet vdmSet = (VDMSet) set;
54:
55:                 return vdmSet.contains(elem);
56:         }
57:
58:         @SuppressWarnings("unchecked")
59:         public static boolean subset(Object left, Object right)
60:         {
61:                 validateSets(left, right, "subset");
62:
63:                 VDMSet leftSet = (VDMSet) left;
64:                 VDMSet rightSet = (VDMSet) right;
65:
66:                 return rightSet.containsAll(leftSet);
67:         }
68:
69:         @SuppressWarnings("unchecked")
70:         public static VDMSet union(Object left, Object right)
71:         {
72:                 validateSets(left, right, "set union");
73:
74:                 VDMSet leftSet = (VDMSet) left;
75:                 VDMSet rightSet = (VDMSet) right;
76:
77:                 VDMSet result = new VDMSet();
78:
79:                 result.addAll(leftSet);
80:                 result.addAll(rightSet);
81:
82:                 return result;
83:         }
84:
85:         @SuppressWarnings("unchecked")
86:         public static VDMSet dunion(Object setOfSets)
87:         {
88:                 final String DUNION = "distributed union";
89:
90:                 validateSet(setOfSets, DUNION);
91:
92:                 VDMSet vdmSetOfSets = (VDMSet) setOfSets;
93:
94:                 VDMSet result = set();
95:
96:•                for (Object set : vdmSetOfSets)
97:                 {
98:                         validateSet(set, DUNION);
99:
100:                         VDMSet vdmSet = (VDMSet) set;
101:                         result.addAll(vdmSet);
102:                 }
103:
104:                 return result;
105:         }
106:
107:         @SuppressWarnings("unchecked")
108:         public static VDMSet dinter(Object setOfSets)
109:         {
110:                 final String DINTER = "distributed intersection";
111:
112:                 validateSet(setOfSets, DINTER);
113:
114:                 VDMSet vdmSetOfSets = (VDMSet) setOfSets;
115:
116:                 VDMSet result = dunion(vdmSetOfSets);
117:
118:•                for (Object set : vdmSetOfSets)
119:                 {
120:                         validateSet(set, DINTER);
121:
122:                         VDMSet vdmSet = (VDMSet) set;
123:                         result.retainAll(vdmSet);
124:                 }
125:
126:                 return result;
127:         }
128:
129:         @SuppressWarnings("unchecked")
130:         public static VDMSet diff(Object left, Object right)
131:         {
132:                 validateSets(left, right, "set difference");
133:
134:                 VDMSet setLeft = (VDMSet) left;
135:                 VDMSet setRight = (VDMSet) right;
136:
137:                 VDMSet result = new VDMSet();
138:
139:                 result.addAll(setLeft);
140:                 result.removeAll(setRight);
141:
142:                 return result;
143:         }
144:
145:         @SuppressWarnings("unchecked")
146:         public static boolean psubset(Object left, Object right)
147:         {
148:                 validateSets(left, right, "proper subset");
149:
150:                 VDMSet setLeft = (VDMSet) left;
151:                 VDMSet setRight = (VDMSet) right;
152:
153:•                return setLeft.size() < setRight.size()
154:•                                && setRight.containsAll(setLeft);
155:         }
156:
157:         @SuppressWarnings("unchecked")
158:         public static VDMSet intersect(Object left, Object right)
159:         {
160:                 validateSets(left, right, "set intersection");
161:
162:                 VDMSet setLeft = (VDMSet) left;
163:                 VDMSet setRight = (VDMSet) right;
164:
165:                 VDMSet result = new VDMSet();
166:
167:                 result.addAll(setLeft);
168:                 result.retainAll(setRight);
169:
170:                 return result;
171:         }
172:
173:         @SuppressWarnings("unchecked")
174:         public static VDMSet powerset(Object originalSet)
175:         {
176:
177:                 validateSet(originalSet, "power set");
178:
179:                 VDMSet vdmOriginalSet = (VDMSet) originalSet;
180:
181:                 VDMSet sets = SetUtil.set();
182:
183:•                if (vdmOriginalSet.isEmpty())
184:                 {
185:                         sets.add(SetUtil.set());
186:                         return sets;
187:                 }
188:
189:                 VDMSeq seq = SeqUtil.seq();
190:                 seq.addAll(vdmOriginalSet);
191:
192:                 Object firstElement = seq.get(0);
193:                 VDMSet rest = SetUtil.set();
194:                 rest.addAll(seq.subList(1, seq.size()));
195:
196:                 VDMSet powerSets = powerset(rest);
197:                 Object[] powerSetsArray = powerSets.toArray();
198:
199:•                for (int i = 0; i < powerSets.size(); i++)
200:                 {
201:                         Object obj = powerSetsArray[i];
202:•                        if (!(obj instanceof VDMSet))
203:                         {
204:                                 throw new IllegalArgumentException("Powerset operation is only applicable to sets. Got: "
205:                                                 + obj);
206:                         }
207:
208:                         VDMSet set = (VDMSet) obj;
209:
210:                         VDMSet newSet = SetUtil.set();
211:                         newSet.add(firstElement);
212:                         newSet.addAll(set);
213:                         sets.add(newSet);
214:                         sets.add(set);
215:                 }
216:
217:                 return sets;
218:         }
219:
220:         @SuppressWarnings("unchecked")
221:         public static VDMSet range(Object first, Object last)
222:         {
223:                 Utils.validateNumbers(first, last, "set range");
224:
225:                 Number firstNumber = (Number) first;
226:                 Number lastNumber = (Number) last;
227:
228:                 long from = (long) Math.ceil(firstNumber.doubleValue());
229:                 long to = (long) Math.floor(lastNumber.doubleValue());
230:
231:                 VDMSet result = new VDMSet();
232:
233:•                for (long i = from; i <= to; i++)
234:                 {
235:                         result.add(i);
236:                 }
237:
238:                 return result;
239:         }
240:
241:         static void validateSet(Object arg, String operator)
242:         {
243:•                if (!(arg instanceof VDMSet))
244:                 {
245:                         throw new IllegalArgumentException(operator
246:                                         + " is only supported for " + VDMSet.class.getName()
247:                                         + ". Got " + arg);
248:                 }
249:         }
250:
251:         private static void validateSets(Object left, Object right, String operator)
252:         {
253:•                if (!(left instanceof VDMSet) || !(right instanceof VDMSet))
254:                 {
255:                         throw new IllegalArgumentException(operator
256:                                         + " is only supported for " + VDMSet.class.getName()
257:                                         + ". Got " + left + " and " + right);
258:                 }
259:         }
260: }