Java Code Examples for java.util.function.BiPredicate#test()

The following examples show how to use java.util.function.BiPredicate#test() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MapNodes.java    From bifurcan with MIT License 6 votes vote down vote up
@Override
public boolean equals(INode<K, V> o, BiPredicate<K, K> keyEquals, BiPredicate<V, V> valEquals) {

  if (this == o) {
    return true;
  }

  if (o instanceof Collision) {
    Collision<K, V> c = (Collision<K, V>) o;
    if (c.size() == size()) {

      Iterator<IEntry<K, V>> it = entries().iterator();
      while (it.hasNext()) {
        IEntry<K, V> e = it.next();
        int idx = c.indexOf(e.key(), keyEquals);
        if (idx < 0 || !valEquals.test(e.value(), (V) entries[idx + 1])) {
          return false;
        }
      }
      return true;
    }
  }

  return false;
}
 
Example 2
Source File: KafkaStream.java    From arcusplatform with Apache License 2.0 6 votes vote down vote up
public KafkaStream<K, V> whileMatches(BiPredicate<? super K, ? super V> predicate) {
   BiPredicate<? super K, ? super V> keepGoing;
   if(this.keepGoing.isPresent()) {
      final BiPredicate<? super K, ? super V> delegate = this.keepGoing.get();
      keepGoing = (k, v) -> delegate.test(k, v) && predicate.test(k, v);
   }
   else {
      keepGoing = predicate;
   }

   return new KafkaStream<K, V>(
         config,
         keyDeserializer,
         valueDeserializer,
         Optional.of(keepGoing),
         filter
   );
}
 
Example 3
Source File: JarUtils.java    From CrossMobile with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static void unzipJar(File injar, File outdir, BiPredicate<JarEntry, File> predicate) {
    if (injar == null || !injar.isFile())
        throw new RuntimeException("Input JAR file " + injar + " does not exist");
    outdir.mkdirs();
    if (!outdir.isDirectory())
        throw new RuntimeException("Output file " + outdir + " is not a directory");
    try (JarFile jarfile = new JarFile(injar)) {
        Enumeration<JarEntry> entries = jarfile.entries();
        while (entries.hasMoreElements()) {
            JarEntry entry = entries.nextElement();
            File outfile = new File(outdir, entry.getName());
            if (!entry.isDirectory() && (predicate == null || predicate.test(entry, outfile))) {
                outfile.getParentFile().mkdirs();
                FileUtils.copyStream(jarfile.getInputStream(entry), new FileOutputStream(outfile));
                outfile.setLastModified(entry.getTime());
            }
        }
    } catch (IOException ex) {
        Log.error("Unable to extract JAR file " + injar + " into " + outdir);
        BaseUtils.throwException(ex);
    }
}
 
Example 4
Source File: BlasTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * Creates dense local on-heap matrix from vector v entities filtered by bipredicate p.
 *
 * @param v Vector with entities for matrix to be created.
 * @param rows Rows number in the target matrix.
 * @param acsMode column or row major mode.
 * @param p bipredicate to filter entities by.
 * @return Dense local on-heap matrix.
 */
private static DenseMatrix fromVector(DenseVector v, int rows, int acsMode,
    BiPredicate<Integer, Integer> p) {
    double[] data = v.getStorage().data();
    int cols = data.length / rows;
    double[] d = new double[data.length];

    int iLim = acsMode == StorageConstants.ROW_STORAGE_MODE ? rows : cols;
    int jLim = acsMode == StorageConstants.ROW_STORAGE_MODE ? cols : rows;

    int shift = 0;

    for (int i = 0; i < iLim; i++)
        for (int j = 0; j < jLim; j++) {
            int ind = i * jLim + j;

            if (!p.test(i, j)) {
                shift++;
                d[ind] = 0.0;
                continue;
            }
            d[ind] = data[ind - shift];
        }

    return new DenseMatrix(d, rows, acsMode);
}
 
