jfun.util.typing
Class TypedSuite

java.lang.Object
  extended by jfun.util.typing.TypedSuite

public abstract class TypedSuite
extends java.lang.Object

Abstract class for implementing a suite of typed elements. When an object is matched against this suite, the most restrictive sub-type is picked. Ambiguity is reported when more than one types match.

The match result is cached for better performance.

Author:
Ben Yu Jan 3, 2006 2:45:45 PM

Constructor Summary
protected TypedSuite()
           
 
Method Summary
protected abstract  java.lang.Object ambiguity(java.lang.Object arg, java.lang.Object candidate1, java.lang.Object candidate2)
          This method is called when ambiguity is detected.
protected  java.lang.Object getCandidate(java.lang.Object arg)
          The main entry of TypedSuite.
protected abstract  java.util.List getCandidates()
          Provides the candidate elements in the suite.
protected abstract  java.lang.Class getType(java.lang.Object candidate)
          Get the type of a candidate object.
protected abstract  java.lang.Object not_found(java.lang.Object arg)
          This method is called when no matching element is found.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TypedSuite

protected TypedSuite()
Method Detail

getCandidates

protected abstract java.util.List getCandidates()
Provides the candidate elements in the suite.


getType

protected abstract java.lang.Class getType(java.lang.Object candidate)
Get the type of a candidate object.

Parameters:
candidate - the candidate object.
Returns:
the type of the object.

not_found

protected abstract java.lang.Object not_found(java.lang.Object arg)
This method is called when no matching element is found.

Parameters:
arg - the object to match.
Returns:
the result.

ambiguity

protected abstract java.lang.Object ambiguity(java.lang.Object arg,
                                              java.lang.Object candidate1,
                                              java.lang.Object candidate2)
This method is called when ambiguity is detected. Typically the implementation will throw an exception.

Parameters:
arg - the object to match.
candidate1 - the first candidate that matches.
candidate2 - the second condidate that matches.
Returns:
the result.

getCandidate

protected java.lang.Object getCandidate(java.lang.Object arg)
The main entry of TypedSuite.

The internal cache is consulted first, if an matching entry is found, it is returned.

When no entry is found in cache, the candidates are investigated one by one. not_found(Object) is called if no matching element is found; ambiguity(Object, Object, Object) is called if ambiguity is detected. The result is finally cached in the internal cache.

Parameters:
arg - the object to be matched.
Returns:
the matching element.