Skip to content

Commit

Permalink
Validate recycler capacity in constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
ppkarwasz committed Mar 28, 2024
1 parent a8eb765 commit 7624f54
Showing 1 changed file with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.logging.log4j.kit.recycler;

import org.apache.logging.log4j.kit.env.Log4jProperty;
import org.apache.logging.log4j.status.StatusLogger;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

Expand All @@ -35,14 +36,18 @@ public record RecyclerProperties(@Nullable String factory, @Nullable Integer cap
private static final int DEFAULT_CAPACITY =
Math.max(2 * Runtime.getRuntime().availableProcessors() + 1, 8);

@Override
public Integer capacity() {
if (capacity == null) {
return DEFAULT_CAPACITY;
}
if (capacity < 1) {
throw new IllegalArgumentException("was expecting a `capacity` greater than 1, found: " + capacity);
public RecyclerProperties {
capacity = validateCapacity(capacity);
}

private static Integer validateCapacity(final @Nullable Integer capacity) {
if (capacity != null) {
if (capacity >= 1) {
return capacity;
}
StatusLogger.getLogger()
.warn("Invalid recycler capacity {}, using default capacity {}.", capacity, DEFAULT_CAPACITY);
}
return capacity;
return DEFAULT_CAPACITY;
}
}

0 comments on commit 7624f54

Please sign in to comment.