class Atomics {

    public static int getAndIncrement(AtomicInteger i, int maxValue) {
        return i.getAndUpdate(prev -> {
            if (prev < maxValue) {
                return prev + 1;
            } else {
                return maxValue;
            }
        });
    }

}