Example 5
Source File: TreeComparison.java    From durian with Apache License 2.0 6 votes vote down vote up
/** Recursively determines equality between two trees. */
private static <E, A> boolean equals(TreeDef<E> expectedDef, E expectedRoot, TreeDef<A> actualDef, A actualRoot, BiPredicate<? super E, ? super A> compareFunc) {
	// compare the roots
	if (!compareFunc.test(expectedRoot, actualRoot)) {
		return false;
	}
	// compare the children lists
	List<E> expectedChildren = expectedDef.childrenOf(expectedRoot);
	List<A> actualChildren = actualDef.childrenOf(actualRoot);
	if (expectedChildren.size() != actualChildren.size()) {
		return false;
	}
	// recurse on each pair of children
	for (int i = 0; i < expectedChildren.size(); ++i) {
		E expectedChild = expectedChildren.get(i);
		A actualChild = actualChildren.get(i);
		if (!equals(expectedDef, expectedChild, actualDef, actualChild, compareFunc)) {
			return false;
		}
	}
	return true;
}
 
Example 6
Source File: UIDecorator.java    From consulo with Apache License 2.0 6 votes vote down vote up
static <ARG, U extends UIDecorator> void apply(BiPredicate<U, ARG> predicate, ARG arg, Class<U> clazz) {
  for (UIDecorator decorator : UIDecorators.getDecorators()) {
    if (!decorator.isAvaliable()) {
      continue;
    }

    U u = ObjectUtil.tryCast(decorator, clazz);
    if (u == null) {
      continue;
    }

    if (predicate.test(u, arg)) {
      break;
    }
  }
}
 
Example 7
Source File: Handler.java    From springsandwich with MIT License 5 votes vote down vote up
public default boolean hasFlag(String[] flags, String flag, BiPredicate<String, String> predicate) {
	if(flags == null || flag == null) return false;
	
	for(int i=0; i<flags.length; i++) {
		String f = flags[0];
		if(f != null && predicate.test(f, flag)) {
			return true;
		}
	}
	return false;		
}
 
Example 8
Source File: StringFilters.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
default Selection eval(BiPredicate<String, String> predicate, Column<String> otherColumn) {
  Selection selection = new BitmapBackedSelection();
  for (int idx = 0; idx < size(); idx++) {
    if (predicate.test(get(idx), otherColumn.get(idx))) {
      selection.add(idx);
    }
  }
  return selection;
}
 
Example 9
Source File: CopyOnWriteAsyncContextMap.java    From servicetalk with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public Key<?> forEach(final BiPredicate<Key<?>, Object> consumer) {
    if (!consumer.test(keyOne, valueOne)) {
        return keyOne;
    }
    return consumer.test(keyTwo, valueTwo) ? null : keyTwo;
}
 
Example 10
Source File: HashImpl.java    From icefig with Apache License 2.0 5 votes vote down vote up
@Override
public MutableHash<K, V> rejectInPlace(BiPredicate<K, V> condition) {
    Objects.requireNonNull(condition);
    final Iterator<Map.Entry<K, V>> each = hash.entrySet().iterator();
    while (each.hasNext()) {
        Map.Entry<K, V> nextEntry = each.next();
        if (condition.test(nextEntry.getKey(), nextEntry.getValue())) {
            each.remove();
        }
    }
    return this;
}
 
Example 11
Source File: Equi.java    From huntbugs with Apache License 2.0 5 votes vote down vote up
private static boolean equiLambdas(Lambda left, Lambda right, EquiContext ctx) {
    if(!equiMethods(left.getMethod(), right.getMethod())
            || !equiTypes(left.getFunctionType(), right.getFunctionType()))
        return false;
    MethodDefinition leftMd = Nodes.getLambdaMethod(left);
    MethodDefinition rightMd = Nodes.getLambdaMethod(right);
    BiPredicate<Variable, Variable> curMatcher = ctx.varMatcher;
    ctx.varMatcher = (vl, vr) -> {
        if(curMatcher.test(vl, vr))
            return true;
        VariableDefinition vlo = vl.getOriginalVariable();
        VariableDefinition vro = vr.getOriginalVariable();
        if(vlo != null && vro != null) {
            if(Variables.getMethodDefinition(vlo) == leftMd && Variables.getMethodDefinition(vro) == rightMd) {
                return vlo.getVariableType().isEquivalentTo(vro.getVariableType()) && vlo.getSlot() == vro.getSlot();
            }
        }
        ParameterDefinition pl = vl.getOriginalParameter();
        ParameterDefinition pr = vr.getOriginalParameter();
        if(pl != null && pr != null && pl.getMethod() == leftMd && pr.getMethod() == rightMd) {
            return pl.getPosition() == pr.getPosition();
        }
        return false;
    };
    boolean result = equiBlocks(left.getBody(), right.getBody(), ctx);
    ctx.varMatcher = curMatcher;
    return result;
}
 
