Example usage for java.util.function Predicate or

List of usage examples for java.util.function Predicate or

Introduction

In this page you can find the example usage for java.util.function Predicate or.

Prototype

default Predicate<T> or(Predicate<? super T> other) 

Source Link

Document

Returns a composed predicate that represents a short-circuiting logical OR of this predicate and another.

Usage

From source file:Main.java

public static void main(String[] args) {
    Predicate<String> i = (s) -> s.length() > 5;
    Predicate<String> j = (s) -> s.length() < 3;

    System.out.println(i.or(j).test("java2s.com "));
}

From source file:Main.java

public static void main(String[] args) {
    Predicate<String> a = (input) -> input.contains("a");
    Predicate<String> b = (input) -> input.contains("b");

    System.out.println("test ac:" + a.or(b).test("ac"));
    System.out.println("test bc:" + a.or(b).test("bc"));
    System.out.println("test de:" + a.or(b).test("de"));

}

From source file:Main.java

public static void main(String[] args) {
    Predicate<Student> isPaidEnough = e -> e.gpa > 1;
    Predicate<Student> isExperiencedEnough = e -> e.id < 10;

    List<Student> students = Arrays.asList(new Student(1, 3, "John"), new Student(2, 4, "Jane"),
            new Student(3, 3, "Jack"));

    System.out.println(findStudents(students, isPaidEnough));
    System.out.println(findStudents(students, isExperiencedEnough));
    System.out.println(findStudents(students, isExperiencedEnough.or(isPaidEnough)));

    Student somebody = students.get(0);//from   w w  w .  j a  va 2 s .c  o m
    System.out.println(findStudents(students, Predicate.<Student>isEqual(somebody)));
}

From source file:com.mgmtp.perfload.perfalyzer.binning.RequestFilesMerger.java

public void mergeFiles(final List<PerfAlyzerFile> inputFiles) throws IOException {
    Predicate<PerfAlyzerFile> predicate1 = perfAlyzerFilePartsMatchWildcards("measuring", "*",
            "requestsPerInterval");
    Predicate<PerfAlyzerFile> predicate2 = perfAlyzerFilePartsMatchWildcards("measuring", "*",
            "aggregatedResponseTimes");

    Predicate<PerfAlyzerFile> predicateOr = predicate1.or(predicate2);

    Set<PerfAlyzerFile> paFiles = inputFiles.stream().filter(predicateOr).collect(toSet());
    ListMultimap<String, PerfAlyzerFile> byOperationMultimap = ArrayListMultimap.create();

    for (PerfAlyzerFile paf : paFiles) {
        byOperationMultimap.put(paf.getFileNameParts().get(1), paf);
    }/*ww w.j  a  v  a  2  s  .  c  o m*/

    StrTokenizer tokenizer = StrTokenizer.getCSVInstance();
    tokenizer.setDelimiterChar(DELIMITER);

    for (String operation : byOperationMultimap.keySet()) {
        List<PerfAlyzerFile> list = byOperationMultimap.get(operation);

        checkState(list.size() == 2, "Two files are required by operation but found %d for '%s'", list.size(),
                operation);

        List<String> resultLines = newArrayListWithCapacity(2);

        PerfAlyzerFile paf1 = list.stream().filter(predicate1).findFirst().get();
        File file1 = new File(binnedDir, paf1.getFile().getPath());
        List<String> lines1 = Files.readLines(file1, Charsets.UTF_8);

        PerfAlyzerFile paf2 = list.stream().filter(predicate2).findFirst().get();
        File file2 = new File(binnedDir, paf2.getFile().getPath());
        List<String> lines2 = Files.readLines(file2, Charsets.UTF_8);

        if (lines1.size() == lines2.size()) {
            File resultFile = new File(binnedDir,
                    paf1.copy().removeFileNamePart(2).addFileNamePart("aggregated").getFile().getPath());

            for (int i = 0; i < lines1.size(); ++i) {
                String line1 = get(lines1, i);
                String line2 = get(lines2, i);
                resultLines.add(line1 + DELIMITER + line2);
            }

            writeLines(resultFile, Charsets.UTF_8.name(), resultLines);

            deleteQuietly(file1);
            deleteQuietly(file2);
        } else {
            log.warn("Files to merge must have the same number of lines. Merging not possible: {}", list);
        }
    }
}

From source file:com.github.springfox.loader.SpringfoxLoaderConfig.java

@Bean
@Conditional(ActiveProfilesCondition.class)
public Docket api() {
    ApiSelectorBuilder apiSelectorBuilder = new Docket(DocumentationType.SWAGGER_2).select();
    Predicate<RequestHandler> predicate = RequestHandlerSelectors
            .basePackage(springfoxLoader.getBasePackage())::apply;
    if (springfoxLoader.includeControllers().length > 0) {
        Class<?>[] controllers = springfoxLoader.includeControllers();
        for (Class<?> controller : controllers) {
            Predicate<RequestHandler> includeControllerRequestHandler = RequestHandlerSelectors
                    .basePackage(controller.getPackage().getName())::apply;
            predicate = predicate.or(includeControllerRequestHandler);
        }//  w w w .j av a  2s  .  c o m
    }

    apiSelectorBuilder.apis(predicate::test);

    apiSelectorBuilder.paths(PathSelectors.any()).build().apiInfo(apiInfo())
            .pathMapping(springfoxLoader.getPath());
    return apiSelectorBuilder.build();
}

