Maybe a new container: Sieve

忘骑象背 165053593 at qq.com
Mon Sep 21 15:51:55 UTC 2020


Dear all,


Thank you for your great job about the openjdk.


I have an idea about a new container, Sieve(I give it the name).


The java file is something like this:
package com.heyifei.studyandshare.utils;

/**
 * @author: 何毅斐(heyifei)
 * @email: heyifei88 at foxmail.com
 * @address: zhangjiang, pudong new district, shanghai, china
 * @date: 2020/9/21
 * @description:
 */

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

/**
 * Container:Sieve
 */
public class Sieve implements Serializable {

  private static final long serialVersionUID = 8621987070720161207L;

  /**
   * items in the sieve.
   */
  final List<Object> inSieveItems = new ArrayList<>();

  /**
   * items out of the sieve,coproduct.
   */
  final List<Object> outSieveItems = new ArrayList<>();

  /**
   * predict like the hole on the sieve
   */
  private Predicate<Object> predicate = null;

  public Sieve(){}

  public Sieve(Predicate<Object> predicate){
    if(predicate == null){
      throw new NullPointerException();
    }
    this.predicate = predicate;
  }

  public boolean put(Object o){
    boolean inSieve;
    if(predicate.test(o)){
      inSieveItems.add(o);
      inSieve = true;
    }else{
      outSieveItems.add(o);
      inSieve = false;
    }
    return inSieve;
  }

  public Object take(){
    return inSieveItems;
  }

  public Object getCoproduct(){
    return outSieveItems;
  }

  public int count(){
    return inSieveItems.size();
  }

  public int getCoproductCount(){
    return outSieveItems.size();
  }

}

Thank you.


More information about the core-libs-dev mailing list