This is an automated email from the ASF dual-hosted git repository. jamesbognar pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/juneau.git
The following commit(s) were added to refs/heads/master by this push: new 79b7fe3c9 Minor refactoring. 79b7fe3c9 is described below commit 79b7fe3c947af55e10e7a75264dec8a38036a6e4 Author: JamesBognar <james.bog...@salesforce.com> AuthorDate: Fri Jul 1 14:17:37 2022 -0400 Minor refactoring. --- .../org/apache/juneau/collections/JsonList.java | 28 ++++++++++++++++++++++ .../org/apache/juneau/httppart/HttpPartSchema.java | 2 +- .../org/apache/juneau/internal/StringUtils.java | 27 +++------------------ .../org/apache/juneau/jsonschema/SchemaUtils.java | 2 +- .../rest/swagger/BasicSwaggerProviderSession.java | 2 +- .../test/java/org/apache/juneau/BeanMapTest.java | 16 ++++++------- .../apache/juneau/transform/AutoListSwapTest.java | 2 +- 7 files changed, 43 insertions(+), 36 deletions(-) diff --git a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/collections/JsonList.java b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/collections/JsonList.java index 917f8fb70..aaeaa3418 100644 --- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/collections/JsonList.java +++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/collections/JsonList.java @@ -21,6 +21,7 @@ import java.util.*; import java.util.function.*; import org.apache.juneau.*; +import org.apache.juneau.internal.*; import org.apache.juneau.json.*; import org.apache.juneau.marshaller.*; import org.apache.juneau.objecttools.*; @@ -107,8 +108,35 @@ import org.apache.juneau.serializer.*; * @serial exclude */ public class JsonList extends LinkedList<Object> { + + //----------------------------------------------------------------------------------------------------------------- + // Static + //----------------------------------------------------------------------------------------------------------------- + private static final long serialVersionUID = 1L; + /** + * Parses a string that can consist of either a JSON array or comma-delimited list. + * + * <p> + * The type of string is auto-detected. + * + * @param s The string to parse. + * @return The parsed string. + * @throws ParseException Malformed input encountered. + */ + public static JsonList ofJsonOrCdl(String s) throws ParseException { + if (StringUtils.isEmpty(s)) + return null; + if (! StringUtils.isJsonArray(s, true)) + return new JsonList((Object[])StringUtils.split(s.trim(), ',')); + return new JsonList(s); + } + + //----------------------------------------------------------------------------------------------------------------- + // Instance + //----------------------------------------------------------------------------------------------------------------- + transient BeanSession session = null; private transient ObjectRest objectRest; diff --git a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/httppart/HttpPartSchema.java b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/httppart/HttpPartSchema.java index becdb4c2f..54ecf70c4 100644 --- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/httppart/HttpPartSchema.java +++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/httppart/HttpPartSchema.java @@ -4053,7 +4053,7 @@ public class HttpPartSchema { return null; Set<String> set = set(); try { - parseListOrCdl(s).forEach(x -> set.add(x.toString())); + JsonList.ofJsonOrCdl(s).forEach(x -> set.add(x.toString())); } catch (ParseException e) { throw asRuntimeException(e); } diff --git a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/internal/StringUtils.java b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/internal/StringUtils.java index 5d9cf234f..df372a537 100644 --- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/internal/StringUtils.java +++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/internal/StringUtils.java @@ -33,7 +33,6 @@ import java.util.zip.*; import javax.xml.bind.*; import org.apache.juneau.*; -import org.apache.juneau.collections.*; import org.apache.juneau.parser.*; import org.apache.juneau.parser.ParseException; import org.apache.juneau.reflect.*; @@ -125,9 +124,8 @@ public final class StringUtils { * </ul> * If <jk>null</jk> or <c>Number</c>, uses the best guess. * @return The parsed number, or <jk>null</jk> if the string was null. - * @throws ParseException Malformed input encountered. */ - public static Number parseNumber(String s, Class<? extends Number> type) throws ParseException { + public static Number parseNumber(String s, Class<? extends Number> type) { if (s == null) return null; if (s.isEmpty()) @@ -213,9 +211,8 @@ public final class StringUtils { * * @param o The string to convert. * @return The first character of the string if the string is of length 0, or <jk>null</jk> if the string is <jk>null</jk> or empty. - * @throws ParseException If string has a length greater than 1. */ - public static Character parseCharacter(Object o) throws ParseException { + public static Character parseCharacter(Object o) { if (o == null) return null; String s = o.toString(); @@ -223,7 +220,7 @@ public final class StringUtils { return null; if (s.length() == 1) return s.charAt(0); - throw new ParseException("Invalid character: ''{0}''", s); + throw new RuntimeException("Invalid character: '"+s+"'"); } /** @@ -2432,24 +2429,6 @@ public final class StringUtils { return false; } - /** - * Parses a string that can consist of either a JSON array or comma-delimited list. - * - * <p> - * The type of string is auto-detected. - * - * @param s The string to parse. - * @return The parsed string. - * @throws ParseException Malformed input encountered. - */ - public static JsonList parseListOrCdl(String s) throws ParseException { - if (isEmpty(s)) - return null; - if (! isJsonArray(s, true)) - return new JsonList((Object[])StringUtils.split(s.trim(), ',')); - return new JsonList(s); - } - /** * Returns <jk>true</jk> if the specified string is valid JSON. * diff --git a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/jsonschema/SchemaUtils.java b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/jsonschema/SchemaUtils.java index 7739146fb..d983deb07 100644 --- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/jsonschema/SchemaUtils.java +++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/jsonschema/SchemaUtils.java @@ -91,7 +91,7 @@ public class SchemaUtils { if (s.isEmpty()) return null; Set<String> set = set(); - parseListOrCdl(s).forEach(x -> set.add(x.toString())); + JsonList.ofJsonOrCdl(s).forEach(x -> set.add(x.toString())); return set; } diff --git a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/swagger/BasicSwaggerProviderSession.java b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/swagger/BasicSwaggerProviderSession.java index 4307b5d87..cccec86e8 100644 --- a/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/swagger/BasicSwaggerProviderSession.java +++ b/juneau-rest/juneau-rest-server/src/main/java/org/apache/juneau/rest/swagger/BasicSwaggerProviderSession.java @@ -690,7 +690,7 @@ public class BasicSwaggerProviderSession { if (s.isEmpty()) return null; s = resolve(s); - return StringUtils.parseListOrCdl(s); + return JsonList.ofJsonOrCdl(s); } catch (ParseException e) { throw new SwaggerException(e, "Malformed swagger JSON array encountered in "+location+".", locationArgs); } diff --git a/juneau-utest/src/test/java/org/apache/juneau/BeanMapTest.java b/juneau-utest/src/test/java/org/apache/juneau/BeanMapTest.java index 9995da036..ded56761c 100755 --- a/juneau-utest/src/test/java/org/apache/juneau/BeanMapTest.java +++ b/juneau-utest/src/test/java/org/apache/juneau/BeanMapTest.java @@ -1565,7 +1565,7 @@ public class BeanMapTest { // With _type JsonMap m = new JsonMap(session); m.put("_type", "LinkedListOfCalendar"); - m.put("items", JsonList.of("2001-07-04T15:30:45Z")); + m.put("items", JsonList.ofJsonOrCdl("2001-07-04T15:30:45Z")); List l = (List)m.cast(Object.class); assertTrue(l instanceof LinkedList); @@ -1596,7 +1596,7 @@ public class BeanMapTest { // Without _type m = new JsonMap().session(bc.getSession()); - m.put("items", JsonList.of("2001-07-04T15:30:45Z")); + m.put("items", JsonList.ofJsonOrCdl("2001-07-04T15:30:45Z")); l = m.cast(List.class); assertTrue(l instanceof LinkedList); @@ -1718,7 +1718,7 @@ public class BeanMapTest { // With _type JsonMap m = new JsonMap(session); m.put("_type", "String2dArray"); - m.put("items", JsonList.of(JsonList.of("1"),JsonList.of("2"))); + m.put("items", JsonList.of(JsonList.ofJsonOrCdl("1"),JsonList.ofJsonOrCdl("2"))); String[][] l = (String[][])m.cast(Object.class); assertEquals("1", l[0][0]); @@ -1732,7 +1732,7 @@ public class BeanMapTest { // Without _type m = new JsonMap(); - m.put("items", JsonList.of(JsonList.of("1"),JsonList.of("2"))); + m.put("items", JsonList.of(JsonList.ofJsonOrCdl("1"),JsonList.ofJsonOrCdl("2"))); l = m.cast(String[][].class); assertEquals("1", l[0][0]); @@ -1750,7 +1750,7 @@ public class BeanMapTest { // With _type JsonMap m = new JsonMap(session); m.put("_type", "Int2dArray"); - m.put("items", JsonList.of(JsonList.of("1"),JsonList.of("2"))); + m.put("items", JsonList.of(JsonList.ofJsonOrCdl("1"),JsonList.ofJsonOrCdl("2"))); int[][] l = (int[][])m.cast(Object.class); assertEquals(1, l[0][0]); @@ -1764,7 +1764,7 @@ public class BeanMapTest { // Without _type m = new JsonMap(); - m.put("items", JsonList.of(JsonList.of("1"),JsonList.of("2"))); + m.put("items", JsonList.of(JsonList.ofJsonOrCdl("1"),JsonList.ofJsonOrCdl("2"))); l = m.cast(int[][].class); assertEquals(1, l[0][0]); @@ -2028,8 +2028,8 @@ public class BeanMapTest { public void testSettingCollectionPropertyMultipleTimes() throws Exception { BeanMap m = BeanContext.DEFAULT.newBeanMap(Y.class); - m.put("f1", JsonList.of("a")); - m.put("f1", JsonList.of("b")); + m.put("f1", JsonList.ofJsonOrCdl("a")); + m.put("f1", JsonList.ofJsonOrCdl("b")); assertEquals("{f1=[b]}", m.toString()); } diff --git a/juneau-utest/src/test/java/org/apache/juneau/transform/AutoListSwapTest.java b/juneau-utest/src/test/java/org/apache/juneau/transform/AutoListSwapTest.java index 8a2b5d0c6..aae5887b2 100644 --- a/juneau-utest/src/test/java/org/apache/juneau/transform/AutoListSwapTest.java +++ b/juneau-utest/src/test/java/org/apache/juneau/transform/AutoListSwapTest.java @@ -33,7 +33,7 @@ import org.junit.*; public class AutoListSwapTest { private static final List<String> STRINGLIST = list("foo"); - private static final JsonList JSONLIST = JsonList.of("foo"); + private static final JsonList JSONLIST = JsonList.ofJsonOrCdl("foo"); private static ObjectSwap find(Class<?> c) { return AutoListSwap.find(BeanContext.DEFAULT, ClassInfo.of(c));