Example 12
Source File: StringFilters.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
default Selection eval(BiPredicate<String, Integer> predicate, Integer value) {
  Selection selection = new BitmapBackedSelection();
  for (int idx = 0; idx < size(); idx++) {
    if (predicate.test(get(idx), value)) {
      selection.add(idx);
    }
  }
  return selection;
}
 
Example 13
Source File: IBiPredicate.java    From ph-commons with Apache License 2.0 5 votes vote down vote up
@Nullable
static <T, U> IBiPredicate <T, U> or (@Nullable final BiPredicate <? super T, ? super U> aFirst,
                                      @Nullable final BiPredicate <? super T, ? super U> aSecond)
{
  if (aFirst != null)
  {
    if (aSecond != null)
      return (x, y) -> aFirst.test (x, y) || aSecond.test (x, y);
    return aFirst::test;
  }
  if (aSecond != null)
    return aSecond::test;
  return null;
}
 
Example 14
Source File: BossBarImpl.java    From adventure with MIT License 5 votes vote down vote up
private @NonNull BossBar editFlags(final @NonNull Flag@NonNull[] flags, final @NonNull BiPredicate<Set<Flag>, Flag> predicate) {
  if(flags.length == 0) return this;
  final Set<Flag> oldFlags = new HashSet<>(this.flags);
  boolean changed = false;
  for(int i = 0, length = flags.length; i < length; i++) {
    if(predicate.test(this.flags, flags[i])) {
      changed = true;
    }
  }
  if(changed) {
    this.forEachListener(listener -> listener.bossBarFlagsChanged(this, oldFlags, this.flags));
  }
  return this;
}
 
Example 15
Source File: AnnotationsScanner.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private static <C> boolean isFiltered(
		Class<?> sourceClass, C context, @Nullable BiPredicate<C, Class<?>> classFilter) {

	return (classFilter != null && classFilter.test(context, sourceClass));
}
 
Example 16
Source File: CollectionHelper.java    From customstuff4 with GNU General Public License v3.0 4 votes vote down vote up
public static <T, U> boolean equalsWithoutOrder(List<T> fst, List<U> snd, BiPredicate<T, U> equality)
{
    if (fst != null && snd != null)
    {
        if (fst.size() == snd.size())
        {
            // create copied lists so the original list is not modified
            List<T> cfst = new ArrayList<>(fst);
            List<U> csnd = new ArrayList<>(snd);

            Iterator<T> ifst = cfst.iterator();
            boolean foundEqualObject;
            while (ifst.hasNext())
            {
                T a = ifst.next();
                Iterator<U> isnd = csnd.iterator();
                foundEqualObject = false;
                while (isnd.hasNext())
                {
                    U b = isnd.next();
                    if (equality.test(a, b))
                    {
                        ifst.remove();
                        isnd.remove();
                        foundEqualObject = true;
                        break;
                    }
                }

                if (!foundEqualObject)
                {
                    // fail early
                    break;
                }
            }
            if (cfst.isEmpty())
            { //both temporary lists have the same size
                return true;
            }
        }
    } else if (fst == null && snd == null)
    {
        return true;
    }
    return false;
}
 
Example 17
Source File: SVReadFilter.java    From gatk with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public Iterator<GATKRead> applyFilter( final Iterator<GATKRead> readItr, final BiPredicate<SVReadFilter, GATKRead> predicate ) {
    return new SVUtils.IteratorFilter<>(readItr, read -> predicate.test(this, read));
}
 
