MCPcopy Create free account
hub / github.com/Whiley/WhileyCompiler / compareData

Method compareData

src/main/java/wycc/util/AbstractItem.java:250–294  ·  view source on GitHub ↗

Compare the data object associated with a given syntactic item. An important question here is what kinds of data are actually permitted. At this stage, it's not completely clear. However, at least: Boolean , BigInteger , String and byte[] . For simpl

(Object leftData, Object rightData)

Source from the content-addressed store, hash-verified

248 * @return
249 */
250 private int compareData(Object leftData, Object rightData) {
251 if(leftData == null || rightData == null) {
252 if(leftData == rightData) {
253 return 0;
254 } else if(leftData == null) {
255 return -1;
256 } else {
257 return 1;
258 }
259 }
260 // At this point, we have two non-null data items. Therefore, we need to
261 // determine whether they refer to the same kind of item or to different
262 // kinds. In the latter case, we need to determine the relative ordering
263 // of kinds.
264 int leftKind = getDataKind(leftData);
265 int rightKind = getDataKind(rightData);
266 if(leftKind != rightKind) {
267 return leftKind - rightKind;
268 } else {
269 switch(leftKind) {
270 case 0: // Boolean
271 return ((Boolean)leftData).compareTo((Boolean)rightData);
272 case 1: // BigInteger
273 return ((BigInteger)leftData).compareTo((BigInteger)rightData);
274 case 2: // String
275 return ((String)leftData).compareTo((String)rightData);
276 default:
277 // byte[]
278 byte[] leftBytes = (byte[]) leftData;
279 byte[] rightBytes = (byte[]) rightData;
280 if(leftBytes.length != rightBytes.length) {
281 return leftBytes.length - rightBytes.length;
282 } else {
283 for(int i=0;i!=leftBytes.length;++i) {
284 int c = Byte.compare(leftBytes[i], rightBytes[i]);
285 if(c != 0) {
286 return c;
287 }
288 }
289 //
290 return 0;
291 }
292 }
293 }
294 }
295
296 private int getDataKind(Object o) {
297 if (o instanceof Boolean) {

Callers 1

compareToMethod · 0.95

Calls 3

getDataKindMethod · 0.95
compareToMethod · 0.45
compareMethod · 0.45

Tested by

no test coverage detected