You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2020/06/27 14:41:48 UTC

[commons-text] 06/09: Replace internal MapStringLookup with FunctionStringLookup.

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

commit b0495d88e37e30ca932bf17c85b08cd95fe5f164
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jun 27 10:19:53 2020 -0400

    Replace internal MapStringLookup with FunctionStringLookup.
---
 .../commons/text/lookup/MapStringLookup.java       | 99 ----------------------
 .../commons/text/lookup/StringLookupFactory.java   |  5 +-
 .../commons/text/lookup/MapStringLookupTest.java   | 51 -----------
 3 files changed, 3 insertions(+), 152 deletions(-)

diff --git a/src/main/java/org/apache/commons/text/lookup/MapStringLookup.java b/src/main/java/org/apache/commons/text/lookup/MapStringLookup.java
deleted file mode 100644
index d7d9a94..0000000
--- a/src/main/java/org/apache/commons/text/lookup/MapStringLookup.java
+++ /dev/null
@@ -1,99 +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.util.Map;
-import java.util.Objects;
-
-/**
- * A map-based lookup where the request for a lookup is answered with the value for that key.
- *
- * @param <V>
- *            A map's value type
- *
- * @since 1.3
- */
-final class MapStringLookup<V> implements StringLookup {
-
-    /**
-     * Creates a new instance backed by a Map. Used by the default lookup.
-     *
-     * @param <T>
-     *            the map's value type.
-     * @param map
-     *            the map of keys to values, may be null.
-     * @return a new instance backed by the given map.
-     */
-    static <T> MapStringLookup<T> on(final Map<String, T> map) {
-        return new MapStringLookup<>(map);
-    }
-
-    /**
-     * Map keys are variable names and value.
-     */
-    private final Map<String, V> map;
-
-    /**
-     * Creates a new instance backed by a Map. Used by the default lookup.
-     *
-     * @param map
-     *            the map of keys to values, may be null.
-     */
-    private MapStringLookup(final Map<String, V> map) {
-        this.map = map;
-    }
-
-    /**
-     * Gets the map used in lookups.
-     *
-     * @return The map used in lookups.
-     */
-    Map<String, V> getMap() {
-        return map;
-    }
-
-    /**
-     * Looks up a String key to a String value using the map.
-     * <p>
-     * If the map is null, then null is returned. The map result object is converted to a string using toString().
-     * </p>
-     *
-     * @param key
-     *            the key to be looked up, may be null.
-     * @return The matching value, null if no match
-     */
-    @Override
-    public String lookup(final String key) {
-        if (map == null) {
-            return null;
-        }
-        final V obj;
-        try {
-            obj = map.get(key);
-        } catch (final NullPointerException e) {
-            // Could be a ConcurrentHashMap and a null key request
-            return null;
-        }
-        return Objects.toString(obj, null);
-    }
-
-    @Override
-    public String toString() {
-        return super.toString() + " [map=" + map + "]";
-    }
-
-}
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 fc213d2..6589068 100644
--- a/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java
+++ b/src/main/java/org/apache/commons/text/lookup/StringLookupFactory.java
@@ -537,7 +537,8 @@ public final class StringLookupFactory {
     }
 
     /**
-     * Returns a new function-based lookup where the request for a lookup is answered by applying the function with a lookup key.
+     * Returns a new function-based lookup where the request for a lookup is answered by applying the function with a
+     * lookup key.
      *
      * @param <V> the function input type.
      * @param function the function.
@@ -697,7 +698,7 @@ public final class StringLookupFactory {
      * @return a new MapStringLookup.
      */
     public <V> StringLookup mapStringLookup(final Map<String, V> map) {
-        return MapStringLookup.on(map);
+        return FunctionStringLookup.on(map);
     }
 
     /**
diff --git a/src/test/java/org/apache/commons/text/lookup/MapStringLookupTest.java b/src/test/java/org/apache/commons/text/lookup/MapStringLookupTest.java
deleted file mode 100644
index d358b24..0000000
--- a/src/test/java/org/apache/commons/text/lookup/MapStringLookupTest.java
+++ /dev/null
@@ -1,51 +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.util.HashMap;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-
-import org.junit.jupiter.api.Assertions;
-import org.junit.jupiter.api.Test;
-
-/**
- * Tests {@link MapStringLookup}.
- */
-public class MapStringLookupTest {
-
-    @Test
-    public void testConcurrentHashMapNull() {
-        Assertions.assertNull(MapStringLookup.on(new ConcurrentHashMap<String, Object>()).lookup(null));
-    }
-
-    @Test
-    public void testHashMapNull() {
-        Assertions.assertNull(MapStringLookup.on(new HashMap<String, Object>()).lookup(null));
-    }
-
-    @Test
-    public void testOne() {
-        final String key = "key";
-        final String value = "value";
-        final Map<String, String> map = new HashMap<>();
-        map.put(key, value);
-        Assertions.assertEquals(value, MapStringLookup.on(map).lookup(key));
-    }
-
-}