From 48da75bb8e726079680c75252574f2047eb9662a Mon Sep 17 00:00:00 2001 From: "Mateusz \"Serafin\" Gajewski" Date: Fri, 7 Apr 2023 21:07:55 +0200 Subject: [PATCH] Add PowerOfTwo validator --- .../java/io/airlift/units/PowerOfTwo.java | 38 +++++++++++++++++++ .../io/airlift/units/PowerOfTwoValidator.java | 32 ++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 src/main/java/io/airlift/units/PowerOfTwo.java create mode 100644 src/main/java/io/airlift/units/PowerOfTwoValidator.java diff --git a/src/main/java/io/airlift/units/PowerOfTwo.java b/src/main/java/io/airlift/units/PowerOfTwo.java new file mode 100644 index 0000000..46f0dd2 --- /dev/null +++ b/src/main/java/io/airlift/units/PowerOfTwo.java @@ -0,0 +1,38 @@ +/* + * Licensed 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 io.airlift.units; + +import javax.validation.Constraint; +import javax.validation.Payload; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import static java.lang.annotation.ElementType.ANNOTATION_TYPE; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +@Target({METHOD, ANNOTATION_TYPE}) +@Retention(RUNTIME) +@Documented +@Constraint(validatedBy = PowerOfTwoValidator.class) +public @interface PowerOfTwo +{ + String message() default "is not a power of two"; + + Class[] groups() default {}; + + Class[] payload() default {}; +} diff --git a/src/main/java/io/airlift/units/PowerOfTwoValidator.java b/src/main/java/io/airlift/units/PowerOfTwoValidator.java new file mode 100644 index 0000000..1ff9c51 --- /dev/null +++ b/src/main/java/io/airlift/units/PowerOfTwoValidator.java @@ -0,0 +1,32 @@ +/* + * Licensed 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 io.airlift.units; + +import javax.validation.ConstraintValidator; +import javax.validation.ConstraintValidatorContext; + +public class PowerOfTwoValidator + implements ConstraintValidator +{ + @Override + public void initialize(PowerOfTwo powerOfTwo) + { + } + + @Override + public boolean isValid(Integer value, ConstraintValidatorContext context) + { + return value == null || Integer.bitCount(value) == 1; + } +}