Пример #1
1
    public static <T> Prop<T> cannonicalize(Prop<T> p) {
      Prop<T> prop = cannon.computeIfAbsent(p.name, x -> p);
      if (p.isCannon() && !prop.isCannon()) {
        cannon.put(p.name, p);
        prop = p;
      } else if (p.isCannon() && prop.isCannon() && p != prop) {
        // should be an Error?
        System.err.println(" WARNING: two competing canonical definitions of a Prop <" + p + ">");
        if (p.typeInformation != null && prop.typeInformation == null) {
          cannon.put(p.name, p);
          prop = p;
        } else if (p.typeInformation == null && prop.typeInformation != null) {

        } else if (p.typeInformation != null && prop.typeInformation != null) {
          if (!Conversions.typeInformationEquals(p.typeInformation, prop.typeInformation)) {
            System.err.println(
                " ERROR: the two competing canonical definitions of "
                    + p
                    + " have different type information");
            throw new IllegalArgumentException(
                p.typeInformation + " " + prop.typeInformation + " " + p + " " + prop);
          }
        }
      }
      prop.setCannon();
      return prop;
    }
Пример #2
0
  public static boolean toBooleanOrDie(Object obj) {

    if (obj == null) {
      die("Can't convert boolean from a null");
    }

    if (obj instanceof Boolean) {
      return ((Boolean) obj).booleanValue();
    } 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 if (str.equals("true")) {
        return true;
      }
      die("Can't convert string", obj, "to boolean ");
      return false;
    } else {
      die("Can't convert", obj, "to boolean ");
      return false;
    }
  }
Пример #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));
    }
  }
Пример #4
0
  public static <T> T[] toArray(Class<T> componentType, Collection<T> collection) {
    T[] array = (T[]) Array.newInstance(componentType, collection.size());

    if (componentType.isAssignableFrom(getComponentType(collection))) {
      return collection.toArray(array);
    } else {

      int index = 0;
      for (Object o : collection) {
        array[index] = Conversions.coerce(componentType, o);
        index++;
      }
      return array;
    }
  }
Пример #5
0
    @CallerSensitive
    public <T> Prop<T> type() {

      Prop on = this;
      if (!isCannon()) {
        Prop<T> already = (Prop<T>) findCannon();
        if (already == null) {
          toCannon();
          on.setCannon();
        } else {
          on = already;
        }
      }

      Class c = sun.reflect.Reflection.getCallerClass(2);

      on.definedInClass = c;

      Field f = null;
      try {
        f = c.getField(name);
      } catch (NoSuchFieldException e) {

        try {
          f = c.getField("_" + name);
        } catch (NoSuchFieldException e3) {
          if (name.startsWith("_"))
            try {
              f = c.getField(name.substring(1));
            } catch (NoSuchFieldException e1) {
              if (name.startsWith("__"))
                try {
                  f = c.getField(name.substring(1));
                } catch (NoSuchFieldException e2) {
                }
            }
        }
      }
      if (f == null)
        throw new IllegalStateException(
            " cannot type a Dict.Prop<T> that we can't find. Name is :" + name + " class is :" + c);

      on.typeInformation = Conversions.linearize(f.getGenericType());

      on.typeInformation.remove(0);

      return (Prop<T>) on;
    }
  /**
   * Builder for {@link PatternMatchingConversion} used to configure it.
   *
   * @param <Source> type to be converted
   * @param <Result> type of the conversion result
   */
  public static class Builder<Source, Result> {
    private final List<Map.Entry<Predicate<Source>, Conversion<Source, Result>>> cases =
        new ArrayList<>();
    private Conversion<Source, Result> defaultConversion =
        Conversions.throwing("Can't find conversion for the given source");

    /**
     * Specify specification on the conversion source and corresponding object for such case.
     *
     * @param condition predicate on conversion source returning true if the given conversion should
     *     be applied.
     * @param conversion conversion to be applied on sources satisfying the given predicate
     * @return this instance
     */
    public Builder<Source, Result> inCaseOf(
        Predicate<Source> condition, Conversion<Source, Result> conversion) {
      cases.add(new AbstractMap.SimpleEntry<>(condition, conversion));

      return this;
    }

    /**
     * Specify default conversion which will be called when there is no match for the given source.
     *
     * @param defaultConversion default conversion
     * @return this instance
     */
    public Builder<Source, Result> otherwise(Conversion<Source, Result> defaultConversion) {
      this.defaultConversion = defaultConversion;

      return this;
    }

    /**
     * Build a {@link PatternMatchingConversion} based on the given configuration.
     *
     * @return initialized pattern matching conversion
     */
    public Conversion<Source, Result> build() {
      return new PatternMatchingConversion<>(cases, defaultConversion);
    }
  }
Пример #7
0
 public static <V> V[] array(Class<V> type, final Collection<V> array) {
   return Conversions.toArray(type, array);
 }