Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 254118 Details for
Bug 460618
JavaElementComparator: Comparison method violates its general contract (was: Trying to create new working set as part of an import fails (on Java8)
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read
this important communication.
Small utility function testing that a Comparator implements its contract
file_460618.txt (text/plain), 3.37 KB, created by
Timo Kinnunen
on 2015-06-04 12:19:45 EDT
(
hide
)
Description:
Small utility function testing that a Comparator implements its contract
Filename:
MIME Type:
Creator:
Timo Kinnunen
Created:
2015-06-04 12:19:45 EDT
Size:
3.37 KB
patch
obsolete
>package org.eclipse.jface.viewers; > >import java.util.Comparator; >import java.util.IdentityHashMap; >import java.util.function.Consumer; >import java.util.function.Function; > >class ComparatorContract { > static <T> Comparator<T> enforce(T[] elements, Function<T, String> labeling, Comparator<T> c) { > T[] clone = elements.clone(); > int length = elements.length; > IdentityHashMap<T, Integer> cloneIndex = new IdentityHashMap<>(length); > byte[] results = new byte[length * length]; > for(int x = 0; x < length; x++) { > T objectX = clone[x]; > cloneIndex.put(objectX, x); > for(int y = 0; y < length; y++) { > T objectY = clone[y]; > int result = c.compare(objectX, objectY); > results[x * length + y] = (byte) Integer.signum(result); > } > } > Function<T, String> id = o -> o == null ? "null" : labeling.apply(o) + " (" + o.getClass().getName() + ")"; > StringBuilder buf = new StringBuilder(); > for(int x = 0; x < length; x++) { > T theX = clone[x]; > for(int y = 0; y < length; y++) { > T theY = clone[y]; > int xy = results[x * length + y]; > int yx = results[y * length + x]; > //The implementor must ensure that sgn(compare(x, y)) == -sgn(compare(y, x)) for all x and y. > if(x >= y && xy != -yx) { > String text = "compare(x, y) is %d but compare(y, x) is not %d, it is %d.%nx was %s,%ny was %s%n"; > buf.append(String.format(text, xy, -xy, yx, id.apply(theX), id.apply(theY))); > } > for(int z = 0; z < length; z++) { > T theZ = clone[z]; > int yz = results[y * length + z]; > int xz = results[x * length + z]; > //The implementor must also ensure that the relation is transitive: > //((compare(x, y)>0) && (compare(y, z)>0)) implies compare(x, z)>0. > if(xy > 0 && yz > 0 && xz <= 0) { > String text = > "compare(x, y) and compare(y, z) are > 0 but compare(x, z) is not > 0, it is %d.%nx was %s,%ny was %s,%nz was %s%n"; > buf.append(String.format(text, xz, id.apply(theX), id.apply(theY), id.apply(theZ))); > } > //Finally, the implementor must ensure that > //compare(x, y)==0 implies that sgn(compare(x, z))==sgn(compare(y, z)) for all z. > if(xy == 0 && xz != yz) { > String text = > "compare(x, y) is 0, but compare(x, z) and compare(y, z) are different: %d is not %d.%nx was %s,%ny was %s,%nz was %s%n"; > buf.append(String.format(text, xz, yz, id.apply(theX), id.apply(theY), id.apply(theZ))); > } > } > } > } > Consumer<String> error = s -> { > throw new IllegalArgumentException(s); > }; > if(buf.length() > 0) { > error.accept(buf.toString()); > } > return (Comparator<T>) (theA, theB) -> { > if(!cloneIndex.containsKey(theA)) { > String text = "new object has been added to the array during sorting.%nit was %s%n"; > error.accept(String.format(text, id.apply(theA))); > } > int a = cloneIndex.get(theA); > if(!cloneIndex.containsKey(theB)) { > String text = "new object has been added to the array during sorting.%nit was %s%n"; > error.accept(String.format(text, id.apply(theB))); > } > int b = cloneIndex.get(theB); > int ab = results[a * length + b]; > int result = c.compare(theA, theB); > if(ab != Integer.signum(result)) { > String text = "compare(x, y) was %d earlier, but now it is %d.%nx was %s,%ny was %s%n"; > error.accept(String.format(text, ab, result, id.apply(theA), id.apply(theB))); > } > return result; > }; > } >}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 460618
:
254118
|
254154
|
254160