Java Code Examples for com.google.common.collect.Iterators#tryFind()

The following examples show how to use com.google.common.collect.Iterators#tryFind() . 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: FindElementInArray.java    From levelup-java-examples with Apache License 2.0 6 votes vote down vote up
@Test
public void find_element_in_array_java_with_guava() {

	Integer[] vikQueensLosingSeasons = { 1962, 1967, 1984, 2011, 1966,
			1963, 1982, 2001, 1990, 2002, 2006, 2010, 1965, 1972, 1979,
			1981, 1985 };

	Optional<Integer> contains = Iterators.tryFind(
			Iterators.forArray(vikQueensLosingSeasons),
			new Predicate<Integer>() {

				public boolean apply(Integer input) {
					if (input == 1962) {
						return true;
					} else {
						return false;
					}
				}
			});
	
	assertTrue(contains.isPresent());
	assertEquals(new Integer(1962), contains.get());
}
 
Example 2
Source File: ShardingPreparedStatement.java    From sharding-jdbc-1.5.1 with Apache License 2.0 5 votes vote down vote up
private BatchPreparedStatementUnit getPreparedBatchStatement(final SQLExecutionUnit sqlExecutionUnit) throws SQLException {
    Optional<BatchPreparedStatementUnit> preparedBatchStatement = Iterators.tryFind(batchStatementUnits.iterator(), new Predicate<BatchPreparedStatementUnit>() {
        
        @Override
        public boolean apply(final BatchPreparedStatementUnit input) {
            return Objects.equals(input.getSqlExecutionUnit(), sqlExecutionUnit);
        }
    });
    if (preparedBatchStatement.isPresent()) {
        return preparedBatchStatement.get();
    }
    BatchPreparedStatementUnit result = new BatchPreparedStatementUnit(sqlExecutionUnit, generatePreparedStatement(sqlExecutionUnit));
    batchStatementUnits.add(result);
    return result;
}
 
Example 3
Source File: RunningService.java    From shardingsphere-elasticjob-cloud with Apache License 2.0 5 votes vote down vote up
private Optional<TaskContext> findTask(final TaskContext taskContext) {
    return Iterators.tryFind(getRunningTasks(taskContext.getMetaInfo().getJobName()).iterator(), new Predicate<TaskContext>() {
        @Override
        public boolean apply(final TaskContext input) {
            return input.equals(taskContext);
        }
    });
}
 
Example 4
Source File: SchemaOrderingTest.java    From streams with Apache License 2.0 5 votes vote down vote up
/**
 * assert iterator of Schema contains URI items ending with in order.
 * @param iterator Iterator of Schema
 * @param items List of String
 */
public void assertContainsItemsEndingWithInOrder(Iterator<Schema> iterator, List<String> items) {
  for ( String item : items ) {
    Optional<Schema> tryFind = Iterators.tryFind( iterator, new SchemaUriEndsWithPredicate(item) );
    Assert.assertTrue(tryFind.isPresent());
  }
}