[JAVA] Inclusion judgment of elements of Collection

Table of Contents ⇒ Java Algorithm Library-Artery-Sample

Q01_15.java


package jp.avaj.lib.algo;

import java.util.Collection;
import java.util.List;

import jp.avaj.lib.test.ArTest;
import jp.avaj.lib.test.L;

/**
Inclusion judgment of Collection elements using ArValidator

-Judge the element with ArValidator and judge whether the Collection meets the following conditions.Each has its own ArValidator
・ There is only one element that satisfies the conditions.
    ⇒ ArValidatorCollectionContainsOnlyOne
・ There is one or more elements that satisfy the conditions
    ⇒ ArValidatorCollectionContains
・ All elements satisfy the conditions.
    ⇒ ArValidatorCollectionContainsAll
・ There is no element that satisfies the conditions.
    ⇒ ArValidatorCollectionContainsNone

・ In this sample, List<String>The beginning of the element is"a"And at the end"z"Check if.

 */
public class Q01_15 {
  public static void main(String[] args) {
    //Start a test case, unnecessary if you don't need aggregation
    ArTest.startTestCase("Q01_15");

    //test data
    List<String> listZero = ArList.construct("abc0,abc1,abc2,abc3");
    List<String> listOne = ArList.construct("abc0,abc1,abc2,abc3z");
    List<String> listTwo = ArList.construct("abc0,abc1z,abc2,abc3z");
    List<String> listAll = ArList.construct("abc0z,abc1z,abc2z,abc3z");

    //ArValidator for individual elements
    ArValidator<String> itemValidator = new ArValidator<String>() {
      @Override
      public boolean check(String value) {
        return value.startsWith("a") && value.endsWith("z");
      }
    };

    boolean result;
    //ArValidator to check the entire List
    ArValidator<Collection<String>> validator;

    L.p("There is only one element that satisfies the condition");
    {
      validator = new ArValidatorCollectionContainsOnlyOne<String>(itemValidator);
      //
      result = validator.check(listZero);
      ArTest.isFalse("listZero","result",result);
      //
      result = validator.check(listOne);
      ArTest.isTrue("listOne","result",result);
      //
      result = validator.check(listTwo);
      ArTest.isFalse("listTwo","result",result);
      //
      result = validator.check(listAll);
      ArTest.isFalse("listAll","result",result);
    }
    L.p("There is one or more elements that satisfy the conditions");
    {
      validator = new ArValidatorCollectionContains<String>(itemValidator);
      //
      result = validator.check(listZero);
      ArTest.isFalse("listZero","result",result);
      //
      result = validator.check(listOne);
      ArTest.isTrue("listOne","result",result);
      //
      result = validator.check(listTwo);
      ArTest.isTrue("listTwo","result",result);
      //
      result = validator.check(listAll);
      ArTest.isTrue("listAll","result",result);
    }
    L.p("All elements satisfy the conditions");
    {
      validator = new ArValidatorCollectionContainsAll<String>(itemValidator);
      //
      result = validator.check(listZero);
      ArTest.isFalse("listZero","result",result);
      //
      result = validator.check(listOne);
      ArTest.isFalse("listOne","result",result);
      //
      result = validator.check(listTwo);
      ArTest.isFalse("listTwo","result",result);
      //
      result = validator.check(listAll);
      ArTest.isTrue("listAll","result",result);
    }
    L.p("There is no element that satisfies the condition");
    {
      validator = new ArValidatorCollectionContainsNone<String>(itemValidator);
      //
      result = validator.check(listZero);
      ArTest.isTrue("listZero","result",result);
      //
      result = validator.check(listOne);
      ArTest.isFalse("listOne","result",result);
      //
      result = validator.check(listTwo);
      ArTest.isFalse("listTwo","result",result);
      //
      result = validator.check(listAll);
      ArTest.isFalse("listAll","result",result);
    }
    //
    //End test case, unnecessary if you don't need aggregation
    ArTest.endTestCase();
  }
}

The result is as follows

result.txt


**** Q01_15 start ****
There is only one element that satisfies the condition
OK listZero:result=false
OK listOne:result=true
OK listTwo:result=false
OK listAll:result=false
There is one or more elements that satisfy the conditions
OK listZero:result=false
OK listOne:result=true
OK listTwo:result=true
OK listAll:result=true
All elements satisfy the conditions
OK listZero:result=false
OK listOne:result=false
OK listTwo:result=false
OK listAll:result=true
There is no element that satisfies the condition
OK listZero:result=true
OK listOne:result=false
OK listTwo:result=false
OK listAll:result=false
**** Q01_15 summary ****
test count = 16
success    = 16

Recommended Posts

Inclusion judgment of elements of Collection
Expired collection of java
Judgment of the calendar
Judgment of JSONArray and JSONObject
[Java] Comparator of Collection class
Android library collection of "Mercariatte"
Judgment of fractions in Ruby
[Java] Delete the elements of List
First launch judgment of Android application
[Apache POI] Judgment of unnecessary cells
[Java] Judgment of identity and equivalence