Eshu

 view release on metacpan or  search on metacpan

t/0255-realworld-java.t  view on Meta::CPAN

public static List<String> processNames(List<String> names) {
    return names.stream()
    .filter(name -> name != null && !name.isEmpty())
    .map(String::trim)
    .map(String::toLowerCase)
    .sorted()
    .distinct()
    .collect(Collectors.toList());
}
END
    is(j($code), $code, 'Java: stream with lambdas');
}

# 11. Optional
{
    my $code = <<'END';
public Optional<User> findActiveUser(long id) {
    return userRepo.findById(id)
    .filter(User::isActive)
    .map(user -> {
        user.setLastSeen(Instant.now());
        return user;
    });
}
END
    is(j($code), $code, 'Java: Optional chaining');
}

# 12. enum
{
    my $code = <<'END';
public enum Status {
    PENDING("Pending"),
    ACTIVE("Active"),
    CANCELLED("Cancelled"),
    DONE("Done");

    private final String label;

    Status(String label) {
        this.label = label;
    }

    public String getLabel() {
        return label;
    }

    public boolean isTerminal() {
        return this == CANCELLED || this == DONE;
    }
}
END
    is(j($code), $code, 'Java: enum with methods');
}

# 13. record
{
    my $code = <<'END';
public record Point(double x, double y) {
    public Point {
        if (Double.isNaN(x) || Double.isNaN(y)) {
            throw new IllegalArgumentException("NaN coordinates");
        }
    }

    public double distance(Point other) {
        double dx = this.x - other.x;
        double dy = this.y - other.y;
        return Math.sqrt(dx * dx + dy * dy);
    }

    public Point translate(double dx, double dy) {
        return new Point(x + dx, y + dy);
    }
}
END
    is(j($code), $code, 'Java: record class');
}

# 14. switch expression
{
    my $code = <<'END';
public static int daysInMonth(int month, int year) {
    return switch (month) {
        case 1, 3, 5, 7, 8, 10, 12 -> 31;
        case 4, 6, 9, 11            -> 30;
        case 2 -> (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) ? 29 : 28;
        default -> throw new IllegalArgumentException("Invalid month: " + month);
    };
}
END
    is(j($code), $code, 'Java: switch expression');
}

# 15. annotation
{
    my $code = <<'END';
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface Cacheable {
    String key() default "";
    int ttl() default 300;
    boolean evictAll() default false;
}
END
    is(j($code), $code, 'Java: custom annotation');
}

# 16. abstract class
{
    my $code = <<'END';
public abstract class Template {
    public final void execute() {
        setup();
        doWork();
        teardown();
    }

    protected void setup() {
        // default: no-op
    }



( run in 1.559 second using v1.01-cache-2.11-cpan-60884a8df76 )