KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jga > fn > logical > Any


1 // ============================================================================
2
// $Id: Any.java,v 1.18 2006/01/08 00:52:25 davidahall Exp $
3
// Copyright (c) 2003-2005 David A. Hall
4
// ============================================================================
5
// The contents of this file are subject to the Common Development and
6
// Distribution License (CDDL), Version 1.0 (the License); you may not use this
7
// file except in compliance with the License. You should have received a copy
8
// of the the License along with this file: if not, a copy of the License is
9
// available from Sun Microsystems, Inc.
10
//
11
// http://www.sun.com/cddl/cddl.html
12
//
13
// From time to time, the license steward (initially Sun Microsystems, Inc.) may
14
// publish revised and/or new versions of the License. You may not use,
15
// distribute, or otherwise make this file available under subsequent versions
16
// of the License.
17
//
18
// Alternatively, the contents of this file may be used under the terms of the
19
// GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
20
// case the provisions of the LGPL are applicable instead of those above. If you
21
// wish to allow use of your version of this file only under the terms of the
22
// LGPL, and not to allow others to use your version of this file under the
23
// terms of the CDDL, indicate your decision by deleting the provisions above
24
// and replace them with the notice and other provisions required by the LGPL.
25
// If you do not delete the provisions above, a recipient may use your version
26
// of this file under the terms of either the CDDL or the LGPL.
27
//
28
// This library is distributed in the hope that it will be useful,
29
// but WITHOUT ANY WARRANTY; without even the implied warranty of
30
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
31
// ============================================================================
32

33 package net.sf.jga.fn.logical;
34
35 import java.util.Collection JavaDoc;
36 import java.util.Iterator JavaDoc;
37 import java.util.ArrayList JavaDoc;
38 import net.sf.jga.fn.UnaryFunctor;
39 import net.sf.jga.fn.UnaryPredicate;
40
41 /**
42  * Unary Predicate that returns true when one of 0 or more branch predicates
43  * returns true. When the collection of branch predicates is empty, an Any
44  * predicate will return false (somewhat arbitrarily). This predicate will
45  * short circuit: once one of the branches returns true, none of the
46  * subsequent branches will be evaluated.
47  * <p>
48  * The order of evaluation is dependant on the type of collection used: when
49  * using the default constructor, the collection used is a list, and branch
50  * predicates will be evaluated in the order given.
51  * <p>
52  * Copyright &copy; 2003-2005 David A. Hall
53  *
54  * @author <a HREF="mailto:davidahall@users.sourceforge.net">David A. Hall</a>
55  **/

56
57 public class Any<T> extends UnaryPredicate<T> {
58
59     static final long serialVersionUID = -3665390675036353724L;
60     
61     private Collection JavaDoc<UnaryFunctor<T,Boolean JavaDoc>> _branches;
62
63     /**
64      * Builds the Any predicate with an empty default collection of branch
65      * predicates. The default collection is a list, and branches will be
66      * evaluated in the order they are added.
67      */

68     public Any() {
69         _branches = new ArrayList JavaDoc<UnaryFunctor<T,Boolean JavaDoc>>();
70     }
71      
72     /**
73      * Builds the Any predicate with the given collection of branch predicates.
74      * More predicates may be added to the collection after construction. The
75      * order of evaluation of the branch predicates is determined by the
76      * collection in use.
77      */

78     public Any(Collection JavaDoc<UnaryFunctor<T,Boolean JavaDoc>> branches) {
79         _branches = (branches != null) ? branches : new ArrayList JavaDoc<UnaryFunctor<T,Boolean JavaDoc>>();
80     }
81      
82     /**
83      * Returns an Iterator over the branch predicates.
84      * @return an Iterator over the branch predicates
85      */

86     public Iterator JavaDoc<UnaryFunctor<T,Boolean JavaDoc>> branches() {
87         return _branches.iterator();
88     }
89
90     // UnaryPredicate interface
91

92     /**
93      * Given arguments <b>x</b>, returns true if at least one branch predicate
94      * returns true when given x. Returns false when there are no branch
95      * predicates.
96      *
97      * @return true if one branch predicates return true, false otherwise
98      */

99     public Boolean JavaDoc fn(T x) {
100         for (UnaryFunctor<T,Boolean JavaDoc> pred : _branches) {
101             if (pred.fn(x))
102                 return true;
103         }
104         
105         return false;
106     }
107     
108     /**
109      * Calls the Visitor's <code>visit(Any)</code> method, if it
110      * implements the nested Visitor interface.
111      */

112     public void accept(net.sf.jga.fn.Visitor v) {
113         if (v instanceof Any.Visitor)
114             ((Any.Visitor)v).visit(this);
115         else
116             v.visit(this);
117     }
118
119     // Object overrides
120

121     public String JavaDoc toString() {
122         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("Any(");
123         String JavaDoc sep = "";
124         for (UnaryFunctor<T,Boolean JavaDoc> pred : _branches) {
125             buf.append(sep).append(pred);
126             sep=",";
127         }
128         buf.append(")");
129         return buf.toString();
130     }
131     
132     // AcyclicVisitor
133

134     /**
135      * Interface for classes that may interpret an <b>Any</b> predicate.
136      */

137     public interface Visitor extends net.sf.jga.fn.Visitor {
138         public void visit(Any host);
139     }
140 }
141
Popular Tags