com.annimon.stream.function.Predicate Java Examples

The following examples show how to use com.annimon.stream.function.Predicate. 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: IterateTest.java    From Lightweight-Stream-API with Apache License 2.0 6 votes vote down vote up
@Test
public void testIterateWithPredicate() {
    Predicate<Integer> condition = new Predicate<Integer>() {
        @Override
        public boolean test(Integer value) {
            return value < 20;
        }
    };
    UnaryOperator<Integer> increment = new UnaryOperator<Integer>() {
        @Override
        public Integer apply(Integer t) {
            return t + 5;
        }
    };
    Stream.iterate(0, condition, increment)
            .custom(assertElements(contains(
                    0, 5, 10, 15
            )));
}
 
Example #2
Source File: OnCloseTest.java    From Lightweight-Stream-API with Apache License 2.0 6 votes vote down vote up
@Test
public void testOnCloseWithOtherOperators() {
    final boolean[] state = new boolean[] { false };
    Stream<Integer> stream = Stream.of(0, 1, 2, 2, 3, 4, 4, 5)
            .filter(new Predicate<Integer>() {
                @Override
                public boolean test(Integer value) {
                    return value < 4;
                }
            })
            .onClose(new Runnable() {
                @Override
                public void run() {
                    state[0] = true;
                }
            })
            .distinct()
            .limit(2);
    stream.findFirst();
    stream.close();
    assertTrue(state[0]);
}
 
Example #3
Source File: IterateTest.java    From Lightweight-Stream-API with Apache License 2.0 6 votes vote down vote up
@Test
public void testIterateWithPredicate() {
    Predicate<Integer> condition = new Predicate<Integer>() {
        @Override
        public boolean test(Integer value) {
            return value < 20;
        }
    };
    UnaryOperator<Integer> increment = new UnaryOperator<Integer>() {
        @Override
        public Integer apply(Integer t) {
            return t + 5;
        }
    };
    Stream.iterate(0, condition, increment)
            .custom(assertElements(contains(
                    0, 5, 10, 15
            )));
}
 
Example #4
Source File: OnCloseTest.java    From Lightweight-Stream-API with Apache License 2.0 6 votes vote down vote up
@Test
public void testOnCloseWithOtherOperators() {
    final boolean[] state = new boolean[] { false };
    Stream<Integer> stream = Stream.of(0, 1, 2, 2, 3, 4, 4, 5)
            .filter(new Predicate<Integer>() {
                @Override
                public boolean test(Integer value) {
                    return value < 4;
                }
            })
            .onClose(new Runnable() {
                @Override
                public void run() {
                    state[0] = true;
                }
            })
            .distinct()
            .limit(2);
    stream.findFirst();
    stream.close();
    assertTrue(state[0]);
}
 
Example #5
Source File: OptionalTest.java    From Lightweight-Stream-API with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilterOnEmptyOptional() {
    Optional<Integer> result = Optional.<Integer>empty()
            .filter(Predicate.Util.negate(Functions.remainder(2)));

    assertThat(result, isEmpty());
}
 
Example #6
Source File: LiveDataUtil.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Filters output of a given live data based off a predicate.
 */
public static @NonNull <A> LiveData<A> filter(@NonNull LiveData<A> source, @NonNull Predicate<A> predicate) {
  MediatorLiveData<A> mediator = new MediatorLiveData<>();

  mediator.addSource(source, newValue -> {
    if (predicate.test(newValue)) {
      mediator.setValue(newValue);
    }
  });

  return mediator;
}
 
Example #7
Source File: QMUserMemoryCache.java    From q-municate-android with Apache License 2.0 5 votes vote down vote up
@Override
public List<QMUser> getUsersByIDs(final Collection<Integer> idsList) {
    List<QMUser> result = null;
    result = Stream.of(usersMap.values()).filter(new Predicate<QMUser>() {
        @Override
        public boolean test(QMUser value) {
            return idsList.contains(value.getId());
        }
    }).collect(Collectors.<QMUser>toList());

    return result;
}
 
Example #8
Source File: QMUserMemoryCache.java    From q-municate-android with Apache License 2.0 5 votes vote down vote up
@Override
public QMUser getUserByColumn(final String column, final String value) {
    Optional<QMUser> result = Stream.of(usersMap.values()).filter(new Predicate<QMUser>() {
        @Override
        public boolean test(QMUser user) {
            return getValueByColumn(user, column).equals(value);
        }
    }).findFirst();

    return result.get();
}
 
Example #9
Source File: QMUserMemoryCache.java    From q-municate-android with Apache License 2.0 5 votes vote down vote up
@Override
public List<QMUser> getByColumn(final String column, final String value) {
    List<QMUser> result = null;
    result = Stream.of(usersMap.values()).filter(new Predicate<QMUser>() {
        @Override
        public boolean test(QMUser user) {
            return  getValueByColumn(user, column).equals(value);
        }
    }).collect(Collectors.<QMUser>toList());

    return result;
}
 