From source file:dk.dma.ais.track.rest.resource.TrackResource.java

/** Create a Predicate<TargetInfo> out of user supplied mmsi and area information */
static Predicate<TargetInfo> createTargetFilterPredicate(Set<Integer> mmsis, Set<Area> baseAreas,
        Set<Area> areas) {
    Predicate<TargetInfo> mmsiPredicate = null;
    if (mmsis != null && mmsis.size() > 0) {
        mmsiPredicate = targetInfo -> mmsis.contains(targetInfo.getMmsi());
    }/* w  w w  .j a  va  2s.  c  om*/

    Predicate<TargetInfo> baseAreaPredicate = null;
    if (baseAreas != null && baseAreas.size() > 0) {
        baseAreaPredicate = targetInfo -> baseAreas.stream()
                .anyMatch(area -> targetInfo.getPosition() != null && area.contains(targetInfo.getPosition()));
    }

    Predicate<TargetInfo> areaPredicate = null;
    if (areas != null && areas.size() > 0) {
        areaPredicate = targetInfo -> areas.stream()
                .anyMatch(area -> targetInfo.getPosition() != null && area.contains(targetInfo.getPosition()));
    }

    Predicate<TargetInfo> resultingAreaPredicate = null;
    if (baseAreaPredicate != null && areaPredicate == null) {
        resultingAreaPredicate = baseAreaPredicate;
    } else if (baseAreaPredicate != null && areaPredicate != null) {
        resultingAreaPredicate = baseAreaPredicate.and(areaPredicate);
    } else {
        resultingAreaPredicate = areaPredicate;
    }

    if (mmsiPredicate == null && resultingAreaPredicate == null)
        return t -> true;
    else if (mmsiPredicate != null && resultingAreaPredicate == null)
        return mmsiPredicate;
    else if (mmsiPredicate == null && resultingAreaPredicate != null)
        return resultingAreaPredicate;
    else
        return mmsiPredicate.or(resultingAreaPredicate);
}

From source file:org.fenixedu.academic.domain.degree.DegreeType.java

@SafeVarargs
public static Predicate<DegreeType> oneOf(Predicate<DegreeType> one, Predicate<DegreeType>... others) {
    for (Predicate<DegreeType> pred : others) {
        one = one.or(pred);
    }//w  w  w .j a v a 2 s .com
    return nonNull.and(one);
}

From source file:org.jnosql.artemis.reflection.Reflections.java

public List<Field> getFields(Class classEntity) {

    List<Field> fields = new ArrayList<>();

    if (isMappedSuperclass(classEntity)) {
        fields.addAll(getFields(classEntity.getSuperclass()));
    }//from www.  ja v a2 s. c om
    Predicate<Field> hasColumnAnnotation = f -> f.getAnnotation(Column.class) != null;
    Predicate<Field> hasKeyAnnotation = f -> f.getAnnotation(Key.class) != null;

    Stream.of(classEntity.getDeclaredFields()).filter(hasColumnAnnotation.or(hasKeyAnnotation))
            .forEach(fields::add);
    return fields;
}

From source file:org.talend.dataprep.api.filter.SimpleFilterService.java

private Predicate<DataSetRow> buildFilter(JsonNode currentNode, RowMetadata rowMetadata) {
    final Iterator<JsonNode> children = currentNode.elements();
    final JsonNode operationContent = children.next();
    final String columnId = operationContent.has("field") ? operationContent.get("field").asText() : null;
    final String value = operationContent.has("value") ? operationContent.get("value").asText() : null;

    final Iterator<String> propertiesIterator = currentNode.fieldNames();
    if (!propertiesIterator.hasNext()) {
        throw new UnsupportedOperationException(
                "Unsupported query, empty filter definition: " + currentNode.toString());
    }//from ww w . jav  a 2s  .  c  o m

    final String operation = propertiesIterator.next();
    if (columnId == null && allowFullFilter(operation)) {
        // Full data set filter (no column)
        final List<ColumnMetadata> columns = rowMetadata.getColumns();
        Predicate<DataSetRow> predicate = null;
        if (!columns.isEmpty()) {
            predicate = buildOperationFilter(currentNode, rowMetadata, columns.get(0).getId(), operation,
                    value);
            for (int i = 1; i < columns.size(); i++) {
                predicate = predicate.or(buildOperationFilter(currentNode, rowMetadata, columns.get(i).getId(),
                        operation, value));
            }
        }
        return predicate;
    } else {
        return buildOperationFilter(currentNode, rowMetadata, columnId, operation, value);
    }
}

From source file:org.talend.dataprep.api.filter.SimpleFilterService.java

/**
 * Create a predicate that do a logical OR between 2 filters
 *
 * @param nodeContent The node content/*from w ww .  ja  va  2s.c  o  m*/
 * @param rowMetadata Row metadata to used to obtain information (valid/invalid, types...)
 * @return the OR predicate
 */
private Predicate<DataSetRow> createOrPredicate(final JsonNode nodeContent, RowMetadata rowMetadata) {
    checkValidMultiPredicate(nodeContent);
    final Predicate<DataSetRow> leftFilter = buildFilter(nodeContent.get(0), rowMetadata);
    final Predicate<DataSetRow> rightFilter = buildFilter(nodeContent.get(1), rowMetadata);
    return leftFilter.or(rightFilter);
}