rishabhdaim commented on code in PR #1793:
URL: https://github.com/apache/jackrabbit-oak/pull/1793#discussion_r1800505172


##########
oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/conditions/Checks.java:
##########
@@ -0,0 +1,118 @@
+package org.apache.jackrabbit.oak.commons.conditions;

Review Comment:
   missing license header.



##########
oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/conditions/Checks.java:
##########
@@ -0,0 +1,118 @@
+package org.apache.jackrabbit.oak.commons.conditions;
+
+import java.util.Objects;
+
+import org.apache.jackrabbit.oak.commons.properties.SystemPropertySupplier;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public final class Checks {
+
+    private Checks() {
+        // no instances for you
+    }
+
+    private static Logger LOG = LoggerFactory.getLogger(Checks.class);
+
+    // when true, message template are arguments checked even when the 
condition
+    // to check for is true
+    private static boolean CHECKMESSAGETEMPLATE = SystemPropertySupplier
+            .create("oak.precondition.checks.CHECKMESSAGETEMPLATE", 
false).loggingTo(LOG).get();
+
+    /**
+     * Checks the specified expression
+     * 
+     * @param expression
+     *            to check
+     * @throws IllegalArgumentException
+     *             when false
+     */
+    public static void checkArgument(boolean expression) throws 
IllegalArgumentException {
+        if (!expression) {
+            throw new IllegalArgumentException();
+        }
+    }
+
+    /**
+     * Checks the specified expression
+     * 
+     * @param expression
+     *            to check
+     * @param message
+     *            to use in exception
+     * @throws IllegalArgumentException
+     *             when false
+     */
+    public static void checkArgument(boolean expression, @NotNull String 
message) throws IllegalArgumentException {
+
+        Objects.requireNonNull(message);
+
+        if (!expression) {
+            throw new IllegalArgumentException(message);
+        }
+    }
+
+    /**
+     * Checks the specified expression
+     * 
+     * @param expression
+     *            to checks
+     * @param messageTemplate
+     *            to use in exception (using {@link String#format} syntax
+     * @throws IllegalArgumentException
+     *             when false
+     */
+    public static void checkArgument(boolean expression, @NotNull String 
messageTemplate, @Nullable Object... messageArgs) {
+
+        Objects.requireNonNull(messageTemplate);
+
+        if (CHECKMESSAGETEMPLATE) {
+            checkTemplate(messageTemplate, messageArgs);
+        }
+
+        if (!expression) {
+            if (!CHECKMESSAGETEMPLATE) {
+                checkTemplate(messageTemplate, messageArgs);
+            }
+
+            String message = String.format(messageTemplate, messageArgs);
+            throw new IllegalArgumentException(message);
+        }
+    }
+
+    protected static boolean checkTemplate(@NotNull String messageTemplate, 
@Nullable Object... messageArgs) {
+        int argsSpecified = messageArgs.length;
+        int argsInTemplate = countArguments(messageTemplate);
+        boolean result = argsSpecified == argsInTemplate;
+        if (argsSpecified != argsInTemplate) {
+            LOG.error("Invalid message format: template '{}', argument count 
{}", messageTemplate, argsSpecified);
+        }
+        return result;
+    }
+
+    protected static int countArguments(String template) {

Review Comment:
   same as above.



##########
oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/conditions/Checks.java:
##########
@@ -0,0 +1,118 @@
+package org.apache.jackrabbit.oak.commons.conditions;
+
+import java.util.Objects;
+
+import org.apache.jackrabbit.oak.commons.properties.SystemPropertySupplier;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public final class Checks {
+
+    private Checks() {
+        // no instances for you
+    }
+
+    private static Logger LOG = LoggerFactory.getLogger(Checks.class);
+
+    // when true, message template are arguments checked even when the 
condition
+    // to check for is true
+    private static boolean CHECKMESSAGETEMPLATE = SystemPropertySupplier
+            .create("oak.precondition.checks.CHECKMESSAGETEMPLATE", 
false).loggingTo(LOG).get();
+
+    /**
+     * Checks the specified expression
+     * 
+     * @param expression
+     *            to check
+     * @throws IllegalArgumentException
+     *             when false
+     */
+    public static void checkArgument(boolean expression) throws 
IllegalArgumentException {
+        if (!expression) {
+            throw new IllegalArgumentException();
+        }
+    }
+
+    /**
+     * Checks the specified expression
+     * 
+     * @param expression
+     *            to check
+     * @param message
+     *            to use in exception
+     * @throws IllegalArgumentException
+     *             when false
+     */
+    public static void checkArgument(boolean expression, @NotNull String 
message) throws IllegalArgumentException {
+
+        Objects.requireNonNull(message);
+
+        if (!expression) {
+            throw new IllegalArgumentException(message);
+        }
+    }
+
+    /**
+     * Checks the specified expression
+     * 
+     * @param expression
+     *            to checks
+     * @param messageTemplate
+     *            to use in exception (using {@link String#format} syntax
+     * @throws IllegalArgumentException
+     *             when false
+     */
+    public static void checkArgument(boolean expression, @NotNull String 
messageTemplate, @Nullable Object... messageArgs) {
+
+        Objects.requireNonNull(messageTemplate);
+
+        if (CHECKMESSAGETEMPLATE) {
+            checkTemplate(messageTemplate, messageArgs);
+        }
+
+        if (!expression) {
+            if (!CHECKMESSAGETEMPLATE) {
+                checkTemplate(messageTemplate, messageArgs);
+            }
+
+            String message = String.format(messageTemplate, messageArgs);
+            throw new IllegalArgumentException(message);
+        }
+    }
+
+    protected static boolean checkTemplate(@NotNull String messageTemplate, 
@Nullable Object... messageArgs) {

Review Comment:
   the scope should be `default`.
   Since it is final and cannot be extended, it doesn't make sense to use a 
`protected` scope.



-- 
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]

Reply via email to