Exemple #1
0
 /**
  * 文字列の配列から指定されたコレクション型オブジェクトに変換する.
  *
  * @param collectionType 変換後のコレクション(java.util.List, java.util.Set, java.util.SortedSet)の型
  * @param elementType 変換後のコレクション要素の型
  * @param strElm 変換する文字列、またはその配列
  * @return
  */
 @SuppressWarnings("unchecked")
 public static Collection<?> stringToCollectionValue(
     Class collectionType, Class elementType, String... strElm) throws IllegalArgumentException {
   if (collectionType == null) {
     throw new NullPointerException("the collectionType parameter is null.");
   }
   if (elementType == null) {
     throw new NullPointerException("the elementType parameter is null.");
   }
   if (strElm == null) {
     throw new NullPointerException("the strElm parameter is null.");
   }
   // コレクションオブジェクトの生成
   Collection collection = null;
   if (ArrayList.class.isAssignableFrom(collectionType)
       || LinkedList.class.isAssignableFrom(collectionType)
       || HashSet.class.isAssignableFrom(collectionType)
       || LinkedHashSet.class.isAssignableFrom(collectionType)
       || TreeSet.class.isAssignableFrom(collectionType)) {
     try {
       collection = (Collection<?>) collectionType.newInstance();
     } catch (Exception e) {
       throw new IllegalArgumentException(
           "the collectionType parameter is not supported type. "
               + "usable Collection(java.util.ArrayList, java.util.LinkedList, java.util.HashSet, "
               + "java.util.LinkedHashSet, java.util.TreeSet, java.util.List, java.util.Set, java.util.SortedSet) of a core datastore type.",
           e);
     }
   } else if (List.class.isAssignableFrom(collectionType)) {
     collection = new ArrayList(strElm.length);
   } else if (SortedSet.class.isAssignableFrom(collectionType)) {
     collection = new TreeSet();
   } else if (Set.class.isAssignableFrom(collectionType)) {
     collection = new HashSet(strElm.length);
   } else {
     throw new IllegalArgumentException(
         "the collectionType parameter is not supported type. "
             + "usable Collection(java.util.ArrayList, java.util.LinkedList, java.util.HashSet, "
             + "java.util.LinkedHashSet, java.util.TreeSet, java.util.List, java.util.Set, java.util.SortedSet) of a core datastore type.");
   }
   // コレクション要素の変換
   for (String elm : strElm) {
     collection.add(ExConversionUtil.convert(elm, elementType));
   }
   return collection;
 }
Exemple #2
0
 /**
  * 文字列を指定された型のオブジェクトに変換する.
  *
  * @param clazz 変換後のオブジェクトの型
  * @param s 変換する文字列
  * @return
  */
 public static <T> T stringToValue(Class<T> clazz, String s) {
   if (clazz == null) {
     throw new NullPointerException("the clazz parameter is null.");
   }
   if (s == null) {
     return null;
   }
   try {
     return ExConversionUtil.convert(s, clazz);
   } catch (IllegalArgumentException e) {
     if (clazz.isArray()) {
       return (T) stringToArrayValue(clazz, s);
     }
   }
   throw new IllegalArgumentException(
       "The class("
           + s.getClass().getName()
           + ") can not be converted to the class("
           + clazz.getName()
           + ").");
 }
Exemple #3
0
 /**
  * 文字列の配列から指定された配列型のオブジェクトに変換する.
  *
  * @param arrayClazz 変換後のオブジェクトの型
  * @param s 変換する文字列(カンマ区切り)
  * @return
  */
 @SuppressWarnings("unchecked")
 public static <T> T stringToArrayValue(Class<T> arrayClazz, String s) {
   if (arrayClazz == null) {
     throw new NullPointerException("the arrayClazz parameter is null.");
   }
   if (s == null) {
     return null;
   }
   String[] arrStr = StrUtil.splittrim(s, ",");
   Object[] arrObj = new Object[arrStr.length];
   for (int i = 0; i < arrStr.length; i++) {
     arrObj[i] = ExConversionUtil.convert(arrStr[i], arrayClazz.getComponentType());
   }
   // プリミティブ型配列に変換
   if (arrayClazz.getComponentType().isPrimitive()) {
     return (T) wrapperToPrimitiveArray(arrObj, arrayClazz);
   }
   // ラッパー型配列に変換
   else {
     return (T) Arrays.copyOf(arrObj, arrObj.length, (Class) arrayClazz);
   }
 }