fr.umlv.jmmf.matcher
Class PatternMatcher
java.lang.Object
|
+--fr.umlv.jmmf.matcher.PatternMatcher
- public class PatternMatcher
- extends java.lang.Object
This class ease the use of multi-polymorphism.
A simple way to use it consist in subclassing the
PatternMatcher class.
An example on a tree recursive type :
public class Test extends PatternMatcher
{
interface Tree {}
static class Leaf implements Tree {
Leaf(int value) {
this.value=value;
}
int value;
}
static class Node implements Tree {
Node(Tree left,Tree right) {
this.left=left; this.right=right;
}
Tree left,right;
}
public int sum(Node node)
{ return sum(left)+sum(right); }
public int sum(Leaf leaf)
{ return value; }
public int sum(Tree tree)
{ return ((Integer)match("sum",tree)).intValue(); }
public static void main(String[] args) {
Tree tree=new Node(new Node(new Leaf(1),new Leaf(3)),new Leaf(7));
System.out.println("sum "+new Test().sum(tree));
}
}
- Version:
- 0.4
- Author:
- Remi Forax
|
Field Summary |
protected java.lang.Object |
bean
|
protected java.util.HashMap |
multimethods
|
|
Constructor Summary |
PatternMatcher()
create a pattern matcher component.
|
PatternMatcher(java.lang.Object bean)
create a pattern matcher component on a bean object.
|
|
Method Summary |
java.lang.Object |
match(java.lang.String name,
java.lang.Object arg)
lookup the best method in the multi-method named name and invoke it. |
java.lang.Object |
match(java.lang.String name,
java.lang.Object[] args)
lookup the best method in the multi-method named name and invoke it. |
java.lang.Object |
match(java.lang.String name,
java.lang.Object[] args,
java.lang.Class[] classes)
lookup the best method in the multi-method named name and invoke it. |
| Methods inherited from class java.lang.Object |
clone,
equals,
finalize,
getClass,
hashCode,
notify,
notifyAll,
toString,
wait,
wait,
wait |
bean
protected java.lang.Object bean
multimethods
protected java.util.HashMap multimethods
PatternMatcher
public PatternMatcher()
- create a pattern matcher component.
Find all multi-methods in this component and store them.
PatternMatcher
public PatternMatcher(java.lang.Object bean)
- create a pattern matcher component on a bean object.
Find all multi-methods in the bean component and store them.
match
public java.lang.Object match(java.lang.String name,
java.lang.Object arg)
- lookup the best method in the multi-method named name and invoke it.
- Parameters:
name - name of the multi-method.arg - first parameter of the multi-method.- Returns:
- the result of a call of the best method of the multi-method.
if the type of the result is a primitive type, the result
is wrapped in the corresponding object (int -> Integer, etc...).
- Throws:
- NoMatchingMethodException - there is no method that
match the parameter arguments.
- MultipleMatchingMethodsException - there is no method that
match the parameter arguments.
- MatchingMethodInvocationException - throws by
the target method invoked.
- IllegalStateException - is method find isn't accessible,
due to an internal error.
- See Also:
match(String, Object[], Class[])
match
public java.lang.Object match(java.lang.String name,
java.lang.Object[] args)
- lookup the best method in the multi-method named name and invoke it.
- Parameters:
name - name of the multi-method.args - parameters of the multi-method.- Returns:
- the result of a call of the best method of the multi-method.
if the type of the result is a primitive type, the result
is wrapped in the corresponding object (int -> Integer, etc...).
- Throws:
- NoMatchingMethodException - there is no method that
match the parameter arguments.
- MultipleMatchingMethodsException - there is no method that
match the parameter arguments.
- MatchingMethodInvocationException - throws by
the target method invoked.
- IllegalStateException - is method find isn't accessible,
due to an internal error.
- See Also:
match(String, Object[], Class[])
match
public java.lang.Object match(java.lang.String name,
java.lang.Object[] args,
java.lang.Class[] classes)
- lookup the best method in the multi-method named name and invoke it.
- Parameters:
name - name of the multi-method.args - parameters of the multi-method.classes - type of the parameter.- Returns:
- the result of a call of the best method of the multi-method.
if the type of the result is a primitive type, the result
is wrapped in the corresponding object (int -> Integer, etc...).
- Throws:
- NoMatchingMethodException - there is no method that
match the parameter arguments.
- MultipleMatchingMethodsException - there is no method that
match the parameter arguments.
- MatchingMethodInvocationException - throws by
the target method invoked.
- IllegalStateException - is method find isn't accessible,
due to an internal error.
Rémi Forax 1999,2000 Université de Marne la Vallée