Example #10
Source File: QMUserMemoryCache.java    From q-municate-android with Apache License 2.0 5 votes vote down vote up
@Override
public List<QMUser> getByColumn(final String column, final Collection<String> values) {
    List<QMUser> result = null;
    result = Stream.of(usersMap.values()).filter(new Predicate<QMUser>() {
        @Override
        public boolean test(QMUser user) {
            return values.contains(getValueByColumn(user,column));
        }
    }).collect(Collectors.<QMUser>toList());

    return result;
}
 
Example #11
Source File: SelectTest.java    From Lightweight-Stream-API with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testSelect() {
    Stream.of(1, "a", 2, "b", 3, "cc").select(String.class)
            .filter(new Predicate<String>() {
                @Override
                public boolean test(String value) {
                    return value.length() == 1;
                }
            })
            .custom(assertElements(contains(
                  "a", "b"
            )));
}
 
Example #12
Source File: TakeUntilTest.java    From Lightweight-Stream-API with Apache License 2.0 5 votes vote down vote up
@Test
public void testTakeUntil() {
    Stream.of(2, 4, 6, 7, 8, 10, 11)
            .takeUntil(Predicate.Util.negate(Functions.remainder(2)))
            .custom(assertElements(contains(
                    2, 4, 6, 7
            )));
}
 
Example #13
Source File: FilterTest.java    From Lightweight-Stream-API with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilterWithOrPredicate() {
    Predicate<Integer> predicate = Predicate.Util.or(Functions.remainder(2), Functions.remainder(3));
    Stream.range(0, 10)
            .filter(predicate)
            .custom(assertElements(contains(
                  0, 2, 3, 4, 6, 8, 9
            )));
}
 
Example #14
Source File: FilterTest.java    From Lightweight-Stream-API with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilterWithAndPredicate() {
    Predicate<Integer> predicate = Predicate.Util.and(Functions.remainder(2), Functions.remainder(3));
    Stream.range(0, 10)
            .filter(predicate)
            .custom(assertElements(contains(
                  0, 6
            )));
}
 
Example #15
Source File: FilterTest.java    From Lightweight-Stream-API with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilterWithXorPredicate() {
    Predicate<Integer> predicate = Predicate.Util.xor(Functions.remainder(2), Functions.remainder(3));
    Stream.range(0, 10)
            .filter(predicate)
            .custom(assertElements(contains(
                  2, 3, 4, 8, 9
            )));
}
 
Example #16
Source File: OptionalTest.java    From Lightweight-Stream-API with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilter() {
    Optional<Integer> result = Optional.of(10)
            .filter(Predicate.Util.negate(Functions.remainder(2)));

    assertThat(result, isEmpty());
}
 
Example #17
Source File: OptionalTest.java    From Lightweight-Stream-API with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilterOnEmptyOptional() {
    Optional<Integer> result = Optional.<Integer>empty()
            .filter(Predicate.Util.negate(Functions.remainder(2)));

    assertThat(result, isEmpty());
}
 
Example #18
Source File: FilterTest.java    From Lightweight-Stream-API with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilterWithXorPredicate() {
    Predicate<Integer> predicate = Predicate.Util.xor(Functions.remainder(2), Functions.remainder(3));
    Stream.range(0, 10)
            .filter(predicate)
            .custom(assertElements(contains(
                  2, 3, 4, 8, 9
            )));
}
 
Example #19
Source File: FilterTest.java    From Lightweight-Stream-API with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilterWithAndPredicate() {
    Predicate<Integer> predicate = Predicate.Util.and(Functions.remainder(2), Functions.remainder(3));
    Stream.range(0, 10)
            .filter(predicate)
            .custom(assertElements(contains(
                  0, 6
            )));
}
 
Example #20
Source File: FilterTest.java    From Lightweight-Stream-API with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilterWithOrPredicate() {
    Predicate<Integer> predicate = Predicate.Util.or(Functions.remainder(2), Functions.remainder(3));
    Stream.range(0, 10)
            .filter(predicate)
            .custom(assertElements(contains(
                  0, 2, 3, 4, 6, 8, 9
            )));
}
 
Example #21
Source File: TakeUntilTest.java    From Lightweight-Stream-API with Apache License 2.0 5 votes vote down vote up
@Test
public void testTakeUntil() {
    Stream.of(2, 4, 6, 7, 8, 10, 11)
            .takeUntil(Predicate.Util.negate(Functions.remainder(2)))
            .custom(assertElements(contains(
                    2, 4, 6, 7
            )));
}
 
Example #22
Source File: SelectTest.java    From Lightweight-Stream-API with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testSelect() {
    Stream.of(1, "a", 2, "b", 3, "cc").select(String.class)
            .filter(new Predicate<String>() {
                @Override
                public boolean test(String value) {
                    return value.length() == 1;
                }
            })
            .custom(assertElements(contains(
                  "a", "b"
            )));
}
 
