You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by ta...@apache.org on 2023/02/22 17:10:37 UTC
[qpid-protonj2] branch main updated: PROTON-2685 Add array search API for symbols
This is an automated email from the ASF dual-hosted git repository.
tabish pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/qpid-protonj2.git
The following commit(s) were added to refs/heads/main by this push:
new 17e0838e PROTON-2685 Add array search API for symbols
17e0838e is described below
commit 17e0838ecc94200922fb219162924ef567c10ae5
Author: Timothy Bish <ta...@gmail.com>
AuthorDate: Wed Feb 22 12:10:21 2023 -0500
PROTON-2685 Add array search API for symbols
Avoids some biolerplate code for looking for a symbol in a standard set
---
.../protonj2/engine/sasl/SaslServerContext.java | 3 +-
.../org/apache/qpid/protonj2/types/Symbol.java | 15 ----
.../org/apache/qpid/protonj2/types/Symbols.java | 88 ++++++++++++++++++++++
.../org/apache/qpid/protonj2/types/SymbolTest.java | 31 +++++++-
4 files changed, 117 insertions(+), 20 deletions(-)
diff --git a/protonj2/src/main/java/org/apache/qpid/protonj2/engine/sasl/SaslServerContext.java b/protonj2/src/main/java/org/apache/qpid/protonj2/engine/sasl/SaslServerContext.java
index d8792fa4..c2ac1a72 100644
--- a/protonj2/src/main/java/org/apache/qpid/protonj2/engine/sasl/SaslServerContext.java
+++ b/protonj2/src/main/java/org/apache/qpid/protonj2/engine/sasl/SaslServerContext.java
@@ -22,6 +22,7 @@ import org.apache.qpid.protonj2.buffer.ProtonBuffer;
import org.apache.qpid.protonj2.engine.Engine;
import org.apache.qpid.protonj2.engine.exceptions.EngineStateException;
import org.apache.qpid.protonj2.types.Symbol;
+import org.apache.qpid.protonj2.types.Symbols;
/**
* SASL Server operating context used by an {@link Engine} that has been
@@ -77,7 +78,7 @@ public interface SaslServerContext extends SaslContext {
* @throws EngineStateException if the engine has already shutdown or failed while processing the mechanisms.
*/
default SaslServerContext sendMechanisms(String[] mechanisms) throws EngineStateException {
- return sendMechanisms(Symbol.getSymbols(mechanisms));
+ return sendMechanisms(Symbols.getSymbols(mechanisms));
}
/**
diff --git a/protonj2/src/main/java/org/apache/qpid/protonj2/types/Symbol.java b/protonj2/src/main/java/org/apache/qpid/protonj2/types/Symbol.java
index f57b79fe..d7a4149b 100644
--- a/protonj2/src/main/java/org/apache/qpid/protonj2/types/Symbol.java
+++ b/protonj2/src/main/java/org/apache/qpid/protonj2/types/Symbol.java
@@ -23,7 +23,6 @@ import java.util.concurrent.ConcurrentHashMap;
import org.apache.qpid.protonj2.buffer.ProtonBuffer;
import org.apache.qpid.protonj2.buffer.ProtonBufferAllocator;
-import org.apache.qpid.protonj2.engine.util.StringUtils;
/**
* Class that represents an AMQP Symbol value. The creation of a Symbol object
@@ -211,18 +210,4 @@ public final class Symbol implements Comparable<Symbol> {
return symbol;
}
-
- /**
- * Look up a set of {@link Symbol} instances that matches the given {@link String}
- * array names of the {@link Symbol} values and return them as a new {@link Symbol}
- * array.
- *
- * @param stringValues
- * The {@link String} array version of the {@link Symbol} values.
- *
- * @return a {@link Symbol} array that matches the given {@link String} array values.
- */
- public static Symbol[] getSymbols(String[] stringValues) {
- return StringUtils.toSymbolArray(stringValues);
- }
}
diff --git a/protonj2/src/main/java/org/apache/qpid/protonj2/types/Symbols.java b/protonj2/src/main/java/org/apache/qpid/protonj2/types/Symbols.java
new file mode 100644
index 00000000..8c248c53
--- /dev/null
+++ b/protonj2/src/main/java/org/apache/qpid/protonj2/types/Symbols.java
@@ -0,0 +1,88 @@
+/*
+ * 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.qpid.protonj2.types;
+
+import java.util.Objects;
+
+import org.apache.qpid.protonj2.engine.util.StringUtils;
+
+/**
+ * This class provides a set of APIs for interacting with sets of Symbol values.
+ */
+public final class Symbols {
+
+ private Symbols() {
+ // No need to create
+ }
+
+ /**
+ * Look up a set of {@link Symbol} instances that matches the given {@link String}
+ * array names of the {@link Symbol} values and return them as a new {@link Symbol}
+ * array.
+ *
+ * @param stringValues
+ * The {@link String} array version of the {@link Symbol} values.
+ *
+ * @return a {@link Symbol} array that matches the given {@link String} array values.
+ */
+ public static Symbol[] getSymbols(String[] stringValues) {
+ return StringUtils.toSymbolArray(stringValues);
+ }
+
+ /**
+ * Given an array of {@link Symbol} values search for a specific value and
+ * return <code>true</code> if the value is found in the array.
+ *
+ * @param symbols
+ * The array of symbols that should be searched (can be null or empty).
+ * @param value
+ * The value to search for in the provided array (cannot be null).
+ *
+ * @return true if the value is found in the symbol array.
+ */
+ public static boolean contains(Symbol[] symbols, String value) {
+ return contains(symbols, Symbol.getSymbol(value));
+ }
+
+ /**
+ * Given an array of {@link Symbol} values search for a specific value and
+ * return <code>true</code> if the value is found in the array.
+ *
+ * @param symbols
+ * The array of symbols that should be searched (can be null or empty).
+ * @param value
+ * The value to search for in the provided array (cannot be null).
+ *
+ * @return true if the value is found in the symbol array.
+ */
+ public static boolean contains(Symbol[] symbols, Symbol value) {
+ Objects.requireNonNull("Value to find cannot be null");
+
+ if (symbols == null || symbols.length == 0) {
+ return false;
+ }
+
+ for (Symbol symbol : symbols) {
+ if (value.equals(symbol)) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+}
diff --git a/protonj2/src/test/java/org/apache/qpid/protonj2/types/SymbolTest.java b/protonj2/src/test/java/org/apache/qpid/protonj2/types/SymbolTest.java
index 911017ff..e4c6cfdf 100644
--- a/protonj2/src/test/java/org/apache/qpid/protonj2/types/SymbolTest.java
+++ b/protonj2/src/test/java/org/apache/qpid/protonj2/types/SymbolTest.java
@@ -18,11 +18,13 @@ package org.apache.qpid.protonj2.types;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.nio.charset.StandardCharsets;
@@ -225,8 +227,8 @@ public class SymbolTest {
public void testGetSymbols() {
String[] symbolStrings = new String[] { "one", "two", "three" };
- Symbol[] symbols1 = Symbol.getSymbols(symbolStrings);
- Symbol[] symbols2 = Symbol.getSymbols(symbolStrings);
+ Symbol[] symbols1 = Symbols.getSymbols(symbolStrings);
+ Symbol[] symbols2 = Symbols.getSymbols(symbolStrings);
assertEquals(symbolStrings.length, symbols1.length);
assertEquals(symbolStrings.length, symbols2.length);
@@ -239,7 +241,28 @@ public class SymbolTest {
@Test
public void testGetSymbolsWithNullOrEmptyArg() {
- assertNull(Symbol.getSymbols(null));
- assertNotNull(Symbol.getSymbols(new String[0]));
+ assertNull(Symbols.getSymbols(null));
+ assertNotNull(Symbols.getSymbols(new String[0]));
+ }
+
+ @Test
+ public void testSymbolsArrayContains() {
+ final Symbol[] symbols = new Symbol[] { Symbol.getSymbol("one"), Symbol.getSymbol("two"), Symbol.getSymbol("three") };
+
+ assertThrows(NullPointerException.class, () -> Symbols.contains(symbols, (String)null));
+ assertThrows(NullPointerException.class, () -> Symbols.contains(symbols, (Symbol)null));
+
+ assertTrue(Symbols.contains(symbols, "one"));
+ assertTrue(Symbols.contains(symbols, "three"));
+ assertTrue(Symbols.contains(symbols, "two"));
+ assertFalse(Symbols.contains(symbols, "four"));
+
+ assertTrue(Symbols.contains(symbols, Symbol.getSymbol("one")));
+ assertTrue(Symbols.contains(symbols, Symbol.getSymbol("two")));
+ assertTrue(Symbols.contains(symbols, Symbol.getSymbol("three")));
+ assertFalse(Symbols.contains(symbols, Symbol.getSymbol("four")));
+
+ assertFalse(Symbols.contains(null, "four"));
+ assertFalse(Symbols.contains(new Symbol[0], "four"));
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org