diff --git a/VERSION b/VERSION index 9e11b32..1d0ba9e 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.3.1 +0.4.0 diff --git a/collections/pom.xml b/collections/pom.xml index e2bfd20..3ab6eb6 100644 --- a/collections/pom.xml +++ b/collections/pom.xml @@ -7,7 +7,7 @@ java-project co.arago - 0.3.1 + 0.4.0 co.arago.util diff --git a/common/README.md b/common/README.md index cabf853..eaaf7c0 100644 --- a/common/README.md +++ b/common/README.md @@ -5,6 +5,6 @@ This is a library containing common classes that are used throughout our project * Reflections: `co.arago.util.reflections.Reflections` * Templates: `co.arago.util.text.templates.*` * Tokenizer: `co.arago.util.text.EscapingStringTokenizer` -* Validation / Field checks: `co.arago.util.validation.RequiredFieldChecks` +* Validation / Field checks: `co.arago.util.validation.ValueChecks` * Deep cloning of serializable objects: `co.arago.util.Cloner` * Get fields of collections, maps and public fields of any object via paths: `co.arago.util.GetByPath` \ No newline at end of file diff --git a/common/pom.xml b/common/pom.xml index dfb86a5..aba9114 100644 --- a/common/pom.xml +++ b/common/pom.xml @@ -7,7 +7,7 @@ co.arago java-project - 0.3.1 + 0.4.0 co.arago.util diff --git a/common/src/main/java/co/arago/util/validation/RequiredFieldChecks.java b/common/src/main/java/co/arago/util/validation/RequiredFieldChecks.java deleted file mode 100644 index 224109f..0000000 --- a/common/src/main/java/co/arago/util/validation/RequiredFieldChecks.java +++ /dev/null @@ -1,121 +0,0 @@ -package co.arago.util.validation; - -import org.apache.commons.lang3.StringUtils; - -import java.util.Collection; -import java.util.Map; -import java.util.Objects; - -/** - * This class provides value checking to the classes that implement it. - */ -public abstract class RequiredFieldChecks { - /** - * Check for null values. Used with required parameters of builder fields. - * - * @param item The item to check for null - * @param name Name of the item for the exception message - * @param Type of any item. - * @return The item itself - * @throws NullPointerException when item == null - */ - public static N notNull(N item, String name) { - return Objects.requireNonNull(item, "Field '" + name + "' is required and cannot be null."); - } - - /** - * Check for null values. Used with required parameters of builder fields. - * - * @param item The item to check for null - * @param name Name of the item for the exception message - * @param Type of any item. - * @return The item itself - * @throws NullPointerException when item == null - */ - public static N[] notNull(String name, N[] item) { - if (item == null) - throw new IllegalArgumentException("Field '" + name + "' is required and cannot be null."); - return item; - } - - /** - * Check for null values. Used with required parameters of builder fields. - * - * @param item The item to check for null - * @param name Name of the item for the exception message - * @param Type of any item. - * @return The item itself - * @throws NullPointerException when item == null - */ - public static N[] notEmpty(String name, N[] item) { - if (item == null || item.length == 0) - throw new IllegalArgumentException("Field '" + name + "' is required and cannot be null or empty."); - return item; - } - - /** - * Check for empty string values. Used with required parameters of builder fields. - * - * @param item The item to check for - * @param name Name of the item for the exception message - * @return The item itself - * @throws IllegalArgumentException when item is empty - */ - public static String notEmpty(String item, String name) { - if (StringUtils.isEmpty(item)) - throw new IllegalArgumentException("Field '" + name + "' cannot be empty."); - return item; - } - - /** - * Check for empty collection values. Used with required parameters of builder fields. - * - * @param item The item to check for - * @param name Name of the item for the exception message - * @return The item itself - * @throws IllegalArgumentException when item is empty - */ - public static Collection notEmpty(Collection item, String name) { - if (item == null || item.isEmpty()) - throw new IllegalArgumentException("Collection '" + name + "' cannot be empty."); - return item; - } - - /** - * Check for empty map values. Used with required parameters of builder fields. - * - * @param item The item to check for - * @param name Name of the item for the exception message - * @return The item itself - * @throws IllegalArgumentException when item is empty - */ - public static Map notEmpty(Map item, String name) { - if (item == null || item.isEmpty()) - throw new IllegalArgumentException("Map '" + name + "' cannot be empty."); - return item; - } - - /** - * Check for blank string values. Used with required parameters of builder fields. - * - * @param item The item to check for - * @param name Name of the item for the exception message - * @return The item itself - * @throws IllegalArgumentException when item is blank - */ - public static String notBlank(String item, String name) { - if (StringUtils.isBlank(item)) - throw new IllegalArgumentException("Field '" + name + "' cannot be blank."); - return item; - } - - /** - * Create an exception for any custom error - * - * @param message The message to display. - * @throws IllegalArgumentException with the message. - */ - public static void anyError(String message) { - throw new IllegalArgumentException(message); - } -} diff --git a/common/src/main/java/co/arago/util/validation/ValueChecks.java b/common/src/main/java/co/arago/util/validation/ValueChecks.java new file mode 100644 index 0000000..2d9d878 --- /dev/null +++ b/common/src/main/java/co/arago/util/validation/ValueChecks.java @@ -0,0 +1,121 @@ +package co.arago.util.validation; + +import org.apache.commons.lang3.StringUtils; + +import java.util.Collection; +import java.util.Map; +import java.util.Objects; + +/** + * This interface provides value checking to the classes that implement it. + */ +public interface ValueChecks { + /** + * Check for null values. + * + * @param value The value to check for null + * @param name Name of the value for the exception message + * @param Type of any value. + * @return The value itself + * @throws NullPointerException when value == null + */ + static N notNull(N value, String name) { + return Objects.requireNonNull(value, "Value '" + name + "' is required and cannot be null."); + } + + /** + * Check for null value arrays. + * + * @param name Name of the value for the exception message + * @param value The value array to check for null + * @param Type of any value. + * @return The value itself + * @throws NullPointerException when value == null + */ + static N[] notNull(String name, N[] value) { + if (value == null) + throw new IllegalArgumentException("Value '" + name + "' is required and cannot be null."); + return value; + } + + /** + * Check for null or empty value array. + * + * @param name Name of the value for the exception message + * @param value The value array to check for null + * @param Type of any value. + * @return The value itself + * @throws NullPointerException when value == null + */ + static N[] notEmpty(String name, N[] value) { + if (value == null || value.length == 0) + throw new IllegalArgumentException("Value '" + name + "' is required and cannot be null or empty."); + return value; + } + + /** + * Check for empty string values. + * + * @param value The value to check for + * @param name Name of the value for the exception message + * @return The value itself + * @throws IllegalArgumentException when value is empty + */ + static String notEmpty(String value, String name) { + if (StringUtils.isEmpty(value)) + throw new IllegalArgumentException("Value '" + name + "' cannot be empty."); + return value; + } + + /** + * Check for empty collection values. + * + * @param value The value to check for + * @param name Name of the value for the exception message + * @return The value itself + * @throws IllegalArgumentException when value is empty + */ + static Collection notEmpty(Collection value, String name) { + if (value == null || value.isEmpty()) + throw new IllegalArgumentException("Collection '" + name + "' cannot be empty."); + return value; + } + + /** + * Check for empty map values. + * + * @param value The value to check for + * @param name Name of the value for the exception message + * @return The value itself + * @throws IllegalArgumentException when value is empty + */ + static Map notEmpty(Map value, String name) { + if (value == null || value.isEmpty()) + throw new IllegalArgumentException("Map '" + name + "' cannot be empty."); + return value; + } + + /** + * Check for blank string values. + * + * @param value The value to check for + * @param name Name of the value for the exception message + * @return The value itself + * @throws IllegalArgumentException when value is blank + */ + static String notBlank(String value, String name) { + if (StringUtils.isBlank(value)) + throw new IllegalArgumentException("Value '" + name + "' cannot be blank."); + return value; + } + + /** + * Create an exception for any custom error + * + * @param message The message to display. + * @throws IllegalArgumentException with the message. + */ + static void anyError(String message) { + throw new IllegalArgumentException(message); + } +} diff --git a/json-schema/pom.xml b/json-schema/pom.xml index 81a6cd9..2f80b43 100644 --- a/json-schema/pom.xml +++ b/json-schema/pom.xml @@ -7,7 +7,7 @@ co.arago java-project - 0.3.1 + 0.4.0 co.arago.util @@ -21,7 +21,7 @@ co.arago.util json - 0.3.1 + 0.4.0 com.kjetland diff --git a/json-surfer/pom.xml b/json-surfer/pom.xml index c4aa880..db403f0 100644 --- a/json-surfer/pom.xml +++ b/json-surfer/pom.xml @@ -7,7 +7,7 @@ co.arago java-project - 0.3.1 + 0.4.0 co.arago.util @@ -22,7 +22,7 @@ co.arago.util json - 0.3.1 + 0.4.0 com.github.jsurfer diff --git a/json/pom.xml b/json/pom.xml index f91e1ca..b68f589 100644 --- a/json/pom.xml +++ b/json/pom.xml @@ -7,7 +7,7 @@ java-project co.arago - 0.3.1 + 0.4.0 co.arago.util diff --git a/json/src/main/java/co/arago/util/json/JsonTools.java b/json/src/main/java/co/arago/util/json/JsonTools.java index dbd9af1..c6ef4c6 100644 --- a/json/src/main/java/co/arago/util/json/JsonTools.java +++ b/json/src/main/java/co/arago/util/json/JsonTools.java @@ -6,7 +6,12 @@ import com.fasterxml.jackson.core.json.JsonReadFeature; import com.fasterxml.jackson.core.json.JsonWriteFeature; import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.*; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.InjectableValues; +import com.fasterxml.jackson.databind.JsonDeserializer; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler; import com.fasterxml.jackson.databind.json.JsonMapper; import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; @@ -33,6 +38,7 @@ public static abstract class Conf> { protected boolean lenientParsing = false; protected boolean escapeNonAscii = false; protected boolean warnOnUnknownProperties = false; + protected boolean autoCloseSource = false; /** * Does not serialize values that are set to 'null' in a json-object to the output. Default is false. @@ -100,6 +106,17 @@ public T setWarnOnUnknownProperties(boolean warnOnUnknownProperties) { return self(); } + /** + * Sets the {@link JsonParser.Feature#AUTO_CLOSE_SOURCE}. + * + * @param autoCloseSource Enable or disable the feature. + * @return {@link #self()} + */ + public T setAutoCloseSource(boolean autoCloseSource) { + this.autoCloseSource = autoCloseSource; + return self(); + } + protected abstract T self(); public abstract JsonTools build(); @@ -134,6 +151,7 @@ protected JsonTools(Conf builder) { mapperBuilder.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, builder.failOnUnknownProperties); mapperBuilder.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, builder.failOnEmptyBeans); mapperBuilder.configure(JsonWriteFeature.ESCAPE_NON_ASCII, builder.escapeNonAscii); + mapperBuilder.configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, builder.autoCloseSource); if (builder.skipNullMapValues) mapperBuilder.serializationInclusion(JsonInclude.Include.NON_NULL); @@ -159,7 +177,8 @@ public boolean handleUnknownProperty( JsonParser p, JsonDeserializer deserializer, Object beanOrClass, - String propertyName) throws IOException { + String propertyName + ) throws IOException { if (log.isWarnEnabled()) { String pointer = p.getParsingContext().getParent().pathAsPointer().toString(); log.warn( @@ -209,8 +228,8 @@ public JsonNode readTree(String data) throws JsonProcessingException { /** * Parse stream into JsonNode tree * - * @param stream A stream to read from - * @return The JsonNode containing the data + * @param stream A stream to read from. JsonParser.Feature AUTO_CLOSE_SOURCE is enabled by default. + * @return The JsonNode containing the data. * @throws IOException On IO error */ public JsonNode readTree(InputStream stream) throws IOException { @@ -232,7 +251,7 @@ public JsonNode readTree(File file) throws IOException { * Transforms a JSON structure read from an inputStream into an Object using injectMap * as additional parameters. * - * @param inputStream Input stream with data + * @param inputStream Input stream with data. JsonParser.Feature AUTO_CLOSE_SOURCE is enabled by default. * @param targetClass The target type of object to create * @param injectMap Map with additional inject values * @param Type of the object @@ -246,7 +265,7 @@ public T toObject(InputStream inputStream, Class targetClass, Map Type of the object * @return The created Object @@ -380,7 +399,7 @@ public T transformObject(Object json, TypeReference typeReference) { * Transforms a JSON structure given as Object into an Object cast by * TypeReference. * - * @param inputStream Input stream with data + * @param inputStream Input stream with data. JsonParser.Feature AUTO_CLOSE_SOURCE is enabled by default. * @param typeReference The target type of object to create * @param Type of the object * @return The created Object @@ -441,7 +460,7 @@ public String toPrettyString(String json) throws IOException { /** * Read an InputStream with json data and return a prettified string. * - * @param stream The stream with json data + * @param stream The stream with json data. JsonParser.Feature AUTO_CLOSE_SOURCE is enabled by default. * @return The prettified json string * @throws IOException If the inputStream contains no valid JSON. */ @@ -506,6 +525,25 @@ public Object toObjectEx(Object json, String className) throws ClassNotFoundExce } + /** + * This function tries to transform either a String or any Object into another object via Jackson. + * + * @param json The json data. String or any Object. String uses {@link JsonTools#toObject(String, String)}, + * everything else uses {@link JsonTools#transformObject(Object, String)}. If the string is blank, null is returned. + * @param clazz Class type of the desired result. The default is {@link Map}. + * @return The generated object or null if no object can be created (String is blank for instance). + * @throws JsonProcessingException On processing error + */ + public T toObjectEx(Object json, Class clazz) throws JsonProcessingException { + if (json instanceof String) { + String str = (String) json; + return str.isBlank() ? null : toObject(str, clazz); + } else { + return transformObject(json, clazz); + } + + } + /** * This function tries to transform either a String or any Object into a map via Jackson. * @@ -514,13 +552,8 @@ public Object toObjectEx(Object json, String className) throws ClassNotFoundExce * @return The generated map or null if no map can be created (String is blank for instance). * @throws IOException When the json is invalid */ - public Object toObjectEx(Object json) throws IOException { - try { - return toObjectEx(json, "java.util.Map"); - } catch (ClassNotFoundException e) { - // will not happen - return null; - } + public Map toObjectEx(Object json) throws IOException { + return toObjectEx(json, Map.class); } /** @@ -548,7 +581,7 @@ public Object toPOJO(File file) throws IOException { /** * Create a POJO structure from an InputStream containing JSON * - * @param inputStream InputStream with JSON data + * @param inputStream InputStream with JSON data. JsonParser.Feature AUTO_CLOSE_SOURCE is enabled by default. * @return The POJO Object * @throws IOException On errors with JSON conversion */ diff --git a/pom.xml b/pom.xml index c2d8c95..a36c39d 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ co.arago java-project - 0.3.1 + 0.4.0 pom