sofya.base
Class MethodSignature.NameComparator

java.lang.Object
  extended by sofya.base.MethodSignature.NameComparator
All Implemented Interfaces:
java.util.Comparator
Enclosing class:
MethodSignature

public static class MethodSignature.NameComparator
extends java.lang.Object
implements java.util.Comparator

A utility comparator which orders method signatures.

Method signatures are compared in the following manner:

  1. The full names of the two signatures, including the fully qualified class names, are compared lexically. If the two names are not equal, the result of the comparison is returned as the result of the signature comparison.
  2. If the names are equal, and an 'equals relation' has been specified to the comparator, that value is returned.
  3. If the names are equal, and no equals relation has been specified, the number of arguments to each method is compared. The method with more arguments is considered 'greater than' the other method.
  4. If both methods have an equal number of arguments, the type of each argument in each method is converted to a string and lexically compared to its match. This proceeds until an argument type comparison is non-equal, or all arguments have been compared.
  5. If all the argument types are lexically equal, the return types of each method are converted to strings and lexically compared. The result of this lexical comparison will be returned as the result of the signature comparison.

Version:
04/21/2004
Author:
Alex Kinneer

Constructor Summary
MethodSignature.NameComparator()
          Constructs a comparator which will operate in strict accordance with the contract of the compare method.
MethodSignature.NameComparator(int equalsRelation)
          Constructs a comparator which returns the specified relation for equal objects.
 
Method Summary
 int compare(java.lang.Object o1, java.lang.Object o2)
          Compares two method signatures using the lexical alphabetical relation of the two signatures' full names.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Comparator
equals
 

Constructor Detail

MethodSignature.NameComparator

public MethodSignature.NameComparator()
Constructs a comparator which will operate in strict accordance with the contract of the compare method.


MethodSignature.NameComparator

public MethodSignature.NameComparator(int equalsRelation)
Constructs a comparator which returns the specified relation for equal objects.

This is provided to allow TreeSet/Maps to be used as sorted lists by treating multiple equivalent values/keys as distinct from each other, since the java collections do not provide any true sorted list (bag) implementation. Note that for both data structures an iterator must be obtained to extract multiple redundant values/keys, as the get methods will only return the first matching value/mapping. In the case of a TreeMap, mappings must be removed after each value retrieval using a redundant key or the first mapping encountered will be retrieved every time.

Parameters:
equalsRelation - Value specifying which ordering the comparator should report for equal objects:
  • -1 specifies that the second object is to be ordered as 'less than' the first (the second object will precede the first in the list)
  • 1 specifies the inverse
  • 0 retains the normal behavior of reporting equality (in sets, the second object will replace the first).
Be aware of the following:
  1. Specifying a value other than 0 will cause the compare(java.lang.Object, java.lang.Object) method to violate its contract (specifically transitivity of equality), and become inconsistent with equals.
  2. Specifying a value other than 0 may cause non-deterministic orderings within equivalence classes of signatures when the source of those signatures is disordered. This is because the ordering of signatures determined to be equal will be dependent on the order in which they are added. In particular, signatures taken from hashed collections will not be consistently sorted.
Method Detail

compare

public int compare(java.lang.Object o1,
                   java.lang.Object o2)
Compares two method signatures using the lexical alphabetical relation of the two signatures' full names.

Specified by:
compare in interface java.util.Comparator
Parameters:
o1 - Method signature for comparison.
o2 - Method signature for comparison to o1.
Returns:
-1 if the first signature is less than the second, 0 if the two objects represent the same method signature or have the name lexically, and 1 if the second signature is greater than the first.
Throws:
java.lang.ClassCastException - If either arguments is an instance of any class (including a subclass) other than MethodSignature.