Example #23
Source File: OptionalTest.java    From Lightweight-Stream-API with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilter() {
    Optional<Integer> result = Optional.of(10)
            .filter(Predicate.Util.negate(Functions.remainder(2)));

    assertThat(result, isEmpty());
}
 
Example #24
Source File: Utils.java    From UsbSerial with MIT License 4 votes vote down vote up
public static <T> List<T> removeIf(Collection<T> c, Predicate<? super T> predicate) {
    return Stream.of(c.iterator())
            .filterNot(predicate)
            .collect(Collectors.<T>toList());
}
 
Example #25
Source File: FullBackupExporter.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
private static int exportTable(@NonNull   String table,
                               @NonNull   SQLiteDatabase input,
                               @NonNull   BackupFrameOutputStream outputStream,
                               @Nullable  Predicate<Cursor> predicate,
                               @Nullable  Consumer<Cursor> postProcess,
                                          int count)
    throws IOException
{
  String template = "INSERT INTO " + table + " VALUES ";

  try (Cursor cursor = input.rawQuery("SELECT * FROM " + table, null)) {
    while (cursor != null && cursor.moveToNext()) {
      EventBus.getDefault().post(new BackupEvent(BackupEvent.Type.PROGRESS, ++count));

      if (predicate == null || predicate.test(cursor)) {
        StringBuilder                     statement        = new StringBuilder(template);
        BackupProtos.SqlStatement.Builder statementBuilder = BackupProtos.SqlStatement.newBuilder();

        statement.append('(');

        for (int i=0;i<cursor.getColumnCount();i++) {
          statement.append('?');

          if (cursor.getType(i) == Cursor.FIELD_TYPE_STRING) {
            statementBuilder.addParameters(BackupProtos.SqlStatement.SqlParameter.newBuilder().setStringParamter(cursor.getString(i)));
          } else if (cursor.getType(i) == Cursor.FIELD_TYPE_FLOAT) {
            statementBuilder.addParameters(BackupProtos.SqlStatement.SqlParameter.newBuilder().setDoubleParameter(cursor.getDouble(i)));
          } else if (cursor.getType(i) == Cursor.FIELD_TYPE_INTEGER) {
            statementBuilder.addParameters(BackupProtos.SqlStatement.SqlParameter.newBuilder().setIntegerParameter(cursor.getLong(i)));
          } else if (cursor.getType(i) == Cursor.FIELD_TYPE_BLOB) {
            statementBuilder.addParameters(BackupProtos.SqlStatement.SqlParameter.newBuilder().setBlobParameter(ByteString.copyFrom(cursor.getBlob(i))));
          } else if (cursor.getType(i) == Cursor.FIELD_TYPE_NULL) {
            statementBuilder.addParameters(BackupProtos.SqlStatement.SqlParameter.newBuilder().setNullparameter(true));
          } else {
            throw new AssertionError("unknown type?"  + cursor.getType(i));
          }

          if (i < cursor.getColumnCount()-1) {
            statement.append(',');
          }
        }

        statement.append(')');

        outputStream.write(statementBuilder.setStatement(statement.toString()).build());

        if (postProcess != null) postProcess.accept(cursor);
      }
    }
  }

  return count;
}
 
Example #26
Source File: Optional.java    From WanAndroid with GNU General Public License v3.0 4 votes vote down vote up
public Optional<T> filter(Predicate<? super T> predicate) {
    if (!isPresent()) return this;
    return predicate.test(value) ? this : Optional.<T>empty();
}
 
Example #27
Source File: Optional.java    From WanAndroid with GNU General Public License v3.0 4 votes vote down vote up
public Optional<T> filterNot(Predicate<? super T> predicate) {
    return filter(Predicate.Util.negate(predicate));
}
 
Example #28
Source File: ObjTakeUntil.java    From Lightweight-Stream-API with Apache License 2.0 4 votes vote down vote up
public ObjTakeUntil(
        @NotNull Iterator<? extends T> iterator,
        @NotNull Predicate<? super T> predicate) {
    this.iterator = iterator;
    this.stopPredicate = predicate;
}
 
Example #29
Source File: ObjDropWhile.java    From Lightweight-Stream-API with Apache License 2.0 4 votes vote down vote up
public ObjDropWhile(
        @NotNull Iterator<? extends T> iterator,
        @NotNull Predicate<? super T> predicate) {
    this.iterator = iterator;
    this.predicate = predicate;
}
 
Example #30
Source File: ObjFilter.java    From Lightweight-Stream-API with Apache License 2.0 4 votes vote down vote up
public ObjFilter(
        @NotNull Iterator<? extends T> iterator,
        @NotNull Predicate<? super T> predicate) {
    this.iterator = iterator;
    this.predicate = predicate;
}