{"record":{"id":"6099d093268086b6","repo":"apache/cassandra","slug":"only-1-undefined-and-positive-values-are-allowe","errorCode":null,"errorMessage":"Only -1 (undefined) and positive values are allowed; given %d","messagePattern":"Only -1 \\(undefined\\) and positive values are allowed; given (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/config/OptionaldPositiveInt.java","lineNumber":34,"sourceCode":" * limitations under the License.\n */\n\npackage org.apache.cassandra.config;\n\nimport java.util.Objects;\nimport java.util.function.IntSupplier;\n\npublic class OptionaldPositiveInt\n{\n    public static final int UNDEFINED_VALUE = -1;\n    public static final OptionaldPositiveInt UNDEFINED = new OptionaldPositiveInt(UNDEFINED_VALUE);\n\n    private final int value;\n\n    public OptionaldPositiveInt(int value)\n    {\n        if (!(value == -1 || value >= 1))\n            throw new IllegalArgumentException(String.format(\"Only -1 (undefined) and positive values are allowed; given %d\", value));\n        this.value = value;\n    }\n\n    public boolean isDefined()\n    {\n        return value != UNDEFINED_VALUE;\n    }\n\n    public int or(int defaultValue)\n    {\n        return value == UNDEFINED_VALUE ? defaultValue : value;\n    }\n\n    public int or(IntSupplier defaultValue)\n    {\n        return value == UNDEFINED_VALUE ? defaultValue.getAsInt() : value;\n    }\n","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/config/OptionaldPositiveInt.java#L16-L52","documentation":"OptionaldPositiveInt models an int that is either -1 (undefined) or any value >= 1; 0 and values < -1 are meaningless for the settings it backs. The constructor throws IllegalArgumentException for any value outside {-1} ∪ [1, Integer.MAX_VALUE].","triggerScenarios":"Constructing OptionaldPositiveInt(0) or with a negative value other than -1, typically from config parsing where a field was set to 0 or a negative number other than the -1 sentinel.","commonSituations":"Setting a config field like a max threshold to 0 thinking 0 means 'disabled'; computing values programmatically that can go negative; misunderstanding that -1, not 0, is the 'undefined' sentinel.","solutions":["Use -1 to mean undefined/disabled.","Use a positive integer (>= 1) for a defined value.","Fix config/code paths that produce 0 or negative values before constructing."],"exampleFix":"// before\nmaxValue = new OptionaldPositiveInt(0);\n// after\nmaxValue = new OptionaldPositiveInt(-1); // undefined","handlingStrategy":"type-guard","validationCode":"if (!(v == -1 || v >= 1)) throw new IllegalArgumentException(\"expected -1 or positive, got \" + v);","typeGuard":"static boolean isValidOptionalPositive(int v) { return v == -1 || v >= 1; }","tryCatchPattern":"try { new OptionaldPositiveInt(v); } catch (IllegalArgumentException e) { LOG.error(\"bad optional-positive int: {}\", e.getMessage()); }","preventionTips":["Use -1 as the 'undefined' sentinel, never 0","Clamp computed values before construction","Document sentinel semantics in config comments"],"tags":["config","validation","integer"],"backgroundTag":"value-out-of-range","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}