001/*
002 * jPOS Project [http://jpos.org]
003 * Copyright (C) 2000-2026 jPOS Software SRL
004 *
005 * This program is free software: you can redistribute it and/or modify
006 * it under the terms of the GNU Affero General Public License as
007 * published by the Free Software Foundation, either version 3 of the
008 * License, or (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013 * GNU Affero General Public License for more details.
014 *
015 * You should have received a copy of the GNU Affero General Public License
016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jpos.util;
020
021import java.util.concurrent.atomic.AtomicLong;
022import java.util.function.Supplier;
023
024/**
025 * Caches a supplier's result up to approximately <code>maxCycles</code>.
026 *
027 * <p>After approximately <code>maxCycles</code> calls to its <code>get()</code> operation,
028 * <code>Recyclable</code> fetches a new result from its underlying Supplier.
029 * </p>
030 *
031 * @param <T> the type of results supplied by this supplier
032 * @since 2.1.3
033 */
034public class Recyclable<T> implements Supplier<T> {
035    private Supplier<T> supplier;
036    private long maxCycles;
037    private AtomicLong cycles = new AtomicLong();
038    private volatile T obj;
039
040    /**
041     * Constructs a Recyclable that delegates to {@code supplier} and rebuilds the wrapped object every {@code maxCycles} uses.
042     *
043     * @param supplier factory that produces fresh instances
044     * @param maxCycles number of uses before the wrapped instance is replaced
045     */
046    public Recyclable(Supplier<T> supplier, long maxCycles) {
047        this.supplier = supplier;
048        this.maxCycles = maxCycles;
049    }
050
051    @Override
052    public T get() {
053        if (cycles.getAndIncrement() == maxCycles || obj == null) {
054            obj = supplier.get();
055            cycles.set(0);
056        }
057        return obj;
058    }
059}