I had another look at the current implementation of And/Or last night.
As the implementation already uses a collection, why not providing a
constructor with a collection as a parameter:

public Or(Collection collection)
{
        this.list.addAll(collection);
}

(Or adding predicate one by one just to make sure they are actually
predicates)

This way, no need for static method.

This is not the way I would have done it (that's why I misunderstood the
way it is coded so far) as I believe OR and AND are binary operations.
So the only constructor would be:

public Or(Predicate left, Predicate right);

And then a static method could be:

public static And and(Collection predicates)
{
        Predicate predicate=null;
        for (Iterator i=predicates.iterator(); i.hasNext(); )
        {
                if (predicate==null)
                        predicate=(Predicate)i.next();
                else
                        predicate=new And(predicate, (Predicate)i.next());
        }
        if (predicate instanceof And)
                return predicate;
        return new And(predicate, new ConstantPredicate(true));
}

PROS:
        - AND/OR are binary operators
        - fields and/or methods could allow to retrieve left and right
          members of a And/Or predicate
        - lightweight (no need to use a collection for only a binary
          operation)

CONS:
        - heavyweight when more than two predicates (because of the many
          nested And objects)
        - need a static method (although a constructor with a Collection as
          a parameter is still possible)

I don't know if this method is better than the current one anyway... So
for now I'll just patch the constructor of the current implementation
(as described at the start of this mail).

Herve

On Tue, Nov 04, 2003 at 10:49:48AM -0800, Rodney Waldhoff wrote:
> Currently the AND and OR functions are not implemented as pure binary
> operations, but rather operations on a collection (list) of values.
> 
> Specifically, each maintains a list of predicates.  OR evaluates to true
> if at least one child evaulates to true (and hence evalutes to false when
> the list is empty).  AND evaluates to true if all of its children evaluate
> to true (and hence evaluates to true when the list is empty).
> 
> You can already do "new And(a,b,c)" or "new And(a).and(b).and(c)" or "new
> And(a,b).and(c)", etc., each of which should get you to the more or less
> tha same predicate.
> 
> A static "factory" constructor, such as:
> 
>  And.and(a) ==> new And(a)
> 
> or
> 
>  And.and(a,b) ==> new And(a,b)
> 
> which would allow:
> 
>  And.and(a).and(b).and(c)
> 
> or something like that would probably be convienient.
> 
> So would:
> 
>  And.and(Collection)
> 
> or
> 
>  And.and(Iterator)
> 
> - Rod <http://radio.weblogs.com/0122027/>

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to