All Downloads are FREE. Search and download functionalities are using the official Maven repository.

gw.util.PredicatedSet Maven / Gradle / Ivy

There is a newer version: 1.18.2
Show newest version
/*
 * Copyright 2014 Guidewire Software, Inc.
 */

package gw.util;


import java.util.Collection;
import java.util.HashSet;

public class PredicatedSet extends HashSet {
  private final Predicate _predicate;

  public PredicatedSet(int initialCapacity, Predicate predicate) {
    super(initialCapacity);
    _predicate = predicate;
  }

  public PredicatedSet(Predicate predicate) {
    _predicate = predicate;
  }

  public boolean add(E o) {
    if (_predicate.evaluate(o)) {
      return super.add(o);
    }
    return false;
  }

  public boolean addAll(Collection c) {
    boolean changed = false;
    for(E e: c) {
      if (_predicate.evaluate(e)) {
        if (super.add(e)) {
          changed = true;
        }
      }
    }
    return changed;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy