/** * Validate the predicates to ensure that all is well. * * @param predicates the predicates to validate * @return predicate array */ static <T> Predicate<? super T>[] validate(final Collection<? extends Predicate<? super T>> predicates) { if (predicates == null) { throw new NullPointerException("The predicate collection must not be null"); } // convert to array like this to guarantee iterator() ordering @SuppressWarnings("unchecked") // OK final Predicate<? super T>[] preds = new Predicate[predicates.size()]; int i = 0; for (final Predicate<? super T> predicate : predicates) { preds[i] = predicate; if (preds[i] == null) { throw new NullPointerException( "The predicate collection must not contain a null predicate, index " + i + " was null"); } i++; } return preds; }
private <T extends Abi.Entry> T find(Class<T> resultClass, final Abi.Entry.Type type, final Predicate<T> searchPredicate) { return (T) CollectionUtils.find(this, new Predicate<Abi.Entry>() { @Override public boolean evaluate(Abi.Entry entry) { return entry.type == type && searchPredicate.evaluate((T) entry); } }); }
private List<Param> filteredInputs(final boolean indexed) { return select(inputs, new Predicate<Param>() { @Override public boolean evaluate(Param param) { return param.indexed == indexed; } }); }
public boolean checkMoment(Date date) { if (calendar != null) { Period period = new Period(new DateTime(date), new Dur(0, 0, 0, 0)); Predicate<CalendarComponent> periodRule = new PeriodRule<>(period); Filter<CalendarComponent> filter = new Filter<>(new Predicate[] {periodRule}, Filter.MATCH_ANY); Collection<CalendarComponent> events = filter.filter(calendar.getComponents(CalendarComponent.VEVENT)); if (events != null && !events.isEmpty()) { return true; } } return false; }
/** * Factory method that performs validation. * * @param <E> the type that the closure acts on * @param predicate predicate to switch on * @param trueClosure closure used if true * @param falseClosure closure used if false * @return the <code>if</code> closure * @throws NullPointerException if any argument is null */ public static <E> Closure<E> ifClosure(final Predicate<? super E> predicate, final Closure<? super E> trueClosure, final Closure<? super E> falseClosure) { if (predicate == null) { throw new NullPointerException("Predicate must not be null"); } if (trueClosure == null || falseClosure == null) { throw new NullPointerException("Closures must not be null"); } return new IfClosure<E>(predicate, trueClosure, falseClosure); }
/** * Factory to create the comparator predicate * * @param <T> the type that the predicate queries * @param object the object to compare to * @param comparator the comparator to use for comparison * @param criterion the criterion to use to evaluate comparison * @return the predicate * @throws NullPointerException if comparator or criterion is null */ public static <T> Predicate<T> comparatorPredicate(final T object, final Comparator<T> comparator, final Criterion criterion) { if (comparator == null) { throw new NullPointerException("Comparator must not be null."); } if (criterion == null) { throw new NullPointerException("Criterion must not be null."); } return new ComparatorPredicate<T>(object, comparator, criterion); }
/** * Evaluates the predicate returning true if only one decorated predicate * returns true. * * @param object the input object * @return true if only one decorated predicate returns true */ public boolean evaluate(final T object) { boolean match = false; for (final Predicate<? super T> iPredicate : iPredicates) { if (iPredicate.evaluate(object)) { if (match) { return false; } match = true; } } return match; }
/** * Factory method that performs validation. * * @param <I> input type for the transformer * @param <O> output type for the transformer * @param predicate predicate to switch on * @param trueTransformer transformer used if true * @param falseTransformer transformer used if false * @return the <code>if</code> transformer * @throws NullPointerException if either argument is null */ public static <I, O> Transformer<I, O> ifTransformer(final Predicate<? super I> predicate, final Transformer<? super I, ? extends O> trueTransformer, final Transformer<? super I, ? extends O> falseTransformer) { if (predicate == null) { throw new NullPointerException("Predicate must not be null"); } if (trueTransformer == null || falseTransformer == null) { throw new NullPointerException("Transformers must not be null"); } return new IfTransformer<I, O>(predicate, trueTransformer, falseTransformer); }
/** * Factory method that performs validation. * <p> * This factory creates a transformer that just returns the input object when * the predicate is false. * * @param <T> input and output type for the transformer * @param predicate predicate to switch on * @param trueTransformer transformer used if true * @return the <code>if</code> transformer * @throws NullPointerException if either argument is null */ public static <T> Transformer<T, T> ifTransformer( final Predicate<? super T> predicate, final Transformer<? super T, ? extends T> trueTransformer) { if (predicate == null) { throw new NullPointerException("Predicate must not be null"); } if (trueTransformer == null) { throw new NullPointerException("Transformer must not be null"); } return new IfTransformer<T, T>(predicate, trueTransformer, NOPTransformer.<T>nopTransformer()); }
/** * Constructor that performs no validation. * Use the static factory method <code>ifTransformer</code> if you want that. * * @param predicate predicate to switch on, not null * @param trueTransformer transformer used if true, not null * @param falseTransformer transformer used if false, not null */ public IfTransformer(final Predicate<? super I> predicate, final Transformer<? super I, ? extends O> trueTransformer, final Transformer<? super I, ? extends O> falseTransformer) { super(); iPredicate = predicate; iTrueTransformer = trueTransformer; iFalseTransformer = falseTransformer; }
/** * Evaluates the predicate returning true if all predicates return true. * * @param object the input object * @return true if all decorated predicates return true */ public boolean evaluate(final T object) { for (final Predicate<? super T> iPredicate : iPredicates) { if (!iPredicate.evaluate(object)) { return false; } } return true; }
/** * Evaluates the predicate returning false if any stored predicate returns false. * * @param object the input object * @return true if none of decorated predicates return true */ public boolean evaluate(final T object) { for (final Predicate<? super T> iPredicate : iPredicates) { if (iPredicate.evaluate(object)) { return false; } } return true; }
/** * Evaluates the predicate returning true if any predicate returns true. * * @param object the input object * @return true if any decorated predicate return true */ public boolean evaluate(final T object) { for (final Predicate<? super T> iPredicate : iPredicates) { if (iPredicate.evaluate(object)) { return true; } } return false; }
/** * Constructs a PredicatedCollectionBuilder with the specified Predicate. * * @param predicate the predicate to use * @throws NullPointerException if predicate is null */ public Builder(final Predicate<? super E> predicate) { if (predicate == null) { throw new NullPointerException("Predicate must not be null"); } this.predicate = predicate; }
public Value findField(final String fieldName){ Value value; value = IterableUtils.find(this, new Predicate<Value>() { @Override public boolean evaluate(Value object) { return object.getMetadata().getName().equals(fieldName); } }); return value; }
/** * Factory to create the predicate. * * @param <T> the type that the predicate queries * @param transformer the transformer to call * @param predicate the predicate to call with the result of the transform * @return the predicate * @throws NullPointerException if the transformer or the predicate is null */ public static <T> Predicate<T> transformedPredicate(final Transformer<? super T, ? extends T> transformer, final Predicate<? super T> predicate) { if (transformer == null) { throw new NullPointerException("The transformer to call must not be null"); } if (predicate == null) { throw new NullPointerException("The predicate to call must not be null"); } return new TransformedPredicate<T>(transformer, predicate); }
/** * Factory method that performs validation and copies the parameter arrays. * * @param <E> the type that the closure acts on * @param predicates array of predicates, cloned, no nulls * @param closures matching array of closures, cloned, no nulls * @param defaultClosure the closure to use if no match, null means nop * @return the <code>chained</code> closure * @throws NullPointerException if array is null * @throws NullPointerException if any element in the array is null * @throws IllegalArgumentException if the array lengths of predicates and closures do not match */ @SuppressWarnings("unchecked") public static <E> Closure<E> switchClosure(final Predicate<? super E>[] predicates, final Closure<? super E>[] closures, final Closure<? super E> defaultClosure) { FunctorUtils.validate(predicates); FunctorUtils.validate(closures); if (predicates.length != closures.length) { throw new IllegalArgumentException("The predicate and closure arrays must be the same size"); } if (predicates.length == 0) { return (Closure<E>) (defaultClosure == null ? NOPClosure.<E>nopClosure(): defaultClosure); } return new SwitchClosure<E>(predicates, closures, defaultClosure); }
public Function findFunction(Predicate<Function> searchPredicate) { return find(Function.class, Abi.Entry.Type.function, searchPredicate); }
public Event findEvent(Predicate<Event> searchPredicate) { return find(Event.class, Abi.Entry.Type.event, searchPredicate); }
/** * Sets the predicate this the iterator to use. * * @param predicate the predicate to use */ public void setPredicate(final Predicate<? super E> predicate) { this.predicate = predicate; nextObject = null; nextObjectSet = false; }
public void syncDatabase(final DatabaseReader reader, DatabaseCounter counter, long lastSyncTimestamp, long currentSyncTimestamp) throws IOException { Database dbCurrentDatabase; Table dbCurrentTable; Record dbCurrentRecord; TableToSync currentTableToSync = null; Map<String, ValueMetadata> columns = null; String joinColumnName; ValueMetadata joinColumnMetadata; int elementType; Log.i(TAG, "start syncDatabase"); // TODO check database name try { // Open the TX mDB.beginTransaction(); while ((elementType = reader.nextElement()) != JSonDatabaseReader.END) { switch (elementType) { case JSonDatabaseReader.START_DB: dbCurrentDatabase = reader.readDatabase(); // Check schema version if (dbCurrentDatabase.getSchemaVersion() > mSchemaVersion) { throw new SyncException(SyncStatus.Code.ERROR_NEW_SCHEMA_VERSION, "Find new schema version, need to update (found:" + dbCurrentDatabase.getSchemaVersion() + ", expected:" + mSchemaVersion + ")"); } break; case JSonDatabaseReader.START_TABLE: // Read the table and column metadata dbCurrentTable = reader.readTable(); // Find the table to sync definition and rules final String tableName = dbCurrentTable.getName(); currentTableToSync = IterableUtils.find(mTableToSync, new Predicate<TableToSync>() { @Override public boolean evaluate(TableToSync object) { return object.getName().equals(tableName); } }); if (currentTableToSync == null) { throw new SyncException(SyncStatus.Code.ERROR_SYNC_COULD_DB, "Unable to find table " + tableName + " into table definition"); } columns = SqlLiteUtility.readTableMetadataAsMap(mDB, dbCurrentTable.getName()); // Add the records columns for join tables for (JoinTable joinTable : currentTableToSync.getJoinTable()) { joinColumnName = JOIN_COLUMN_PREFIX + joinTable.getJoinColumn(); joinColumnMetadata = new ValueMetadata(joinColumnName.toUpperCase(), ValueMetadata.TYPE_STRING); columns.put(joinColumnName, joinColumnMetadata); } break; case JSonDatabaseReader.RECORD: dbCurrentRecord = reader.readRecord(columns); // Found record sync single record if (!dbCurrentRecord.isEmpty()) { syncRecord(currentTableToSync, dbCurrentRecord, counter, lastSyncTimestamp, currentSyncTimestamp); } break; } } // Commit the TX mDB.setTransactionSuccessful(); } catch (Exception e) { throw e; } finally { mDB.endTransaction(); } Log.i(TAG, "end syncDatabase tables: " + counter.getTableSyncedCount() + " recUpdated:" + counter.getRecordUpdated() + " recInserted:" + counter.getRecordInserted()); }
/** * Constructor that wraps (not copies). * <p> * If there are any elements already in the collection being decorated, they * are validated. * * @param coll the collection to decorate, must not be null * @param predicate the predicate to use for validation, must not be null * @throws NullPointerException if collection or predicate is null * @throws IllegalArgumentException if the collection contains invalid elements */ protected PredicatedCollection(final Collection<E> coll, final Predicate<? super E> predicate) { super(coll); if (predicate == null) { throw new NullPointerException("Predicate must not be null."); } this.predicate = predicate; for (final E item : coll) { validate(item); } }
/** * Factory to create the predicate. * <p> * If the array is size zero, the predicate always returns false. * If the array is size one, then that predicate is returned. * * @param <T> the type that the predicate queries * @param predicates the predicates to check, cloned, not null * @return the <code>any</code> predicate * @throws NullPointerException if the predicates array is null * @throws NullPointerException if any predicate in the array is null */ @SuppressWarnings("unchecked") public static <T> Predicate<T> anyPredicate(final Predicate<? super T>... predicates) { FunctorUtils.validate(predicates); if (predicates.length == 0) { return FalsePredicate.<T>falsePredicate(); } if (predicates.length == 1) { return (Predicate<T>) predicates[0]; } return new AnyPredicate<T>(FunctorUtils.copy(predicates)); }
/** * Factory to create the predicate. * <p> * If the array is size zero, the predicate always returns true. * * @param <T> the type that the predicate queries * @param predicates the predicates to check, cloned, not null * @return the <code>any</code> predicate * @throws NullPointerException if the predicates array is null * @throws NullPointerException if any predicate in the array is null */ public static <T> Predicate<T> nonePredicate(final Predicate<? super T>... predicates) { FunctorUtils.validate(predicates); if (predicates.length == 0) { return TruePredicate.<T>truePredicate(); } return new NonePredicate<T>(FunctorUtils.copy(predicates)); }
/** * Factory to create the predicate. * * @param <T> the type that the predicate queries * @param transformer the transformer to decorate * @return the predicate * @throws NullPointerException if the transformer is null */ public static <T> Predicate<T> transformerPredicate(final Transformer<? super T, Boolean> transformer) { if (transformer == null) { throw new NullPointerException("The transformer to call must not be null"); } return new TransformerPredicate<T>(transformer); }
/** * Factory to create the predicate. * <p> * If the collection is size zero, the predicate always returns true. * * @param <T> the type that the predicate queries * @param predicates the predicates to check, cloned, not null * @return the <code>one</code> predicate * @throws NullPointerException if the predicates array is null * @throws NullPointerException if any predicate in the array is null */ public static <T> Predicate<T> nonePredicate(final Collection<? extends Predicate<? super T>> predicates) { final Predicate<? super T>[] preds = FunctorUtils.validate(predicates); if (preds.length == 0) { return TruePredicate.<T>truePredicate(); } return new NonePredicate<T>(preds); }
/** * Factory to create the predicate. * <p> * If the array is size zero, the predicate always returns false. * If the array is size one, then that predicate is returned. * * @param <T> the type that the predicate queries * @param predicates the predicates to check, cloned, not null * @return the <code>any</code> predicate * @throws NullPointerException if the predicates array is null * @throws NullPointerException if any predicate in the array is null */ @SuppressWarnings("unchecked") public static <T> Predicate<T> onePredicate(final Predicate<? super T>... predicates) { FunctorUtils.validate(predicates); if (predicates.length == 0) { return FalsePredicate.<T>falsePredicate(); } if (predicates.length == 1) { return (Predicate<T>) predicates[0]; } return new OnePredicate<T>(FunctorUtils.copy(predicates)); }
/** * Factory to create the predicate. * <p> * If the collection is size zero, the predicate always returns true. * If the collection is size one, then that predicate is returned. * * @param <T> the type that the predicate queries * @param predicates the predicates to check, cloned, not null * @return the <code>all</code> predicate * @throws NullPointerException if the predicates array is null * @throws NullPointerException if any predicate in the array is null */ public static <T> Predicate<T> allPredicate(final Collection<? extends Predicate<? super T>> predicates) { final Predicate<? super T>[] preds = validate(predicates); if (preds.length == 0) { return truePredicate(); } if (preds.length == 1) { return coerce(preds[0]); } return new AllPredicate<T>(preds); }
/** * Factory to create the predicate. * * @param <T> the type that the predicate queries * @param object the object to compare to * @return the predicate */ public static <T> Predicate<T> equalPredicate(final T object) { if (object == null) { return NullPredicate.nullPredicate(); } return new EqualPredicate<T>(object); }
/** * Factory to create the null true predicate. * * @param <T> the type that the predicate queries * @param predicate the predicate to decorate, not null * @return the predicate * @throws NullPointerException if the predicate is null */ public static <T> Predicate<T> nullIsTruePredicate(final Predicate<? super T> predicate) { if (predicate == null) { throw new NullPointerException("Predicate must not be null"); } return new NullIsTruePredicate<T>(predicate); }
/** * Constructor that performs no validation. * Use <code>ifClosure</code> if you want that. * * @param predicate predicate to switch on, not null * @param trueClosure closure used if true, not null * @param falseClosure closure used if false, not null */ public IfClosure(final Predicate<? super E> predicate, final Closure<? super E> trueClosure, final Closure<? super E> falseClosure) { super(); iPredicate = predicate; iTrueClosure = trueClosure; iFalseClosure = falseClosure; }
/** * Factory to create the predicate. * * @param <T> the type that the predicate queries * @param predicate1 the first predicate to check, not null * @param predicate2 the second predicate to check, not null * @return the <code>and</code> predicate * @throws NullPointerException if either predicate is null */ public static <T> Predicate<T> orPredicate(final Predicate<? super T> predicate1, final Predicate<? super T> predicate2) { if (predicate1 == null || predicate2 == null) { throw new NullPointerException("Predicate must not be null"); } return new OrPredicate<T>(predicate1, predicate2); }
/** * Factory method that performs validation. * * @param <T> the input type * @param predicate the predicate to call, not null * @return the <code>predicate</code> transformer * @throws IllegalArgumentException if the predicate is null */ public static <T> Transformer<T, Boolean> predicateTransformer(final Predicate<? super T> predicate) { if (predicate == null) { throw new IllegalArgumentException("Predicate must not be null"); } return new PredicateTransformer<T>(predicate); }
/** * Factory to create the identity predicate. * * @param <T> the type that the predicate queries * @param object the object to compare to * @return the predicate */ public static <T> Predicate<T> identityPredicate(final T object) { if (object == null) { return NullPredicate.<T>nullPredicate(); } return new IdentityPredicate<T>(object); }