Example 18
Source File: MapNodes.java    From bifurcan with MIT License 4 votes vote down vote up
public static <K, V> Node<K, V> difference(int shift, Object editor, Node<K, V> a, Node<K, V> b, BiPredicate<K, K> equals) {
  Node<K, V> result = new Node<K, V>(editor);

  INode<K, V> n;
  int idx;
  PrimitiveIterator.OfInt masks = Util.masks(a.nodemap | a.datamap);
  while (masks.hasNext()) {
    int mask = masks.nextInt();
    int state = mergeState(mask, a.nodemap, a.datamap, b.nodemap, b.datamap);
    switch (state) {
      case NODE_NONE:
        result = transferNode(mask, a, result);
        break;
      case ENTRY_NONE:
        result = transferEntry(mask, a, result);
        break;
      case ENTRY_ENTRY:
        int ia = a.entryIndex(mask);
        int ib = b.entryIndex(mask);
        if (b.hashes[ib] != a.hashes[ia] || !equals.test((K) b.content[ib << 1], (K) a.content[ia << 1])) {
          result = transferEntry(mask, a, result);
        }
        break;
      case NODE_NODE:
        n = diffNodes(shift + 5, editor, a.node(mask), b.node(mask), equals);
        if (n != null) {
          result = result.putNode(mask, n);
        }
        break;
      case NODE_ENTRY:
        idx = b.entryIndex(mask);
        n = a.node(mask).remove(shift + 5, editor, b.hashes[idx], (K) b.content[idx << 1], equals);
        if (n.size() > 0) {
          result = result.putNode(mask, n);
        }
        break;
      case ENTRY_NODE:
        idx = a.entryIndex(mask);
        if (get(b, shift, a.hashes[idx], (K) a.content[idx << 1], equals, DEFAULT_VALUE) == DEFAULT_VALUE) {
          result = transferEntry(mask, a, result);
        }
        break;
      default:
        throw new IllegalStateException();
    }
  }

  return result.size() > 0 ? result : null;
}
 
Example 19
Source File: MapNodes.java    From bifurcan with MIT License 4 votes vote down vote up
public static <K, V> Node<K, V> intersection(int shift, Object editor, Node<K, V> a, Node<K, V> b, BiPredicate<K, K> equals) {
  Node<K, V> result = new Node<K, V>(editor);

  PrimitiveIterator.OfInt masks = Util.masks((a.nodemap | a.datamap) & (b.nodemap | b.datamap));
  while (masks.hasNext()) {
    int mask = masks.nextInt();
    int state = mergeState(mask, a.nodemap, a.datamap, b.nodemap, b.datamap);
    int idx, ia, ib;
    switch (state) {
      case ENTRY_ENTRY:
        ia = a.entryIndex(mask);
        ib = b.entryIndex(mask);
        if (b.hashes[ib] == a.hashes[ia] && equals.test((K) b.content[ib << 1], (K) a.content[ia << 1])) {
          result = transferEntry(mask, a, result);
        }
        break;
      case NODE_NODE:
        INode<K, V> n = intersectNodes(shift + 5, editor, a.node(mask), b.node(mask), equals);
        if (n != null) {
          result = result.putNode(mask, n);
        }
        break;
      case NODE_ENTRY:
        idx = b.entryIndex(mask);
        int hash = b.hashes[idx];
        K key = (K) b.content[idx << 1];
        Object val = get(a, shift, hash, key, equals, DEFAULT_VALUE);
        if (val != DEFAULT_VALUE) {
          result = result.put(shift, editor, hash, key, (V) val, equals, null);
        }
        break;
      case ENTRY_NODE:
        idx = a.entryIndex(mask);
        if (get(b, shift, a.hashes[idx], (K) a.content[idx << 1], equals, DEFAULT_VALUE) != DEFAULT_VALUE) {
          result = transferEntry(mask, a, result);
        }
        break;
      default:
        throw new IllegalStateException();
    }
  }

  return result.size() > 0 ? result : null;
}
 
Example 20
Source File: CopyOnWriteAsyncContextMap.java    From servicetalk with Apache License 2.0 4 votes vote down vote up
@Nullable
@Override
public Key<?> forEach(final BiPredicate<Key<?>, Object> consumer) {
    return consumer.test(key, value) ? null : key;
}