Exemple #1
0
  public static <T> T createFromArg(Class<T> clz, Object value) {
    if (value == null) {
      return null;
    }
    ClassMeta meta = ClassMeta.classMeta(clz);
    List<ConstructorAccess> constructors = meta.oneArgumentConstructors();

    if (constructors.size() == 0) {
      return null;
    } else if (constructors.size() == 1) {
      ConstructorAccess constructorAccess = constructors.get(0);
      Class<?> arg1Type = constructorAccess.parameterTypes()[0];
      if (arg1Type.isInstance(value)) {
        return (T) constructorAccess.create(value);
      } else {
        return (T) constructorAccess.create(coerce(arg1Type, value));
      }
    } else {
      for (ConstructorAccess c : constructors) {
        Class<?> arg1Type = c.parameterTypes()[0];
        if (arg1Type.isInstance(value)) {
          return (T) c.create(value);
        }
      }

      for (ConstructorAccess c : constructors) {
        Class<?> arg1Type = c.parameterTypes()[0];
        if (arg1Type.isAssignableFrom(value.getClass())) {
          return (T) c.create(value);
        }
      }
    }
    return null;
  }
Exemple #2
0
  /**
   * This flattens a list.
   *
   * @param o object that might be a list
   * @param list list to add o to or all of o's items to.
   * @return an object or a list
   */
  public static Object unifyListOrArray(Object o, List list) {

    if (o == null) {
      return null;
    }

    boolean isArray = o.getClass().isArray();

    if (list == null && !isArray && !(o instanceof Iterable)) {
      return o;
    }

    if (list == null) {
      list = new LinkedList();
    }

    if (isArray) {
      int length = Array.getLength(o);

      for (int index = 0; index < length; index++) {

        Object o1 = Array.get(o, index);
        if (o1 instanceof Iterable || o.getClass().isArray()) {
          unifyListOrArray(o1, list);
        } else {
          list.add(o1);
        }
      }
    } else if (o instanceof Collection) {

      Collection i = ((Collection) o);

      for (Object item : i) {

        if (item instanceof Iterable || o.getClass().isArray()) {
          unifyListOrArray(item, list);
        } else {
          list.add(item);
        }
      }

    } else {

      list.add(o);
    }

    return list;
  }
Exemple #3
0
  /**
   * Converts the value to boolean, and if it is null, it uses the default value passed.
   *
   * @param obj value to convert to boolean
   * @param defaultValue default value
   * @return obj converted to boolean
   */
  public static boolean toBoolean(Object obj, boolean defaultValue) {

    if (obj == null) {
      return defaultValue;
    }

    if (obj instanceof Boolean) {
      return ((Boolean) obj).booleanValue();
    } else if (obj instanceof Number || obj.getClass().isPrimitive()) {
      int value = toInt(obj);
      return value != 0 ? true : false;
    } else if (obj instanceof String || obj instanceof CharSequence) {
      String str = Conversions.toString(obj);
      if (str.length() == 0) {
        return false;
      }
      if (str.equals("false")) {
        return false;
      } else {
        return true;
      }
    } else if (Boon.isArray(obj)) {
      return Boon.len(obj) > 0;
    } else if (obj instanceof Collection) {

      if (len(obj) > 0) {
        List list = Lists.list((Collection) obj);
        while (list.remove(null)) {}

        return Lists.len(list) > 0;
      } else {
        return false;
      }
    } else {
      return toBoolean(Conversions.toString(obj));
    }
  }
Exemple #4
0
  /**
   * This flattens a list.
   *
   * @param o object that might be a list
   * @param list list to add o to or all of o's items to.
   * @return an object or a list
   */
  public static Object unifyList(Object o, List list) {

    if (o == null) {
      return null;
    }

    if (list == null) {
      list = new ArrayList();
    }

    if (o instanceof Iterable) {
      Iterable i = ((Iterable) o);

      for (Object item : i) {

        unifyListOrArray(item, list);
      }

    } else {
      list.add(o);
    }

    return list;
  }
