Example usage for java.util.function Predicate and

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

Introduction

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

Prototype

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

Source Link

Document

Returns a composed predicate that represents a short-circuiting logical AND 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.and(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.and(b).test("ac"));
    System.out.println("test bc:" + a.and(b).test("bc"));
    System.out.println("test ab:" + a.and(b).test("ab"));

}

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.and(isPaidEnough)));

    Student somebody = students.get(0);//from ww w.j  a  v  a2 s .c om
    System.out.println(findStudents(students, Predicate.<Student>isEqual(somebody)));
}

From source file:cn.lambdalib.util.mc.Raytrace.java

/**
 * Performs a RayTrace starting from the target entity's eye towards its looking direction.
 * The trace will automatically ignore the target entity.
 *///from   w  w  w.  ja v a2 s. c  o m
public static MovingObjectPosition traceLiving(Entity entity, double dist, Predicate<Entity> pred,
        IBlockSelector blockSel) {
    Motion3D mo = new Motion3D(entity, true);
    Vec3 v1 = mo.getPosVec(), v2 = mo.move(dist).getPosVec();

    Predicate<Entity> exclude = EntitySelectors.exclude(entity);

    return perform(entity.worldObj, v1, v2, pred == null ? exclude : exclude.and(pred), blockSel);
}

From source file:dk.dma.ais.view.rest.QueryParameterHelper.java

private static Predicate<AisPacket> getSourceFilter(UriInfo info) {
    List<String> filters = QueryParameterValidators.getParameters(info, "filter");

    if (filters.isEmpty()) {
        return null;
    }/* w w  w . ja v  a  2s.  com*/
    Predicate<AisPacket> p = AisPacketFilters.parseSourceFilter(filters.get(0));
    for (int i = 1; i < filters.size(); i++) {
        p = p.and(AisPacketFilters.parseSourceFilter(filters.get(i)));
    }
    return p;
}

From source file:dk.dma.ais.view.rest.QueryParameterHelper.java

/**
 *///from  w  w  w  .ja  v a2 s . co  m
private static Predicate<AisPacket> getPacketFilter(UriInfo info) {
    List<String> filters = QueryParameterValidators.getParameters(info, "filter");
    if (filters.isEmpty()) {
        return null;
    }

    Predicate<AisPacket> p = AisPacketFilters.parseExpressionFilter(filters.get(0));
    for (int i = 1; i < filters.size(); i++) {
        p = p.and(AisPacketFilters.parseExpressionFilter(filters.get(i)));
    }
    return p;
}

From source file:dk.dma.ais.view.rest.QueryParameterHelper.java

private static Predicate<AisPacketSource> getPacketSourceFilter(UriInfo info) {
    List<String> filters = QueryParameterValidators.getParameters(info, "filter");
    if (filters.isEmpty()) {
        return null;
    }/*from   w  w w . j ava2s.  c om*/

    Predicate<AisPacketSource> p = null;
    try {
        p = AisPacketSource.createPredicate(filters.get(0));
        for (int i = 1; i < filters.size(); i++) {
            p = p.and(AisPacketSource.createPredicate(filters.get(i)));
        }
    } catch (NullPointerException e) {

    }

    return p;
}

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 . java 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:io.github.mywarp.mywarp.command.InformativeCommands.java

@Command(aliases = { "list", "alist" }, desc = "list.description", help = "list.help")
@Require("mywarp.cmd.list")
@Billable(FeeType.LIST)/*from  ww  w.j a  v a 2  s.c om*/
public void list(final Actor actor, @OptArg("1") int page, @Switch('c') final String creator,
        @Switch('n') final String name,
        @Switch('r') @Range(min = 1, max = Integer.MAX_VALUE) final Integer radius,
        @Switch('w') final String world) throws IllegalCommandSenderException {

    // build the listing predicate
    Predicate<Warp> filter = authorizationResolver.isViewable(actor);

    if (creator != null) {
        filter = filter.and(input -> {
            Optional<String> creatorName = playerNameResolver.getByUniqueId(input.getCreator());
            return creatorName.isPresent() && StringUtils.containsIgnoreCase(creatorName.get(), creator);
        });
    }

    if (name != null) {
        filter = filter.and(input -> StringUtils.containsIgnoreCase(input.getName(), name));
    }

    if (radius != null) {
        if (!(actor instanceof LocalEntity)) {
            throw new IllegalCommandSenderException(actor);
        }

        LocalEntity entity = (LocalEntity) actor;

        final UUID worldId = entity.getWorld().getUniqueId();

        final int squaredRadius = radius * radius;
        final Vector3d position = entity.getPosition();
        filter = filter.and(input -> input.getWorldIdentifier().equals(worldId)
                && input.getPosition().distanceSquared(position) <= squaredRadius);
    }

    if (world != null) {
        filter = filter.and(input -> {
            Optional<LocalWorld> worldOptional = game.getWorld(input.getWorldIdentifier());
            return worldOptional.isPresent()
                    && StringUtils.containsIgnoreCase(worldOptional.get().getName(), world);
        });
    }

    //query the warps
    //noinspection RedundantTypeArguments
    final List<Warp> warps = Ordering.natural().sortedCopy(warpManager.getAll(filter));

    Function<Warp, Message> mapping = input -> {
        // 'name' (world) by player
        Message.Builder builder = Message.builder();
        builder.append("'");
        builder.append(input);
        builder.append("' (");
        builder.append(CommandUtil.toWorldName(input.getWorldIdentifier(), game));
        builder.append(") ");
        builder.append(msg.getString("list.by"));
        builder.append(" ");

        if (actor instanceof LocalPlayer && input.isCreator(((LocalPlayer) actor).getUniqueId())) {
            builder.append(msg.getString("list.you"));
        } else {
            builder.append(CommandUtil.toName(input.getCreator(), playerNameResolver));
        }
        return builder.build();
    };

    // display
    StringPaginator.of(msg.getString("list.heading"), warps).withMapping(mapping::apply).paginate()
            .display(actor, page);
}

From source file:controller.PlayController.java

public List<Play> filterPlays(Play play, List<Play> plays) {
    Predicate<Play> selector = p -> p != null;
    String playName = play.getPlayName();
    Integer ticketPrice = play.getTicketPrice();
    if (playName != null && !playName.equals("")) {
        selector = selector.and(p -> p.getPlayName().equals(playName));
    }// ww  w. j a va  2 s  . c om
    if (play.getStartTime() != null) {
        Time startTime = Time.valueOf(play.getStartTime().toString());
        selector = selector.and(p -> (p.getStartTime().after(startTime) || p.getStartTime().equals(startTime)));
    }
    if (play.getEndTime() != null) {
        Time endTime = Time.valueOf(play.getEndTime().toString());
        selector = selector.and(p -> (p.getEndTime().before(endTime) || p.getEndTime().equals(endTime)));
    }
    if (play.getStartDate() != null) {
        Date startDate = Date.valueOf(play.getStartDate().toString());
        selector = selector.and(p -> p.getStartDate().equals(startDate));
    }
    if (ticketPrice != 0) {
        selector = selector.and(p -> p.getTicketPrice() == ticketPrice);
    }
    return lambdaFilter(selector, plays);
}