Skip to content

Commit

Permalink
#11777 change Index to allow null string keys to avoid having to add …
Browse files Browse the repository at this point in the history
…null checks

Signed-off-by: Ludovic Orban <[email protected]>
  • Loading branch information
lorban committed May 14, 2024
1 parent 9160108 commit 33bc4f7
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -486,13 +486,9 @@ static int getServerPort(Request request)
return authority.getPort();

// Is there a scheme with a default port?
String rawScheme = uri.getScheme();
if (StringUtil.isNotBlank(rawScheme))
{
HttpScheme scheme = HttpScheme.CACHE.get(rawScheme);
if (scheme != null && scheme.getDefaultPort() > 0)
return scheme.getDefaultPort();
}
HttpScheme scheme = HttpScheme.CACHE.get(request.getHttpURI().getScheme());
if (scheme != null && scheme.getDefaultPort() > 0)
return scheme.getDefaultPort();

// Is there a local port?
SocketAddress local = request.getConnectionMetaData().getLocalSocketAddress();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public V remove(String s)

public V get(String s)
{
return get(s, 0, s.length());
return get(s, 0, s == null ? 0 : s.length());
}

public V get(ByteBuffer b)
Expand All @@ -73,7 +73,7 @@ public V get(ByteBuffer b)

public V getBest(String s)
{
return getBest(s, 0, s.length());
return getBest(s, 0, s == null ? 0 : s.length());
}

public V getBest(byte[] b, int offset, int len)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,6 @@ public V get(ByteBuffer b, int offset, int len)
return _value[t];
}

@Override
public V getBest(String s)
{
return getBest(0, s, 0, s.length());
}

@Override
public V getBest(String s, int offset, int length)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public interface Index<V>
/**
* Get an exact match from a String key
*
* @param s The key
* @param s The key, possibly null
* @return the value for the string key
*/
V get(String s);
Expand All @@ -45,7 +45,7 @@ public interface Index<V>
/**
* Get an exact match from a String key
*
* @param s The key
* @param s The key, possibly null
* @param offset The offset within the string of the key
* @param len the length of the key
* @return the value for the string / offset / length
Expand All @@ -65,7 +65,7 @@ public interface Index<V>
/**
* Check if there is an exact match from a String key
*
* @param s The key
* @param s The key, possibly null
* @return true if there is a match, false otherwise
*/
default boolean contains(String s)
Expand All @@ -76,7 +76,7 @@ default boolean contains(String s)
/**
* Get the best match from key in a String.
*
* @param s The string
* @param s The string, possibly null
* @param offset The offset within the string of the key
* @param len the length of the key
* @return The value or null if not found
Expand All @@ -87,7 +87,7 @@ default boolean contains(String s)
* Get the best match from key in a String, which may be
* a prefix match or an exact match.
*
* @param s The string
* @param s The string, possibly null
* @return The value or null if not found
*/
V getBest(String s);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,17 @@ public void testIsEmpty(AbstractTrie<Integer> trie) throws Exception
assertTrue(trie.isEmpty());
}

@ParameterizedTest
@MethodSource("emptyImplementations")
public void testGetNullStringKey(AbstractTrie<Integer> trie) throws Exception
{
assertNull(trie.get((String)null));
assertNull(trie.get((String)null, 0, 0));
assertFalse(trie.contains(null));
assertNull(trie.getBest((String)null));
assertNull(trie.getBest((String)null, 0, 0));
}

@ParameterizedTest
@MethodSource("implementations")
public void testIsNotEmpty(AbstractTrie<Integer> trie) throws Exception
Expand Down

0 comments on commit 33bc4f7

Please sign in to comment.