Exemple #5
0
  public static <T> T coerceWithFlag(
      TypeType coerceTo, Class<T> clz, boolean[] flag, Object value) {

    flag[0] = true;
    if (value == null) {
      return null;
    }

    if (clz.isInstance(value)) {
      return (T) value;
    }

    switch (coerceTo) {
      case STRING:
      case CHAR_SEQUENCE:
        return (T) value.toString();

      case INT:
      case INTEGER_WRAPPER:
        Integer i = toInt(value);
        if (i == Integer.MIN_VALUE) {
          flag[0] = false;
        }
        return (T) i;

      case SHORT:
      case SHORT_WRAPPER:
        Short s = toShort(value);
        if (s == Short.MIN_VALUE) {
          flag[0] = false;
        }
        return (T) s;

      case BYTE:
      case BYTE_WRAPPER:
        Byte by = toByte(value);
        if (by == Byte.MIN_VALUE) {
          flag[0] = false;
        }
        return (T) by;

      case CHAR:
      case CHAR_WRAPPER:
        Character ch = toChar(value);
        if (ch == (char) 0) {
          flag[0] = false;
        }
        return (T) ch;

      case LONG:
      case LONG_WRAPPER:
        Long l = toLong(value);
        if (l == Long.MIN_VALUE) {
          flag[0] = false;
        }

        return (T) l;

      case DOUBLE:
      case DOUBLE_WRAPPER:
        Double d = toDouble(value);
        if (d == Double.MIN_VALUE) {
          flag[0] = false;
        }

        return (T) d;

      case FLOAT:
      case FLOAT_WRAPPER:
        Float f = toFloat(value);
        if (f == Float.MIN_VALUE) {
          flag[0] = false;
        }
        return (T) f;

      case DATE:
        return (T) toDate(value);

      case BIG_DECIMAL:
        return (T) toBigDecimal(value);

      case BIG_INT:
        return (T) toBigInteger(value);

      case CALENDAR:
        return (T) toCalendar(toDate(value));

      case BOOLEAN:
      case BOOLEAN_WRAPPER:
        return (T) (Boolean) toBooleanOrDie(value);

      case MAP:
        return (T) toMap(value);

      case ARRAY:
      case ARRAY_INT:
      case ARRAY_BYTE:
      case ARRAY_SHORT:
      case ARRAY_FLOAT:
      case ARRAY_DOUBLE:
      case ARRAY_LONG:
      case ARRAY_STRING:
      case ARRAY_OBJECT:
        return toPrimitiveArrayIfPossible(clz, value);

      case COLLECTION:
        return toCollection(clz, value);

      case INSTANCE:
        if (value instanceof Map) {
          return MapObjectConversion.fromMap((Map<String, Object>) value, clz);
        } else if (value instanceof List) {
          return MapObjectConversion.fromList((List<Object>) value, clz);
        } else if (clz.isInstance(value)) {
          return (T) value;
        } else {
          ClassMeta meta = ClassMeta.classMeta(clz);
          List<ConstructorAccess> constructors = meta.oneArgumentConstructors();

          if (constructors.size() == 0) {
            return null;
          } else if (constructors.size() == 1) {
            ConstructorAccess constructorAccess = constructors.get(0);
            Class<?> arg1Type = constructorAccess.parameterTypes()[0];
            if (arg1Type.isInstance(value)) {
              return (T) constructorAccess.create(value);
            } else {
              return (T) constructorAccess.create(coerce(arg1Type, value));
            }
          } else {
            for (ConstructorAccess c : constructors) {
              Class<?> arg1Type = c.parameterTypes()[0];
              if (arg1Type.isInstance(value)) {
                return (T) c.create(value);
              }
            }

            for (ConstructorAccess c : constructors) {
              Class<?> arg1Type = c.parameterTypes()[0];
              if (arg1Type.isAssignableFrom(value.getClass())) {
                return (T) c.create(value);
              }
            }

            flag[0] = false;
            break;
          }
        }

      case ENUM:
        return (T) toEnum((Class<? extends Enum>) clz, value);

      case CLASS:
        return (T) toClass(value);

      case TIME_ZONE:
        return (T) toTimeZone(flag, value);

      case UUID:
        return (T) toUUID(flag, value);

      case CURRENCY:
        return (T) toCurrency(value);

      case OBJECT:
        return (T) value;

      default:
        flag[0] = false;
        break;
    }
    return null;
  }