TheAlgorithms/Java · error · IllegalArgumentException

Capacity must be > 0

Error message

Capacity must be > 0

What it means

The RRCache.Builder constructor requires a strictly positive capacity — it is the only mandatory parameter and defines the maximum number of entries the cache can hold. Zero or negative capacity is rejected immediately.

Source

Thrown at src/main/java/com/thealgorithms/datastructures/caches/RRCache.java:431

     *
     * @param <K> the type of keys maintained by the cache
     * @param <V> the type of values stored in the cache
     */
    public static class Builder<K, V> {
        private final int capacity;
        private long defaultTTL = 0;
        private Random random;
        private BiConsumer<K, V> evictionListener;
        private EvictionStrategy<K, V> evictionStrategy = new RRCache.PeriodicEvictionStrategy<>(100);
        /**
         * Creates a new {@code Builder} with the specified cache capacity.
         *
         * @param capacity the maximum number of entries the cache can hold; must be > 0
         * @throws IllegalArgumentException if {@code capacity} is less than or equal to 0
         */
        public Builder(int capacity) {
            if (capacity <= 0) {
                throw new IllegalArgumentException("Capacity must be > 0");
            }
            this.capacity = capacity;
        }

        /**
         * Sets the default time-to-live (TTL) in milliseconds for cache entries.
         *
         * @param ttlMillis the TTL duration in milliseconds; must be >= 0
         * @return this builder instance for chaining
         * @throws IllegalArgumentException if {@code ttlMillis} is negative
         */
        public Builder<K, V> defaultTTL(long ttlMillis) {
            if (ttlMillis < 0) {
                throw new IllegalArgumentException("Default TTL must be >= 0");
            }
            this.defaultTTL = ttlMillis;
            return this;
        }

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Validate that capacity is >= 1 before constructing the Builder
  2. Provide a sensible positive default when configuration is missing or invalid
  3. Fail fast at startup with a clear configuration error

Example fix

// before
RRCache<String,String> c = new RRCache.Builder<String,String>(poolSize).build();

// after
int cap = Math.max(1, poolSize);
RRCache<String,String> c = new RRCache.Builder<String,String>(cap).build();
Defensive patterns

Strategy: validation

Validate before calling

int cap = Math.max(1, configuredCapacity);
RRCache<String,String> cache = new RRCache.Builder<String,String>(cap).build();

Try / catch

try {
    builder = new RRCache.Builder<>(cap);
} catch (IllegalArgumentException e) {
    builder = new RRCache.Builder<>(100); // safe default
}

Prevention

When it happens

Trigger: Calling new RRCache.Builder<>(0), new RRCache.Builder<>(-10), or passing a config-derived capacity <= 0.

Common situations: Capacity from a properties file or environment variable that can be 0. Deriving capacity from a collection that can be empty (e.g., list.size()).

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/aabdec149a752047. Report an issue: GitHub.