testandfilter.java

来自「一个关于java 的常用工具包」· Java 代码 · 共 126 行

JAVA
126
字号
package org.jutiltest.java.collections;import org.jutil.java.collections.AndFilter;import org.jutil.junit.JutilTest;import org.jutil.junit.CVSRevision;import org.jutil.java.collections.Filter;import org.jutil.java.collections.Exists;import junit.framework.*;import java.util.Vector;import java.util.Collection;import java.util.Iterator;import java.util.Enumeration;import java.util.Set;import java.util.HashSet;import java.util.ArrayList;import org.jutil.java.collections.Collections; /*  * @path    $Source: /cvsroot/org-jutil/jutil.org/src/org/jutiltest/java/collections/TestAndFilter.java,v $ * @version $Revision: 1.10 $ * @date    $Date: 2002/07/21 18:59:16 $ * @state   $State: Exp $ * @author  Marko van Dooren * @release $Name:  $ */public class TestAndFilter extends JutilTest {  public TestAndFilter(String name) {    super(name, new CVSRevision("1.11"));  }    private Vector $intVector;  public void setUp() {    $intVector = new Vector();    for(int i=0; i<37; i++) {      $intVector.add(new Integer(i));    }  }    public void testFilter() {    Collection clone = new ArrayList($intVector);    Filter trueFilter = new Filter() {      public boolean criterion(Object element) {        return true;      }    };      Filter falseFilter = new Filter() {      public boolean criterion(Object element) {        return false;      }    };    Filter smallerThan30 = new Filter() {      public boolean criterion(Object element) {        return ((Integer)element).intValue()<30;      }    };    Filter biggerThan19 = new Filter() {      public boolean criterion(Object element) {        return ((Integer) element).intValue() >= 20;      }    };    Filter[] filters = new Filter[2];    filters[0] = smallerThan30;    filters[1] = biggerThan19;        new AndFilter(filters).retain($intVector);        boolean bool = new Exists() {      public boolean criterion(Object element) {        return ((Integer)element).intValue()>=30 || ((Integer)element).intValue()<20;      }    }.in($intVector);    assertTrue(! bool);    assertTrue($intVector.size() == 10);            // reset the set    setUp();    new AndFilter(filters).discard($intVector);    assertTrue($intVector.size() == 27);                // empty collections    new AndFilter(filters).retain(new HashSet());    new AndFilter(filters).discard(new Vector());        // collection containing only null references    Vector stupidVector=new Vector();    for(int i=0; i<100;i++) {      stupidVector.add(null);    }    Filter[] otherFilters = new Filter[2];    otherFilters[0]=falseFilter;    otherFilters[1]=trueFilter;        new AndFilter(otherFilters).retain(stupidVector);        assertTrue(stupidVector.size() == 0);    for(int i=0; i<100;i++) {      stupidVector.add(null);    }    otherFilters[1]=falseFilter;    new AndFilter(otherFilters).discard(stupidVector);    assertTrue(stupidVector.size() == 100);  }  } /* * <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 Software * License version 1.1 or later, a copy of which has been included with * this distribution in the LICENSE file, which can also be found at * http://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 + =
减小字号Ctrl + -
显示快捷键?