import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.function.*;

// The main method must be in a class named "Main".
class Main {

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            System.out.println(findIp().orElse("No ip found"));
        }

        System.out.println("-".repeat(10) + " with exception " + "-".repeat(10));

        for (int i = 0; i < 10; i++) {
            try {
                System.out.println(findIp().orElseThrow(() -> new IOException("Ip not found")));
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
    }

    private static Optional<String> findIp() {
        Map<String, String> headers = Map.of("Accepts", "Application/json");
        Supplier<String> getIp = () -> new Random().nextInt(10) < 5 ? "192.168.1.1" : null;

        return Optional.ofNullable(headers.get("X-Forwarded-For"))
                .or(() -> Optional.ofNullable(headers.get("X-Custom-Forwarded-For")))
                .or(() -> Optional.ofNullable(getIp.get()))
                .map(ip -> ip.split(",")[0].trim())
                .filter(Predicate.not(String::isBlank));
    }
}

class Optional<T> {
    private static final Optional<?> EMPTY = new Optional<>(null);

    private final T value;

    private Optional(T value) {
        this.value = value;
    }

    public static <U> Optional<U> of(U value) {
        return new Optional<>(Objects.requireNonNull(value));
    }

    public static <T> Optional<T> ofNullable(T value) {
        if (value == null) return empty();
        return new Optional<>(value);
    }

    @SuppressWarnings("unchecked")
    public static <U> Optional<U> empty() {
        return (Optional<U>) EMPTY;
    }

    public boolean isEmpty() {
        return this.value == null;
    }

    public T get() {
        if (this.value == null) throw new NoSuchElementException("No value present");
        return this.value;
    }

    public <U> Optional<U> map(Function<? super T, ? extends U> mapper) {
        if (this.isEmpty()) return empty();

        return new Optional<>(mapper.apply(this.value));
    }

    @SuppressWarnings("unchecked")
    public <U> Optional<U> flatMap(Function<? super T, Optional<? extends U>> mapper) {
        if (this.isEmpty()) return empty();

        return (Optional<U>) mapper.apply(this.value);
    }

    public T orElseThrow() {
        if (this.isEmpty()) throw new NoSuchElementException("No value present");

        return this.value;
    }

    public <X extends Throwable> T orElseThrow(Supplier<? extends X> supplier) throws X {
        if (this.isEmpty()) throw supplier.get();

        return this.value;
    }

    @SuppressWarnings("unchecked")
    public Optional<T> or(Supplier<Optional<? extends T>> optional) {
        if (this.isEmpty()) return (Optional<T>) Objects.requireNonNull(optional.get());

        return this;
    }

    public Optional<T> filter(Predicate<T> predicate) {
        if (this.isEmpty()) return this;

        return predicate.test(this.value) ? this : empty();
    }

    public T orElse(T other) {
        return this.isEmpty() ? other : this.value;
    }
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: