This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-text.git
The following commit(s) were added to refs/heads/master by this push: new 758bb88 Replace internal Base64DecoderStringLookup and Base64EncoderStringLookup with a FunctionStringLookup. Checkstyle. 758bb88 is described below commit 758bb8804bbca4a8961819fa57e49958b7e3033b Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Sat Jun 27 10:53:27 2020 -0400 Replace internal Base64DecoderStringLookup and Base64EncoderStringLookup with a FunctionStringLookup. Checkstyle. --- .../text/lookup/Base64DecoderStringLookup.java | 69 ------------------ .../text/lookup/Base64EncoderStringLookup.java | 69 ------------------ .../commons/text/lookup/StringLookupFactory.java | 81 ++++++++++++++++++++-- .../text/lookup/Base64DecoderStringLookupTest.java | 6 +- .../text/lookup/Base64EncoderStringLookupTest.java | 6 +- .../text/lookup/StringLookupFactoryTest.java | 6 +- 6 files changed, 84 insertions(+), 153 deletions(-) diff --git a/src/main/java/org/apache/commons/text/lookup/Base64DecoderStringLookup.java b/src/main/java/org/apache/commons/text/lookup/Base64DecoderStringLookup.java deleted file mode 100644 index 26a05d5..0000000 --- a/src/main/java/org/apache/commons/text/lookup/Base64DecoderStringLookup.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * 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.lookup; - -import java.nio.charset.StandardCharsets; -import java.util.Base64; - -import org.apache.commons.text.StringSubstitutor; - -/** - * Decodes Base64 Strings. - * <p> - * Using a {@link StringLookup} from the {@link StringLookupFactory}: - * </p> - * - * <pre> - * StringLookupFactory.INSTANCE.base64DecoderStringLookup().lookup("SGVsbG9Xb3JsZCE="); - * </pre> - * <p> - * Using a {@link StringSubstitutor}: - * </p> - * - * <pre> - * StringSubstitutor.createInterpolator().replace("... ${base64Decoder:SGVsbG9Xb3JsZCE=} ...")); - * </pre> - * <p> - * The above examples convert {@code "SGVsbG9Xb3JsZCE="} to {@code "HelloWorld!"}. - * </p> - * - * @since 1.5 - */ -final class Base64DecoderStringLookup extends AbstractStringLookup { - - /** - * Defines the singleton for this class. - */ - static final Base64DecoderStringLookup INSTANCE = new Base64DecoderStringLookup(); - - /** - * No need to build instances for now. - */ - private Base64DecoderStringLookup() { - // empty - } - - @Override - public String lookup(final String key) { - if (key == null) { - return null; - } - return new String(Base64.getDecoder().decode(key), StandardCharsets.ISO_8859_1); - } - -} diff --git a/src/main/java/org/apache/commons/text/lookup/Base64EncoderStringLookup.java b/src/main/java/org/apache/commons/text/lookup/Base64EncoderStringLookup.java deleted file mode 100644 index 5c30951..0000000 --- a/src/main/java/org/apache/commons/text/lookup/Base64EncoderStringLookup.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * 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.lookup; - -import java.nio.charset.StandardCharsets; -import java.util.Base64; - -import org.apache.commons.text.StringSubstitutor; - -/** - * Encodes Base64 Strings. - * <p> - * Using a {@link StringLookup} from the {@link StringLookupFactory}: - * </p> - * - * <pre> - * StringLookupFactory.INSTANCE.base64EncoderStringLookup().lookup("HelloWorld!"); - * </pre> - * <p> - * Using a {@link StringSubstitutor}: - * </p> - * - * <pre> - * StringSubstitutor.createInterpolator().replace("... ${base64Encoder:HelloWorld!} ...")); - * </pre> - * <p> - * The above examples convert {@code "HelloWorld!"} to {@code "SGVsbG9Xb3JsZCE="}. - * </p> - * - * @since 1.6 - */ -final class Base64EncoderStringLookup extends AbstractStringLookup { - - /** - * Defines the singleton for this class. - */ - static final Base64EncoderStringLookup INSTANCE = new Base64EncoderStringLookup(); - - /** - * No need to build instances for now. - */ - private Base64EncoderStringLookup() { - // empty - } - - @Override - public String lookup(final String key) { - if (key == null) { - return null; - } - return Base64.getEncoder().encodeToString(key.getBytes(StandardCharsets.ISO_8859_1)); - } - -} diff --git a/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java b/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java index d87b2f3..dec504f 100644 --- a/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java +++ b/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java @@ -17,6 +17,8 @@ package org.apache.commons.text.lookup; +import java.nio.charset.StandardCharsets; +import java.util.Base64; import java.util.Map; import java.util.function.Function; @@ -37,13 +39,13 @@ import org.apache.commons.text.StringSubstitutor; * </tr> * <tr> * <td>{@value #KEY_BASE64_DECODER}</td> - * <td>{@link Base64DecoderStringLookup}</td> + * <td>{@link FunctionStringLookup}</td> * <td>{@link #base64DecoderStringLookup()}</td> * <td>1.6</td> * </tr> * <tr> * <td>{@value #KEY_BASE64_ENCODER}</td> - * <td>{@link Base64EncoderStringLookup}</td> + * <td>{@link FunctionStringLookup}</td> * <td>{@link #base64EncoderStringLookup()}</td> * <td>1.6</td> * </tr> @@ -149,7 +151,25 @@ public final class StringLookupFactory { public static final StringLookupFactory INSTANCE = new StringLookupFactory(); /** - * Defines the singleton for this class. + * Looks up keys from environment variables. + * <p> + * Using a {@link StringLookup} from the {@link StringLookupFactory}: + * </p> + * + * <pre> + * StringLookupFactory.INSTANCE.dateStringLookup().lookup("USER"); + * </pre> + * <p> + * Using a {@link StringSubstitutor}: + * </p> + * + * <pre> + * StringSubstitutor.createInterpolator().replace("... ${env:USER} ...")); + * </pre> + * <p> + * The above examples convert (on Linux) {@code "USER"} to the current user name. On Windows 10, you would use + * {@code "USERNAME"} to the same effect. + * </p> */ static final FunctionStringLookup<String> INSTANCE_ENVIRONMENT_VARIABLES = FunctionStringLookup .on(key -> System.getenv(key)); @@ -294,6 +314,53 @@ public final class StringLookupFactory { } /** + * Encodes Base64 Strings. + * <p> + * Using a {@link StringLookup} from the {@link StringLookupFactory}: + * </p> + * + * <pre> + * StringLookupFactory.INSTANCE.base64EncoderStringLookup().lookup("HelloWorld!"); + * </pre> + * <p> + * Using a {@link StringSubstitutor}: + * </p> + * + * <pre> + * StringSubstitutor.createInterpolator().replace("... ${base64Encoder:HelloWorld!} ...")); + * </pre> + * <p> + * The above examples convert {@code "HelloWorld!"} to {@code "SGVsbG9Xb3JsZCE="}. + * </p> + * Defines the singleton for this class. + */ + static final FunctionStringLookup<String> INSTANCE_BASE64_ENCODER = FunctionStringLookup + .on(key -> Base64.getEncoder().encodeToString(key.getBytes(StandardCharsets.ISO_8859_1))); + + /** + * Decodes Base64 Strings. + * <p> + * Using a {@link StringLookup} from the {@link StringLookupFactory}: + * </p> + * + * <pre> + * StringLookupFactory.INSTANCE.base64DecoderStringLookup().lookup("SGVsbG9Xb3JsZCE="); + * </pre> + * <p> + * Using a {@link StringSubstitutor}: + * </p> + * + * <pre> + * StringSubstitutor.createInterpolator().replace("... ${base64Decoder:SGVsbG9Xb3JsZCE=} ...")); + * </pre> + * <p> + * The above examples convert {@code "SGVsbG9Xb3JsZCE="} to {@code "HelloWorld!"}. + * </p> + */ + static final FunctionStringLookup<String> INSTANCE_BASE64_DECODER = FunctionStringLookup + .on(key -> new String(Base64.getDecoder().decode(key), StandardCharsets.ISO_8859_1)); + + /** * No need to build instances for now. */ private StringLookupFactory() { @@ -309,7 +376,7 @@ public final class StringLookupFactory { public void addDefaultStringLookups(final Map<String, StringLookup> stringLookupMap) { if (stringLookupMap != null) { // "base64" is deprecated in favor of KEY_BASE64_DECODER. - stringLookupMap.put("base64", Base64DecoderStringLookup.INSTANCE); + stringLookupMap.put("base64", StringLookupFactory.INSTANCE_BASE64_DECODER); for (final DefaultStringLookup stringLookup : DefaultStringLookup.values()) { stringLookupMap.put(InterpolatorStringLookup.toKey(stringLookup.getKey()), stringLookup.getStringLookup()); @@ -341,7 +408,7 @@ public final class StringLookupFactory { * @since 1.5 */ public StringLookup base64DecoderStringLookup() { - return Base64DecoderStringLookup.INSTANCE; + return StringLookupFactory.INSTANCE_BASE64_DECODER; } /** @@ -368,7 +435,7 @@ public final class StringLookupFactory { * @since 1.6 */ public StringLookup base64EncoderStringLookup() { - return Base64EncoderStringLookup.INSTANCE; + return StringLookupFactory.INSTANCE_BASE64_ENCODER; } /** @@ -397,7 +464,7 @@ public final class StringLookupFactory { */ @Deprecated public StringLookup base64StringLookup() { - return Base64DecoderStringLookup.INSTANCE; + return StringLookupFactory.INSTANCE_BASE64_DECODER; } /** diff --git a/src/test/java/org/apache/commons/text/lookup/Base64DecoderStringLookupTest.java b/src/test/java/org/apache/commons/text/lookup/Base64DecoderStringLookupTest.java index eb8a744..f95ee47 100644 --- a/src/test/java/org/apache/commons/text/lookup/Base64DecoderStringLookupTest.java +++ b/src/test/java/org/apache/commons/text/lookup/Base64DecoderStringLookupTest.java @@ -21,18 +21,18 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; /** - * Tests {@link Base64DecoderStringLookup}. + * Tests {@link StringLookupFactory#INSTANCE_BASE64_DECODER}. */ public class Base64DecoderStringLookupTest { @Test public void test() { - Assertions.assertEquals("HelloWorld!", Base64DecoderStringLookup.INSTANCE.lookup("SGVsbG9Xb3JsZCE=")); + Assertions.assertEquals("HelloWorld!", StringLookupFactory.INSTANCE_BASE64_DECODER.lookup("SGVsbG9Xb3JsZCE=")); } @Test public void testNull() { - Assertions.assertNull(Base64DecoderStringLookup.INSTANCE.lookup(null)); + Assertions.assertNull(StringLookupFactory.INSTANCE_BASE64_DECODER.lookup(null)); } } diff --git a/src/test/java/org/apache/commons/text/lookup/Base64EncoderStringLookupTest.java b/src/test/java/org/apache/commons/text/lookup/Base64EncoderStringLookupTest.java index 9591e20..4d05b81 100644 --- a/src/test/java/org/apache/commons/text/lookup/Base64EncoderStringLookupTest.java +++ b/src/test/java/org/apache/commons/text/lookup/Base64EncoderStringLookupTest.java @@ -21,18 +21,18 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; /** - * Tests {@link Base64EncoderStringLookup}. + * Tests {@link StringLookupFactory#INSTANCE_BASE64_ENCODER}. */ public class Base64EncoderStringLookupTest { @Test public void test() { - Assertions.assertEquals("SGVsbG9Xb3JsZCE=", Base64EncoderStringLookup.INSTANCE.lookup("HelloWorld!")); + Assertions.assertEquals("SGVsbG9Xb3JsZCE=", StringLookupFactory.INSTANCE_BASE64_ENCODER.lookup("HelloWorld!")); } @Test public void testNull() { - Assertions.assertNull(Base64EncoderStringLookup.INSTANCE.lookup(null)); + Assertions.assertNull(StringLookupFactory.INSTANCE_BASE64_ENCODER.lookup(null)); } } diff --git a/src/test/java/org/apache/commons/text/lookup/StringLookupFactoryTest.java b/src/test/java/org/apache/commons/text/lookup/StringLookupFactoryTest.java index ad82c59..71dcbc0 100644 --- a/src/test/java/org/apache/commons/text/lookup/StringLookupFactoryTest.java +++ b/src/test/java/org/apache/commons/text/lookup/StringLookupFactoryTest.java @@ -68,8 +68,10 @@ public class StringLookupFactoryTest { @Test public void testSingletons() { final StringLookupFactory stringLookupFactory = StringLookupFactory.INSTANCE; - Assertions.assertSame(Base64DecoderStringLookup.INSTANCE, stringLookupFactory.base64DecoderStringLookup()); - Assertions.assertSame(Base64EncoderStringLookup.INSTANCE, stringLookupFactory.base64EncoderStringLookup()); + Assertions.assertSame(StringLookupFactory.INSTANCE_BASE64_DECODER, + stringLookupFactory.base64DecoderStringLookup()); + Assertions.assertSame(StringLookupFactory.INSTANCE_BASE64_ENCODER, + stringLookupFactory.base64EncoderStringLookup()); Assertions.assertSame(ConstantStringLookup.INSTANCE, stringLookupFactory.constantStringLookup()); Assertions.assertSame(DateStringLookup.INSTANCE, stringLookupFactory.dateStringLookup()); Assertions.assertSame(DnsStringLookup.INSTANCE, stringLookupFactory.dnsStringLookup());