dcapwell commented on code in PR #2245: URL: https://github.com/apache/cassandra/pull/2245#discussion_r1162163111
########## src/java/org/apache/cassandra/config/PositiveInt.java: ########## @@ -0,0 +1,88 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.cassandra.config; + +import java.util.Objects; +import java.util.function.IntSupplier; + +public class PositiveInt +{ + public final int value; + + public PositiveInt(int value) + { + if (value < 1) + throw new IllegalArgumentException(String.format("Only positive values are allowed; given %d", value)); + this.value = value; + } + + private PositiveInt(int value, boolean ignored) + { + this.value = value; + } + + @Override + public String toString() + { + return Integer.toString(value); + } + + @Override + public boolean equals(Object o) + { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + PositiveInt that = (PositiveInt) o; + return value == that.value; + } + + @Override + public int hashCode() + { + return Objects.hash(value); + } + + public static class UndefinedPositiveInt extends PositiveInt Review Comment: the idea is that `PositiveInt` makes sure the values are `>= 1` and for those that require `-1` (atm both users) they use this `UndefinedPositiveInt` that acts as the `null` case. I tried `Optional<PositiveInt>` but this became `Optional<Integer>` at runtime (and would ClassCast if accessed); Jackson is smart enough in this case, but sadly SnakeYaml isn't (FYI @ekaterinadimitrova2, more points for Jackson)... So we need a `null` case where `-1` means null... > Why have a base class PositiveInt and a separate derived UndefinedPositiveInt? I think we need 2, but for this patch we only need `UndefinedPositiveInt`, so I can remove the `extends` and have it its own class... -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

