elharo commented on code in PR #450:
URL: https://github.com/apache/commons-text/pull/450#discussion_r1409225221


##########
src/main/java/org/apache/commons/text/TokenFormatterFactory.java:
##########
@@ -0,0 +1,130 @@
+/*
+ * 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.commons.text;
+
+import org.apache.commons.lang3.StringUtils;
+
+public class TokenFormatterFactory {
+
+    /**
+     * Token formatter that returns the token as is.
+     */
+    public static class NoOpFormatter implements TokenFormatter {
+        @Override
+        public String format(char[] prior, int tokenIndex, char[] token) {
+            return new String(token);
+        }
+
+    }
+
+    /**
+     * Token formatter that always returns a constant string, and optionally 
checks the passed in token
+     * for the constant and throws an error when found.
+     */
+    public static class ConstantTokenFormatter implements TokenFormatter {
+
+        /**
+         * The constant to return.
+         */
+        private char[] constant;
+
+        /**
+         * Whether or not to throw an exception if the constant is found.
+         */
+        private boolean failOnConstantFound = true;
+
+        public ConstantTokenFormatter(char constant) {
+            this(new char[] {constant}, true);
+        }
+
+        public ConstantTokenFormatter(char constant, boolean 
failOnConstantFound) {
+            this(new char[] {constant}, failOnConstantFound);
+        }
+
+        public ConstantTokenFormatter(String constant) {
+            this(constant, true);
+        }
+
+        public ConstantTokenFormatter(String constant, boolean 
failOnConstantFound) {
+            this(constant.toCharArray(), failOnConstantFound);
+        }
+
+        public ConstantTokenFormatter(char[] constant, boolean 
failOnConstantFound) {
+            this.constant = constant;
+            this.failOnConstantFound = failOnConstantFound;
+        }
+
+        @Override
+        public String format(char[] prior, int tokenIndex, char[] token) {
+            if (failOnConstantFound) {
+                for (int i = 0; i < token.length; i++) {
+                    boolean match = false;
+                    int t = i;
+                    for (int j = 0; j < constant.length; j++) {
+                        if (token[t] == constant[j]) {
+                            match = true;
+                        } else {
+                            match = false;
+                            break;
+                        }
+                        t++;
+                    }
+                    if (match) {
+                        throw new IllegalArgumentException("Token " + 
tokenIndex + " contains illegal character '" + new String(constant) + "' at 
index " + t);
+                    }
+                }
+            }
+
+            return new String(constant);
+        }
+
+        /**
+         * Set whether to check the token for the constant.
+         * @param checkTokenForConstant whether to check.
+         */
+        public void setFailOnConstantFound(boolean checkTokenForConstant) {
+            this.failOnConstantFound = checkTokenForConstant;
+        }
+
+    }
+
+    /**
+     * Reuseable NoOpFormatter instance.
+     */
+    private static final NoOpFormatter NOOP_FORMATTER = new NoOpFormatter();
+
+    /**
+     * Reuseable Empty String formatter instance.

Review Comment:
   Reusable



##########
src/main/java/org/apache/commons/text/TokenFormatterFactory.java:
##########
@@ -0,0 +1,130 @@
+/*
+ * 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.commons.text;
+
+import org.apache.commons.lang3.StringUtils;
+
+public class TokenFormatterFactory {
+
+    /**
+     * Token formatter that returns the token as is.
+     */
+    public static class NoOpFormatter implements TokenFormatter {
+        @Override
+        public String format(char[] prior, int tokenIndex, char[] token) {
+            return new String(token);
+        }
+
+    }
+
+    /**
+     * Token formatter that always returns a constant string, and optionally 
checks the passed in token
+     * for the constant and throws an error when found.
+     */
+    public static class ConstantTokenFormatter implements TokenFormatter {
+
+        /**
+         * The constant to return.
+         */
+        private char[] constant;
+
+        /**
+         * Whether or not to throw an exception if the constant is found.
+         */
+        private boolean failOnConstantFound = true;
+
+        public ConstantTokenFormatter(char constant) {
+            this(new char[] {constant}, true);
+        }
+
+        public ConstantTokenFormatter(char constant, boolean 
failOnConstantFound) {
+            this(new char[] {constant}, failOnConstantFound);
+        }
+
+        public ConstantTokenFormatter(String constant) {
+            this(constant, true);
+        }
+
+        public ConstantTokenFormatter(String constant, boolean 
failOnConstantFound) {
+            this(constant.toCharArray(), failOnConstantFound);
+        }
+
+        public ConstantTokenFormatter(char[] constant, boolean 
failOnConstantFound) {
+            this.constant = constant;
+            this.failOnConstantFound = failOnConstantFound;
+        }
+
+        @Override
+        public String format(char[] prior, int tokenIndex, char[] token) {
+            if (failOnConstantFound) {
+                for (int i = 0; i < token.length; i++) {
+                    boolean match = false;
+                    int t = i;
+                    for (int j = 0; j < constant.length; j++) {
+                        if (token[t] == constant[j]) {
+                            match = true;
+                        } else {
+                            match = false;
+                            break;
+                        }
+                        t++;
+                    }
+                    if (match) {
+                        throw new IllegalArgumentException("Token " + 
tokenIndex + " contains illegal character '" + new String(constant) + "' at 
index " + t);
+                    }
+                }
+            }
+
+            return new String(constant);
+        }
+
+        /**
+         * Set whether to check the token for the constant.
+         * @param checkTokenForConstant whether to check.
+         */
+        public void setFailOnConstantFound(boolean checkTokenForConstant) {
+            this.failOnConstantFound = checkTokenForConstant;
+        }
+
+    }
+
+    /**
+     * Reuseable NoOpFormatter instance.

Review Comment:
   Reusable



##########
src/main/java/org/apache/commons/text/TokenFormatterFactory.java:
##########
@@ -0,0 +1,130 @@
+/*
+ * 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.commons.text;
+
+import org.apache.commons.lang3.StringUtils;
+
+public class TokenFormatterFactory {
+
+    /**
+     * Token formatter that returns the token as is.
+     */
+    public static class NoOpFormatter implements TokenFormatter {
+        @Override
+        public String format(char[] prior, int tokenIndex, char[] token) {
+            return new String(token);
+        }
+
+    }
+
+    /**
+     * Token formatter that always returns a constant string, and optionally 
checks the passed in token
+     * for the constant and throws an error when found.
+     */
+    public static class ConstantTokenFormatter implements TokenFormatter {
+
+        /**
+         * The constant to return.
+         */
+        private char[] constant;
+
+        /**
+         * Whether or not to throw an exception if the constant is found.
+         */
+        private boolean failOnConstantFound = true;
+
+        public ConstantTokenFormatter(char constant) {
+            this(new char[] {constant}, true);
+        }
+
+        public ConstantTokenFormatter(char constant, boolean 
failOnConstantFound) {
+            this(new char[] {constant}, failOnConstantFound);
+        }
+
+        public ConstantTokenFormatter(String constant) {
+            this(constant, true);
+        }
+
+        public ConstantTokenFormatter(String constant, boolean 
failOnConstantFound) {
+            this(constant.toCharArray(), failOnConstantFound);
+        }
+
+        public ConstantTokenFormatter(char[] constant, boolean 
failOnConstantFound) {
+            this.constant = constant;
+            this.failOnConstantFound = failOnConstantFound;
+        }
+
+        @Override
+        public String format(char[] prior, int tokenIndex, char[] token) {
+            if (failOnConstantFound) {
+                for (int i = 0; i < token.length; i++) {

Review Comment:
   I think you can exit early once you get within token.length - 1 characters 
of the end. That is, no need to check the last 16 characters of token for a 17 
character constant.



##########
src/main/java/org/apache/commons/text/cases/CamelCase.java:
##########
@@ -0,0 +1,40 @@
+/*
+ * 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.commons.text.cases;
+
+/**
+ * Case implementation that parses and formats strings of the form 
'myCamelCase'
+ * <p>
+ * CamelCase is a case where tokens are delimited by upper case Unicode 
characters. The very first
+ * token should begin with a lower case character, and any subsequent tokens 
begin with an
+ * upper case character. All remaining characters will be lower case or non 
cased.
+ * </p>
+ */
+public final class CamelCase extends UpperCaseDelimitedCase {
+
+    /** Constant reusable instance of this case. */
+    public static final CamelCase INSTANCE = new CamelCase();
+
+    /**
+     * Constructs new CamelCase instance.
+     */
+

Review Comment:
   extra blank line



##########
src/main/java/org/apache/commons/text/cases/Cases.java:
##########
@@ -0,0 +1,44 @@
+/*
+ * 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.commons.text.cases;
+
+/**
+ * Util methods for the Cases API.
+ */
+public class Cases {
+
+    /** Constant reusable instance of KebabCase. */
+    public static final KebabCase KEBAB = KebabCase.INSTANCE;
+    /** Constant reusable instance of SnakeCase. */
+    public static final SnakeCase SNAKE = SnakeCase.INSTANCE;
+    /** Constant reusable instance of CamelCase. */
+    public static final CamelCase CAMEL = CamelCase.INSTANCE;
+    /** Constant reusable instance of PascalCase. */
+    public static final PascalCase PASCAL = PascalCase.INSTANCE;
+
+    /**
+     * Utility method for converting between cases.
+     * @param string the cased string to parse

Review Comment:
   blank line before first @param



##########
src/main/java/org/apache/commons/text/matcher/AbstractStringMatcher.java:
##########
@@ -428,6 +428,59 @@ public int size() {
         }
     }
 
+    /**
+     * Matches Uppercase characters as determined by {@link 
java.lang.Character#isUpperCase(int)}
+     * <p>
+     * Thread=safe.
+     * </p>
+     */
+    static final class UppercaseMatcher extends AbstractStringMatcher {
+
+        /**
+         * Constructs a new instance of {@code UppercaseMatcher}.
+         */
+        UppercaseMatcher() {
+        }
+
+        /**
+         * Returns {@code 1} if there is a match, or {@code 0} if there is no 
match.

Review Comment:
   Why not a boolean?



##########
src/main/java/org/apache/commons/text/matcher/AbstractStringMatcher.java:
##########
@@ -428,6 +428,59 @@ public int size() {
         }
     }
 
+    /**
+     * Matches Uppercase characters as determined by {@link 
java.lang.Character#isUpperCase(int)}
+     * <p>
+     * Thread=safe.
+     * </p>
+     */
+    static final class UppercaseMatcher extends AbstractStringMatcher {
+
+        /**
+         * Constructs a new instance of {@code UppercaseMatcher}.
+         */
+        UppercaseMatcher() {
+        }
+
+        /**
+         * Returns {@code 1} if there is a match, or {@code 0} if there is no 
match.
+         *
+         * @param buffer the text content to match against, do not change
+         * @param start the starting position for the match, valid for buffer
+         * @param bufferStart unused
+         * @param bufferEnd unused
+         * @return The number of matching characters, zero for no match
+         */
+        @Override
+        public int isMatch(char[] buffer, int start, int bufferStart, int 
bufferEnd) {
+            int codePoint = Character.codePointAt(buffer, start);
+            return Character.isUpperCase(codePoint) ? 
Character.charCount(codePoint) : 0;
+        }
+
+        /**
+         * Returns {@code 1} if there is a match, or {@code 0} if there is no 
match.

Review Comment:
   why not a boolean?



##########
src/main/java/org/apache/commons/text/matcher/AbstractStringMatcher.java:
##########
@@ -428,6 +428,59 @@ public int size() {
         }
     }
 
+    /**
+     * Matches Uppercase characters as determined by {@link 
java.lang.Character#isUpperCase(int)}
+     * <p>
+     * Thread=safe.
+     * </p>
+     */
+    static final class UppercaseMatcher extends AbstractStringMatcher {
+
+        /**
+         * Constructs a new instance of {@code UppercaseMatcher}.
+         */
+        UppercaseMatcher() {
+        }
+
+        /**
+         * Returns {@code 1} if there is a match, or {@code 0} if there is no 
match.
+         *
+         * @param buffer the text content to match against, do not change
+         * @param start the starting position for the match, valid for buffer
+         * @param bufferStart unused
+         * @param bufferEnd unused
+         * @return The number of matching characters, zero for no match

Review Comment:
   the number



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