Exemplo n.º 1
0
  public static String prettyPrintMessageWithLinesTabs(String message) {

    String[] groups;
    String[] records;
    CharBuf charBuf = CharBuf.create(255);

    groups = split(message, DELIMITER);

    int groupNumber = 0;

    int recordNumber = 0;

    for (String group : groups) {
      records = split(group, SUB_DELIM);

      if (records.length > 1) {
        recordNumber = 0;

        charBuf
            .add("\tgrp ")
            .add(Str.lpad("" + groupNumber, 3, ' '))
            .add('\t')
            .add('\t')
            .add("start")
            .add('\n');
        for (String record : records) {
          charBuf
              .add("\t\trec ")
              .add(Str.lpad("" + recordNumber, 3, ' '))
              .add('\t')
              .add('\t')
              .add(record)
              .add('\n');
          recordNumber++;
        }
        charBuf
            .add("\tgrp ")
            .add(Str.lpad("" + groupNumber, 3, ' '))
            .add('\t')
            .add('\t')
            .add("stop")
            .add('\n');
      } else {

        charBuf
            .add("\tgrp ")
            .add(Str.lpad("" + groupNumber, 3, ' '))
            .add('\t')
            .add('\t')
            .add(group)
            .add('\n');
      }
      groupNumber++;
    }

    return charBuf.toString();
  }
Exemplo n.º 2
0
  public static <T extends Enum> T toEnum(Class<T> cls, String value, Enum defaultEnum) {

    T[] enumConstants = cls.getEnumConstants();
    for (T e : enumConstants) {
      if (e.name().equals(value)) {
        return e;
      }
    }

    value = value.toUpperCase().replace('-', '_');
    for (T e : enumConstants) {
      if (e.name().equals(value)) {
        return e;
      }
    }

    value = Str.underBarCase(value);
    for (T e : enumConstants) {
      if (e.name().equals(value)) {
        return e;
      }
    }

    return (T) defaultEnum;
  }
Exemplo n.º 3
0
  public static float toFloat(Object obj) {
    if (obj.getClass() == float.class) {
      return (Float) obj;
    }

    try {
      if (obj instanceof Float) {
        return (Float) obj;
      } else if (obj instanceof Number) {
        return ((Number) obj).floatValue();
      } else if (obj instanceof CharSequence) {
        try {
          return Float.parseFloat(Str.toString(obj));
        } catch (Exception ex) {
          die(String.format("Unable to convert %s to a float", obj.getClass()));
          return Float.MIN_VALUE;
        }
      } else {
      }
    } catch (Exception ex) {

      log.warning(
          String.format(
              "unable to convert to float and there was an exception %s", ex.getMessage()));
    }

    die(String.format("Unable to convert %s to a float", obj.getClass()));
    return Float.MIN_VALUE;
  }
Exemplo n.º 4
0
  public static long toLong(Object obj, final long longDefault) {

    if (obj instanceof Long) {
      return (Long) obj;
    }

    if (obj instanceof Number) {
      return ((Number) obj).longValue();
    } else if (obj instanceof CharSequence) {
      String str = Str.toString(obj);
      if (Dates.isJsonDate(str)) {
        return Dates.fromJsonDate(str).getTime();
      }

      try {
        return StringScanner.parseLong(str);
      } catch (Exception ex) {
        return longDefault;
      }
    } else if (obj instanceof Date) {
      return ((Date) obj).getTime();
    } else {
      return toInt(obj);
    }
  }
Exemplo n.º 5
0
  public String clientId() {

    if (count == null) {
      count = new AtomicLong();
    }
    if (clientId == null) {
      clientId = UUID.randomUUID().toString();
    }
    return Str.add(clientId, ".", "" + count.incrementAndGet());
  }
Exemplo n.º 6
0
  public static Currency toCurrency(Object obj) {
    if (obj instanceof Currency) {
      return (Currency) obj;
    }

    if (obj instanceof String) {
      String str = Str.toString(obj);
      return Currency.getInstance(str);
    }

    return null;
  }
Exemplo n.º 7
0
 public static int toInt(Object obj, int defaultValue) {
   if (obj.getClass() == int.class) {
     return int.class.cast(obj);
   }
   if (obj instanceof Number) {
     return ((Number) obj).intValue();
   } else if (obj instanceof Boolean || obj.getClass() == Boolean.class) {
     boolean value = toBoolean(obj);
     return value ? 1 : 0;
   } else if (obj instanceof CharSequence) {
     try {
       return StringScanner.parseInt(Str.toString(obj));
     } catch (Exception ex) {
       return defaultValue;
     }
   }
   return defaultValue;
 }
Exemplo n.º 8
0
  /**
   * Gets the field value.
   *
   * @return the value of the field
   */
  public FieldAccess field() {

    FieldAccess field;

    if (path) {
      field = BeanUtils.idxField(objectUnderTest, name.toString());

      if (field == null) {
        return fakeField();
      }

      return field;
    }

    if (name instanceof Enum) {
      name = Str.camelCaseLower(name.toString());
    }

    field = fields().get(name);

    return field;
  }
Exemplo n.º 9
0
 public String logDirectory() {
   return (Str.isEmpty(logDirectory)) ? "/var/log/slumberdb/" : logDirectory;
 }