targetClass) {
/**
* Convert a value object to a collection objects, use for converting the
* iterable/array/iterator/enumeration objects to collection objects.
- *
- * if the value object not belong to above then regard the value object as the first item of
+ *
+ * If the value object not belong to above then regard the value object as the first item of
* collection and convert it.
*
* @param the target class of item of the collection
@@ -218,10 +223,13 @@ public static T convert(Object value, Class targetClass) {
* @param targetItemClass the target class of item of the collection
* @param collectionFactory the constructor of collection
* @param hints the converter hints use for intervening converters
+ * @param supportsSingleton whether to be converted as single element collection if the given
+ * value is not iterable/array/iterator/enumeration.
* @return A collection of items converted according to the target type
*/
public static > C convert(Object value, Class targetItemClass,
- IntFunction collectionFactory, Map hints) {
+ IntFunction collectionFactory, Map hints, boolean supportsSingleton) {
+ shouldNotNull(targetItemClass, "The target item class of the conversion can't be null");
Iterable it = null;
int size = 10;
if (value instanceof Iterable) {
@@ -242,8 +250,11 @@ public static > C convert(Object value, Class targ
Object[] array = wrapArray(value);
size = array.length;
it = convert(iterableOf(wrapArray(array)), targetItemClass, hints);
- } else {
+ } else if (supportsSingleton) {
it = convert(iterableOf(value), targetItemClass, hints);
+ } else {
+ throw new ConversionException(
+ "The given value can't be converted, the target item class of the conversion must be iterable/array/iterator/enumeration");
}
}
final C collection = collectionFactory.apply(size);
@@ -269,13 +280,14 @@ public static T convert(Object value, Class targetClass, Map sourceClass = value.getClass();
if (targetClass.isAssignableFrom(sourceClass)) {
return (T) value;
}
Converter converter = resolveConverter(sourceClass, targetClass);
if (converter != null) {
- LOGGER.finer(() -> String.format("Resolve converter %", converter));
+ LOGGER.finer(() -> String.format("Resolve converter %s", converter));
return converter.apply((S) value, hints);
} else {
Converter stringConverter = resolveConverter(String.class, targetClass);
@@ -331,6 +343,7 @@ public static T convert(Object value, TypeLiteral typeLiteral, Map T[] convert(Object[] value, Class targetItemClass,
IntFunction arrayFactory, Map hints) {
+ shouldNotNull(targetItemClass, "The target item class of the conversion can't be null");
if (value == null) {
return arrayFactory.apply(0);
}
@@ -359,6 +372,7 @@ public static > C convert(Object[] value,
if (value == null) {
return null;
}
+ shouldNotNull(targetItemClass, "The target item class of the conversion can't be null");
C collection = collectionFactory.apply(value.length);
for (T t : convert(iterableOf(value), targetItemClass, hints)) {
collection.add(t);
@@ -376,6 +390,7 @@ public static > C convert(Object[] value,
* @return the converted object
*/
public static T[] convertArray(Object value, Class targetClass, Map hints) {
+ shouldNotNull(targetClass, "The target class can't null");
if (value == null) {
return (T[]) Array.newInstance(targetClass, 0);
} else if (value.getClass().isArray()) {
@@ -412,6 +427,7 @@ public static T[] convertArray(Object value, Class targetClass, Map Map convertMap(Object value, Class keyClass, Class valueClass,
Map hints) {
+ shouldNoneNull(keyClass, valueClass);
if (value == null) {
return null;
} else if (value instanceof Map) {
@@ -552,9 +568,9 @@ static Object convertType(Object value, Type targetType, Map hints) {
"Cannot convert for type " + typeClass + "<" + Arrays.toString(argClasses) + "[]>");
}
} else if (List.class.isAssignableFrom(typeClass) && argClasses.length == 1) {
- result = convert(value, argClasses[0], ArrayList::new, hints);
+ result = convert(value, argClasses[0], ArrayList::new, hints, false);
} else if (Set.class.isAssignableFrom(typeClass) && argClasses.length == 1) {
- result = convert(value, argClasses[0], HashSet::new, hints);
+ result = convert(value, argClasses[0], HashSet::new, hints, false);
} else if (Optional.class.isAssignableFrom(typeClass) && argClasses.length == 1) {
result = Optional.ofNullable(convert(value, argClasses[0], hints));
} else if (Supplier.class.isAssignableFrom(typeClass) && argClasses.length == 1) {
diff --git a/corant-shared/src/main/java/org/corant/shared/resource/Resource.java b/corant-shared/src/main/java/org/corant/shared/resource/Resource.java
index 3564e301a..008f2bbf5 100644
--- a/corant-shared/src/main/java/org/corant/shared/resource/Resource.java
+++ b/corant-shared/src/main/java/org/corant/shared/resource/Resource.java
@@ -22,7 +22,7 @@
import java.nio.channels.ReadableByteChannel;
import java.util.Map;
import java.util.logging.Level;
-import org.corant.shared.util.Resources;
+import java.util.logging.Logger;
import org.corant.shared.util.Streams;
/**
@@ -38,6 +38,8 @@
*/
public interface Resource {
+ Logger logger = Logger.getLogger(Resource.class.getName());
+
String META_CONTENT_TYPE = "Content-Type";
String META_CONTENT_LENGTH = "Content-Length";
String META_LAST_MODIFIED = "Last-Modified";
@@ -54,7 +56,7 @@ default boolean exists() {
/**
* Return a byte array for the content of this resource, please evaluate the size of the resource
* when using it to avoid OOM.
- *
+ *
* NOTE: the stream will be closed after reading.
*
* @throws IOException If I/O errors occur
@@ -92,7 +94,7 @@ default Map getMetadata() {
* Returns metadata information for this resource's corresponding name with the given name and
* type.
*
- * @param
+ * @param the metadata value type class
* @param name the metadata key
* @param type the metadata value type, may involve type conversion
*/
@@ -142,7 +144,7 @@ default InputStream tryOpenInputStream() {
try {
return openInputStream();
} catch (IOException e) {
- Resources.logger.log(Level.WARNING, e,
+ logger.log(Level.WARNING, e,
() -> String.format("Can't not open stream from %s.", getLocation()));
}
return null;
diff --git a/corant-shared/src/main/java/org/corant/shared/resource/WritableResource.java b/corant-shared/src/main/java/org/corant/shared/resource/WritableResource.java
index 868f5dbee..58dea4a76 100644
--- a/corant-shared/src/main/java/org/corant/shared/resource/WritableResource.java
+++ b/corant-shared/src/main/java/org/corant/shared/resource/WritableResource.java
@@ -19,7 +19,6 @@
import java.nio.channels.WritableByteChannel;
import java.nio.file.OpenOption;
import java.util.logging.Level;
-import org.corant.shared.util.Resources;
/**
* corant-shared
@@ -39,7 +38,7 @@ default OutputStream tryOpenOutputStream() {
try {
return openOutputStream();
} catch (IOException e) {
- Resources.logger.log(Level.WARNING, e,
+ logger.log(Level.WARNING, e,
() -> String.format("Can't not open stream from %s.", getLocation()));
}
return null;
diff --git a/corant-shared/src/main/java/org/corant/shared/util/Conversions.java b/corant-shared/src/main/java/org/corant/shared/util/Conversions.java
index ee8009346..d1e2684f6 100644
--- a/corant-shared/src/main/java/org/corant/shared/util/Conversions.java
+++ b/corant-shared/src/main/java/org/corant/shared/util/Conversions.java
@@ -101,7 +101,7 @@ public static BigDecimal toBigDecimal(Object obj) {
/**
* Convert an object to BigDecimal object, supports String to BigDecimal Number to BigDecimal,
* return the given alternative object if the converted object is null else return the converted
- * obejct. Support converting String or Numeric type to BigDecimal type.
+ * object. Support converting String or Numeric type to BigDecimal type.
*
* @param obj the object that to be converted
* @param altVal the alternative object if converted object is null
@@ -116,7 +116,7 @@ public static BigDecimal toBigDecimal(Object obj, BigDecimal altVal) {
/**
* Convert an object to rounding BigDecimal object with scale, support converting String or
* Numeric type to BigDecimal type. Return the given alternative object if the converted object is
- * null else return the converted obejct.
+ * null else return the converted object.
*
* @param obj the object that to be converted
* @param altVal the alternative object if converted object is null
@@ -225,7 +225,7 @@ public static Byte toByte(Object obj) {
/**
* Convert an object to Byte object, support converting String (normal or Hex) or Numeric type to
* Byte type, Float or Double type object may involve rounding or truncation. Return the given
- * alternative object if the converted object is null else return the converted obejct.
+ * alternative object if the converted object is null else return the converted object.
*
* @param obj the object that to be converted
* @param altVal the alternative object if converted object is null
@@ -269,7 +269,7 @@ public static Class> toClass(Object obj) {
*/
public static > C toCollection(Object obj, Class itemClass,
IntFunction collectionFactory) {
- return Conversion.convert(obj, itemClass, collectionFactory, null);
+ return Conversion.convert(obj, itemClass, collectionFactory, null, true);
}
/**
@@ -377,12 +377,9 @@ public static List toList(Object obj, Class clazz) {
public static List toList(Object obj, Class clazz, Map hints) {
if (obj == null) {
return null;
- } else if (obj instanceof Collection) {
- return Conversion.convert((Collection>) obj, ArrayList::new, clazz, hints);
- } else if (obj.getClass().isArray()) {
- return Conversion.convert(wrapArray(obj), ArrayList::new, clazz, hints);
+ } else {
+ return Conversion.convert(obj, clazz, ArrayList::new, hints, false);
}
- return Conversion.convert(obj, clazz, ArrayList::new, hints);// FIXME weird
}
public static List toList(Object obj, Function