Package: Tuple

Tuple

nameinstructionbranchcomplexitylinemethod
Tuple(Object[])
M: 5 C: 17
77%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 1 C: 5
83%
M: 0 C: 1
100%
compareTo(Object)
M: 64 C: 0
0%
M: 12 C: 0
0%
M: 7 C: 0
0%
M: 18 C: 0
0%
M: 1 C: 0
0%
compatible(Class[])
M: 72 C: 0
0%
M: 22 C: 0
0%
M: 12 C: 0
0%
M: 18 C: 0
0%
M: 1 C: 0
0%
copy()
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
equals(Object)
M: 10 C: 47
82%
M: 8 C: 10
56%
M: 7 C: 3
30%
M: 4 C: 13
76%
M: 0 C: 1
100%
get(int)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
hashCode()
M: 30 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
init(Object[])
M: 0 C: 29
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
mk_(Object[])
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
size()
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
toString()
M: 0 C: 47
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 5
100%
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: /*
25: * The source of inspiration for the implementation of this class:
26: * https://github.com/ripper234/Basic/tree/master/java/src/main/java/org/basic/datastructures/tuple
27: */
28: @SuppressWarnings("rawtypes")
29: public class Tuple implements ValueType, Comparable
30: {
31:         private static final long serialVersionUID = 8013562414260810880L;
32:
33:         private final Object[] values;
34:
35:         public static Tuple mk_(Object... values)
36:         {
37:                 return new Tuple(values);
38:         }
39:
40:         private Tuple(Object[] values)
41:         {
42:•                if (values == null || values.length < 2)
43:                 {
44:                         throw new IllegalArgumentException("A tuple can only have two or more values");
45:                 } else
46:                 {
47:
48:                         this.values = new Object[values.length];
49:                         init(values);
50:                 }
51:         }
52:
53:         private void init(Object[] initvalues)
54:         {
55:•                for (int i = 0; i < initvalues.length; i++)
56:                 {
57:                         Object currentValue = initvalues[i];
58:
59:•                        if (currentValue instanceof ValueType)
60:                         {
61:                                 this.values[i] = ((ValueType) currentValue).copy();
62:                         } else
63:                         {
64:                                 this.values[i] = currentValue;
65:                         }
66:                 }
67:         }
68:
69:         public Long size()
70:         {
71:                 return (long) values.length;
72:         }
73:
74:         public Object get(int i)
75:         {
76:                 return values[i];
77:         }
78:
79:         public boolean compatible(Class... types)
80:         {
81:•                if (this.values.length != types.length)
82:                 {
83:                         return false;
84:                 }
85:
86:•                for (int i = 0; i < this.values.length; i++)
87:                 {
88:                         Object toValue = this.values[i];
89:                         Class type = types[i];
90:
91:•                        if (type == null)
92:                         {
93:                                 return false;
94:                         }
95:
96:•                        if (toValue instanceof VDMSeq && type == String.class)
97:                         {
98:•                                for (Object c : (VDMSeq) toValue)
99:                                 {
100:•                                        if (!(c instanceof Character))
101:                                         {
102:                                                 return false;
103:                                         }
104:                                 }
105:
106:                                 return true;
107:                         }
108:
109:•                        if (toValue instanceof String && type == VDMSeq.class)
110:                         {
111:                                 return true;
112:                         }
113:
114:•                        if (toValue != null && !type.isInstance(toValue))
115:                         {
116:                                 return false;
117:                         }
118:                 }
119:
120:                 return true;
121:         }
122:
123:         @Override
124:         public boolean equals(Object obj)
125:         {
126:•                if (obj == null)
127:                 {
128:                         return false;
129:                 }
130:
131:•                if (this == obj)
132:                 {
133:                         return true;
134:                 }
135:
136:•                if (!(obj instanceof Tuple))
137:                 {
138:                         return false;
139:                 }
140:
141:                 final Tuple other = (Tuple) obj;
142:•                if (other.size() != size())
143:                 {
144:                         return false;
145:                 }
146:
147:                 final int size = values.length;
148:•                for (int i = 0; i < size; i++)
149:                 {
150:                         final Object thisNthValue = get(i);
151:                         final Object otherNthValue = other.get(i);
152:•                        if (thisNthValue == null && otherNthValue != null
153:                                         || thisNthValue != null
154:•                                                        && !Utils.equals(thisNthValue, otherNthValue))
155:                         {
156:                                 return false;
157:                         }
158:                 }
159:
160:                 return true;
161:         }
162:
163:         @Override
164:         public int hashCode()
165:         {
166:                 int hash = 17;
167:•                for (Object value : values)
168:                 {
169:•                        if (value != null)
170:                         {
171:                                 hash = hash * 37 + value.hashCode();
172:                         }
173:                 }
174:                 return hash;
175:         }
176:
177:         public Tuple copy()
178:         {
179:                 return new Tuple(values);
180:         }
181:
182:         @Override
183:         public String toString()
184:         {
185:                 StringBuilder str = new StringBuilder();
186:
187:                 str.append(Utils.toString(values[0]));
188:
189:•                for (int i = 1; i < values.length; i++)
190:                 {
191:                         str.append(", " + Utils.toString(values[i]));
192:                 }
193:
194:                 return "mk_(" + str + ")";
195:         }
196:
197:         @Override
198:         public int compareTo(Object o)
199:         {
200:•                if (o instanceof Tuple)
201:                 {
202:                         Tuple ot = (Tuple) o;
203:                         int diff = values.length - ot.values.length;
204:
205:•                        if (diff != 0)
206:                         {
207:                                 return diff;
208:                         } else
209:                         {
210:•                                for (int i = 0; i < values.length; i++)
211:                                 {
212:                                         Object val = values[i];
213:
214:•                                        if (val instanceof Comparable)
215:                                         {
216:                                                 Comparable compVal = (Comparable) val;
217:
218:                                                 @SuppressWarnings("unchecked")
219:                                                 int c = compVal.compareTo(ot.values[i]);
220:
221:•                                                if (c != 0)
222:                                                 {
223:                                                         return c;
224:                                                 }
225:                                         } else
226:                                         {
227:                                                 return 1;
228:                                         }
229:                                 }
230:
231:                                 return 0;
232:                         }
233:                 }
234:
235:•                if (o == null)
236:                 {
237:                         return 1;
238:                 }
239:
240:                 return this.toString().compareTo(o.toString());
241:         }
242: }