use of java.util.Comparator in project binnavi by google.
the class CTableSorter method getComparator.
@SuppressWarnings({ "unchecked", "rawtypes" })
protected Comparator<Object> getComparator(final int column) {
final Class columnType = tableModel.getColumnClass(column);
Comparator<Object> comparator = primaryColumnComparator.get(column);
if (comparator != null) {
return comparator;
}
comparator = columnComparators.get(columnType);
if (comparator != null) {
return comparator;
}
if (columnType.equals(String.class)) {
return (Comparator) new LexicalComparator();
}
if (Comparable.class.isAssignableFrom(columnType)) {
return COMPARABLE_COMPARATOR;
}
return (Comparator) new LexicalComparator();
}
use of java.util.Comparator in project j2objc by google.
the class AbstractMapTest method test_removeLjava_lang_Object.
/**
* java.util.AbstractMap#remove(java.lang.Object)
*/
public void test_removeLjava_lang_Object() {
Object key = new Object();
Object value = new Object();
AbstractMap map1 = new HashMap(0);
map1.put("key", value);
assertSame("HashMap(0)", map1.remove("key"), value);
AbstractMap map4 = new IdentityHashMap(1);
map4.put(key, value);
assertSame("IdentityHashMap", map4.remove(key), value);
AbstractMap map5 = new LinkedHashMap(122);
map5.put(key, value);
assertSame("LinkedHashMap", map5.remove(key), value);
AbstractMap map6 = new TreeMap(new Comparator() {
// Bogus comparator
public int compare(Object object1, Object object2) {
return 0;
}
});
map6.put(key, value);
assertSame("TreeMap", map6.remove(key), value);
AbstractMap map7 = new WeakHashMap();
map7.put(key, value);
assertSame("WeakHashMap", map7.remove(key), value);
AbstractMap aSpecialMap = new MyMap();
aSpecialMap.put(specialKey, specialValue);
Object valueOut = aSpecialMap.remove(specialKey);
assertSame("MyMap", valueOut, specialValue);
}
use of java.util.Comparator in project j2objc by google.
the class ArraysTest method test_binarySearch$Ljava_lang_ObjectLjava_lang_ObjectLjava_util_Comparator.
/**
* @tests java.util.Arrays#binarySearch(java.lang.Object[],
* java.lang.Object, java.util.Comparator)
*/
public void test_binarySearch$Ljava_lang_ObjectLjava_lang_ObjectLjava_util_Comparator() {
// Test for method int java.util.Arrays.binarySearch(java.lang.Object
// [], java.lang.Object, java.util.Comparator)
Comparator comp = new ReversedIntegerComparator();
for (int counter = 0; counter < arraySize; counter++) objectArray[counter] = objArray[arraySize - counter - 1];
assertTrue("Binary search succeeded for value not present in array 1", Arrays.binarySearch(objectArray, new Integer(-1), comp) == -(arraySize + 1));
assertEquals("Binary search succeeded for value not present in array 2", -1, Arrays.binarySearch(objectArray, new Integer(arraySize), comp));
for (int counter = 0; counter < arraySize; counter++) assertTrue("Binary search on Object[] with custom comparator answered incorrect position", Arrays.binarySearch(objectArray, objArray[counter], comp) == arraySize - counter - 1);
}
use of java.util.Comparator in project j2objc by google.
the class ObjectStreamClass method computeSerialVersionUID.
/**
* Compute and return the Serial Version UID of the class {@code cl}.
* The value is computed based on the class name, superclass chain, field
* names, method names, modifiers, etc.
*
* @param cl
* a java.lang.Class for which to compute the SUID
* @param fields
* cl.getDeclaredFields(), pre-computed by the caller
* @return the value of SUID of this class
*/
private static long computeSerialVersionUID(Class<?> cl, Field[] fields) {
/*
* First we should try to fetch the static slot 'static final long
* serialVersionUID'. If it is defined, return it. If not defined, we
* really need to compute SUID using SHAOutputStream
*/
for (int i = 0; i < fields.length; i++) {
final Field field = fields[i];
if (field.getType() == long.class) {
int modifiers = field.getModifiers();
if (Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers)) {
if (UID_FIELD_NAME.equals(field.getName())) {
/*
* We need to be able to see it even if we have no
* visibility. That is why we set accessible first (new
* API in reflect 1.2)
*/
field.setAccessible(true);
try {
// Static field, parameter is ignored
return field.getLong(null);
} catch (IllegalAccessException iae) {
throw new RuntimeException("Error fetching SUID: " + iae);
}
}
}
}
}
Digest digest = getDigest();
ByteArrayOutputStream sha = new ByteArrayOutputStream();
try {
DataOutputStream output = new DataOutputStream(sha);
output.writeUTF(cl.getName());
int classModifiers = CLASS_MODIFIERS_MASK & cl.getModifiers();
/*
* Workaround for 1F9LOQO. Arrays are ABSTRACT in JDK, but that is
* not in the specification. Since we want to be compatible for
* X-loading, we have to pretend we have the same shape
*/
boolean isArray = cl.isArray();
if (isArray) {
classModifiers |= Modifier.ABSTRACT;
}
// Required for JDK UID compatibility
if (cl.isInterface() && !Modifier.isPublic(classModifiers)) {
classModifiers &= ~Modifier.ABSTRACT;
}
output.writeInt(classModifiers);
/*
* In JDK1.2 arrays implement Cloneable and Serializable but not in
* JDK 1.1.7. So, JDK 1.2 "pretends" arrays have no interfaces when
* computing SHA-1 to be compatible.
*/
if (!isArray) {
// Interface information
Class<?>[] interfaces = cl.getInterfaces();
if (interfaces.length > 1) {
// Only attempt to sort if really needed (saves object
// creation, etc)
Comparator<Class<?>> interfaceComparator = new Comparator<Class<?>>() {
public int compare(Class<?> itf1, Class<?> itf2) {
return itf1.getName().compareTo(itf2.getName());
}
};
Arrays.sort(interfaces, interfaceComparator);
}
// Dump them
for (int i = 0; i < interfaces.length; i++) {
output.writeUTF(interfaces[i].getName());
}
}
// Field information
if (fields.length > 1) {
// Only attempt to sort if really needed (saves object creation,
// etc)
Comparator<Field> fieldComparator = new Comparator<Field>() {
public int compare(Field field1, Field field2) {
return field1.getName().compareTo(field2.getName());
}
};
Arrays.sort(fields, fieldComparator);
}
// Dump them
for (int i = 0; i < fields.length; i++) {
Field field = fields[i];
int modifiers = field.getModifiers() & FIELD_MODIFIERS_MASK;
boolean skip = Modifier.isPrivate(modifiers) && (Modifier.isTransient(modifiers) || Modifier.isStatic(modifiers));
if (!skip) {
// write name, modifier & "descriptor" of all but private
// static and private transient
output.writeUTF(field.getName());
output.writeInt(modifiers);
output.writeUTF(descriptorForFieldSignature(getFieldSignature(field)));
}
}
/*
* Normally constructors come before methods (because <init> <
* anyMethodName). However, <clinit> is an exception. Besides,
* reflect will not let us get to it.
*/
if (hasClinit(cl)) {
// write name, modifier & "descriptor"
output.writeUTF(CLINIT_NAME);
output.writeInt(CLINIT_MODIFIERS);
output.writeUTF(CLINIT_SIGNATURE);
}
// Constructor information
Constructor<?>[] constructors = cl.getDeclaredConstructors();
if (constructors.length > 1) {
// Only attempt to sort if really needed (saves object creation,
// etc)
Comparator<Constructor<?>> constructorComparator = new Comparator<Constructor<?>>() {
public int compare(Constructor<?> ctr1, Constructor<?> ctr2) {
// signature
return (getConstructorSignature(ctr1).compareTo(getConstructorSignature(ctr2)));
}
};
Arrays.sort(constructors, constructorComparator);
}
// Dump them
for (int i = 0; i < constructors.length; i++) {
Constructor<?> constructor = constructors[i];
int modifiers = constructor.getModifiers() & METHOD_MODIFIERS_MASK;
boolean isPrivate = Modifier.isPrivate(modifiers);
if (!isPrivate) {
/*
* write name, modifier & "descriptor" of all but private
* ones
*
* constructor.getName() returns the constructor name as
* typed, not the VM name
*/
output.writeUTF("<init>");
output.writeInt(modifiers);
output.writeUTF(descriptorForSignature(getConstructorSignature(constructor)).replace('/', '.'));
}
}
// Method information
Method[] methods = cl.getDeclaredMethods();
if (methods.length > 1) {
Comparator<Method> methodComparator = new Comparator<Method>() {
public int compare(Method m1, Method m2) {
int result = m1.getName().compareTo(m2.getName());
if (result == 0) {
// first
return getMethodSignature(m1).compareTo(getMethodSignature(m2));
}
return result;
}
};
Arrays.sort(methods, methodComparator);
}
// Dump them
for (int i = 0; i < methods.length; i++) {
Method method = methods[i];
int modifiers = method.getModifiers() & METHOD_MODIFIERS_MASK;
boolean isPrivate = Modifier.isPrivate(modifiers);
if (!isPrivate) {
// write name, modifier & "descriptor" of all but private
// ones
output.writeUTF(method.getName());
output.writeInt(modifiers);
output.writeUTF(descriptorForSignature(getMethodSignature(method)).replace('/', '.'));
}
}
} catch (IOException e) {
throw new RuntimeException(e + " computing SHA-1/SUID");
}
// now compute the UID based on the SHA
byte[] hash = digest.digest(sha.toByteArray());
return Memory.peekLong(hash, 0, ByteOrder.LITTLE_ENDIAN);
}
use of java.util.Comparator in project j2objc by google.
the class CollectionsTest method test_reverseOrder.
/**
* @tests java.util.Collections#reverseOrder()
*/
public void test_reverseOrder() {
// Test for method java.util.Comparator
// java.util.Collections.reverseOrder()
// assumes no duplicates in ll
Comparator comp = Collections.reverseOrder();
LinkedList list2 = new LinkedList(ll);
Collections.sort(list2, comp);
final int llSize = ll.size();
for (int counter = 0; counter < llSize; counter++) assertEquals("New comparator does not reverse sorting order", list2.get(llSize - counter - 1), ll.get(counter));
}
Aggregations