List of usage examples for java.util.function Predicate test
boolean test(T t);
From source file:com.vmware.photon.controller.common.dcp.ServiceHostUtils.java
/** * Generic wait function.//from w w w. j a v a2 s . c o m */ public static <T> T waitForState(Supplier<T> supplier, Predicate<T> predicate, long waitIterationSleep, long waitIterationCount, Runnable cleanup) throws Throwable { for (int i = 0; i < waitIterationCount; i++) { T t = supplier.get(); if (predicate.test(t)) { return t; } Thread.sleep(waitIterationSleep); } if (cleanup != null) { cleanup.run(); } throw new TimeoutException("timeout waiting for state transition."); }
From source file:org.briljantframework.data.vector.Vectors.java
/** * Finds the index of the first value for which {@code predicate} returns true. * * @param vector the vector/* w w w . j av a2 s . c o m*/ * @param predicate the predicate * @return the index or {@code -1} if no value matched the predicate {@code true} */ public static <T> int find(Class<T> cls, Vector vector, Predicate<T> predicate) { for (int i = 0; i < vector.size(); i++) { if (predicate.test(vector.loc().get(cls, i))) { return i; } } return -1; }
From source file:com.vmware.photon.controller.common.dcp.ServiceHostUtils.java
/** * Wait for a query to returns particular information. */// w ww .java 2s . c o m public static QueryTask waitForQuery(ServiceHost host, String referrer, QueryTask query, Predicate<QueryTask> predicate, long waitIterationCount, long waitIterationSleep) throws Throwable { for (int i = 0; i < waitIterationCount; i++) { QueryTask result = sendQueryAndWait(host, referrer, query); if (predicate.test(result)) { return result; } Thread.sleep(waitIterationSleep); } throw new RuntimeException("timeout waiting for query result."); }
From source file:cn.lambdalib.util.mc.Raytrace.java
public static MovingObjectPosition rayTraceEntities(World world, Vec3 vec1, Vec3 vec2, Predicate<Entity> selector) { Entity entity = null;//from w w w .ja v a 2s. c o m AxisAlignedBB boundingBox = WorldUtils.getBoundingBox(vec1, vec2); List list = world.getEntitiesWithinAABBExcludingEntity(null, boundingBox.expand(1.0D, 1.0D, 1.0D), EntitySelectors.toEntitySelector(selector)); double d0 = 0.0D; for (int j = 0; j < list.size(); ++j) { Entity entity1 = (Entity) list.get(j); if (!entity1.canBeCollidedWith() || (selector != null && !selector.test(entity1))) continue; float f = 0.3F; AxisAlignedBB axisalignedbb = entity1.boundingBox.expand(f, f, f); MovingObjectPosition movingobjectposition1 = axisalignedbb.calculateIntercept(vec1, vec2); if (movingobjectposition1 != null) { double d1 = vec1.distanceTo(movingobjectposition1.hitVec); if (d1 < d0 || d0 == 0.0D) { entity = entity1; d0 = d1; } } } if (entity != null) { return new MovingObjectPosition(entity); } return null; }
From source file:fr.landel.utils.commons.ObjectUtils.java
/** * Returns a default value if the object doesn't match the predicate. * /*from w ww .jav a2 s.c o m*/ * <pre> * ObjectUtils.defaultIf(Objects::nonNull, null, null) = null * ObjectUtils.defaultIf(Objects::nonNull, null, "") = "" * ObjectUtils.defaultIf(Objects::nonNull, null, "zz") = "zz" * ObjectUtils.defaultIf(Objects::nonNull, "abc", *) = "abc" * ObjectUtils.defaultIf(Objects::nonNull, Boolean.TRUE, *) = Boolean.TRUE * </pre> * * @param predicate * the predicate (cannot be {@code null}) * @param object * the {@code Object} to test, may be {@code null} * @param defaultValue * the default value, may be {@code null} * @param <T> * the type of the object * @return {@code object} if it matches the predicate, defaultValue * otherwise * @throws NullPointerException * if {@code predicate} is {@code null} */ public static <T> T defaultIf(final Predicate<T> predicate, final T object, final T defaultValue) { Objects.requireNonNull(predicate, PREDICATE_ERROR); return predicate.test(object) ? object : defaultValue; }
From source file:org.jsweet.input.typescriptdef.TypescriptDef2Java.java
private static void addTsDefFiles(File input, ArrayList<File> files, Predicate<File> libFilter) { if (input.isDirectory()) { File[] subFiles = input.listFiles(); if (subFiles != null) { for (File file : subFiles) { addTsDefFiles(file, files, libFilter); }//from w w w. ja v a 2s . c om } } else { if (input.getName().endsWith(".d.ts") && !input.getPath().contains("legacy") && libFilter.test(input)) { files.add(input); } } }
From source file:password.pwm.util.java.JavaHelper.java
public static long copyWhilePredicate(final InputStream input, final OutputStream output, final Predicate predicate) throws IOException { final int bufferSize = 4 * 1024; final byte[] buffer = new byte[bufferSize]; long bytesCopied; long totalCopied = 0; do {//from ww w . j av a2 s . c o m bytesCopied = IOUtils.copyLarge(input, output, 0, bufferSize, buffer); if (bytesCopied > 0) { totalCopied += bytesCopied; } if (!predicate.test(null)) { return totalCopied; } } while (bytesCopied > 0); return totalCopied; }
From source file:org.jbpm.workbench.ht.client.editors.taskslist.AbstractTaskListPresenterTest.java
protected static void testTaskStatusCondition(Predicate<TaskSummary> predicate, String... validStatutes) { List<String> allStatus = TaskUtils.getStatusByType(TaskType.ALL); final List<String> validStatuses = Arrays.asList(validStatutes); allStatus.removeAll(validStatuses);//from w w w . j a v a2 s.c o m allStatus.forEach(s -> assertFalse(predicate.test(TaskSummary.builder().status(s).build()))); validStatuses.forEach(s -> assertTrue(predicate.test(TaskSummary.builder().status(s).build()))); }
From source file:alfio.util.EventUtil.java
public static boolean checkWaitingQueuePreconditions(Event event, List<SaleableTicketCategory> categories, ConfigurationManager configurationManager, Predicate<Event> noTicketsAvailable) { return findLastCategory(categories).map(lastCategory -> { ZonedDateTime now = ZonedDateTime.now(event.getZoneId()); if (isPreSales(event, categories)) { return configurationManager.getBooleanConfigValue( Configuration.from(event.getOrganizationId(), event.getId(), ENABLE_PRE_REGISTRATION), false);// w w w.ja v a 2 s. co m } else if (configurationManager.getBooleanConfigValue( Configuration.from(event.getOrganizationId(), event.getId(), ENABLE_WAITING_QUEUE), false)) { return now.isBefore(lastCategory.getZonedExpiration()) && noTicketsAvailable.test(event); } return false; }).orElse(false); }
From source file:fr.landel.utils.assertor.utils.AssertorIterable.java
private static <I extends Iterable<T>, T> boolean has(final I iterable, final Predicate<T> predicate, final boolean all, final EnumAnalysisMode analysisMode) { if (EnumAnalysisMode.STANDARD.equals(analysisMode)) { if (all) { for (final T object : iterable) { if (!predicate.test(object)) { return false; }/*from ww w .j a v a2s.com*/ } return true; } else { for (final T object : iterable) { if (predicate.test(object)) { return true; } } return false; } } else { final Stream<T> stream = StreamSupport.stream(iterable.spliterator(), EnumAnalysisMode.PARALLEL.equals(analysisMode)); if (all) { return stream.allMatch(predicate); } else { return stream.anyMatch(predicate); } } }