📄 forall.java
字号:
package org.jutil.java.collections;import java.util.Collection;import java.util.Iterator;/** * <p>A boolean for-all operator.</p> * * <center> * <img src="doc-files/ForAll.png"/> * </center> * * <p>A convenience accumulator of collections that checks whether all * elements of a collection satisfy the criterion defined in the abstract * method <code>public boolean criterion(Object element)</code>.</p> * * <p>As with <code>Accumulator</code>, this class can best be used as an anonymous * inner class.</p> *<pre><code> *boolean bool = * new ForAll() { * * /oo * o also public behavior * o * o post (* additional precondition for criterion method *) * o * o public model boolean isValidElement(Object element); * o/ * * /oo * o also public behavior * o * o post \result == ((MyType)element.someProperty() ...) * o/ * public boolean criterion(Object element) { * // criterion code * } * }.in(collection); *</code></pre> * * @path $Source: /cvsroot/org-jutil/jutil.org/src/org/jutil/java/collections/ForAll.java,v $ * @version $Revision: 1.19 $ * @date $Date: 2002/07/20 19:11:30 $ * @state $State: Exp $ * @author Marko van Dooren * @author Jan Dockx * @release $Name: $ */public abstract class ForAll extends BooleanAccumulator { public final static String CVS_REVISION="$Revision: 1.19 $"; /** * Check whether the given object satisfies the criterion * for this ForAll accumulator. */ /*@ @ public behavior @ @ pre isValidElement(element); @ @ // criterion should be consistent with equals; subclass can say more @ post (\forall Object o1;; @ (\forall Object o2; o2.equals(o1); criterion(o1) == criterion(o2))); @*/ public abstract boolean criterion(Object element); /*@ @ also public behavior @ @ post \result == true; @*/ final public boolean initialAccumulator() { return true; } /*@ @ also public behavior @ @ post \result == (acc && criterion(element)); @*/ final public boolean accumulate(Object element, boolean acc) { return (criterion(element) && acc); } /*@ @ also public behavior @ @ post \result == (\forall Object o; collection.contains(o); criterion(o)); @*/ public boolean in(Collection collection) { boolean acc = true; if(collection != null) { Iterator iter = collection.iterator(); // variant : the number of elements in the explicit collection that // have not yet been checked. // invariant : acc == true if all previous elements in the explicit collection // satisfy the criterion. Otherwise, it is false. while (iter.hasNext() && acc) { acc = accumulate(iter.next(), acc); } } return acc; } }/*<copyright>Copyright (C) 1997-2001. This software is copyrighted by the people and entities mentioned after the "@author" tags above, on behalf of the JUTIL.ORG Project. The copyright is dated by the dates after the "@date" tags above. All rights reserved.This software is published under the terms of the JUTIL.ORG SoftwareLicense version 1.1 or later, a copy of which has been included withthis distribution in the LICENSE file, which can also be found athttp://org-jutil.sourceforge.net/LICENSE. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the JUTIL.ORG Software License for more details.For more information, please see http://org-jutil.sourceforge.net/</copyright>*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -