Example #1
0
    public static AssetUploaderInput parseObject(Object objectToParse) {
      if (objectToParse instanceof String) {
        String stringToParse = (String) objectToParse;
        try {
          return AssetUploaderInput.valueOf(stringToParse.toUpperCase());
        } catch (IllegalArgumentException ignored) {
        }
      }

      return AssetUploaderInput.UNKNOWN;
    }
Example #2
0
  @SuppressWarnings("unchecked")
  public static Optional<Uploader> parseJsonObject(JSONObject parse, MultiTypeFormatter formatter) {

    long id = Long.MIN_VALUE;
    String name = null;
    boolean admin = false;
    // Loop em todas entradas do JSON
    for (Map.Entry entries : (Set<Map.Entry>) parse.entrySet()) {

      Object key = entries.getKey();
      Object value = entries.getValue();
      String valueString = String.valueOf(value);
      /** Transforma o objeto em um {@link AssetUploaderInput) para usar com switch **/
      switch (AssetUploaderInput.parseObject(key)) {
        case ADMIN:
          {
            // Obtem o valor que indica se quem enviou era administrador
            if (formatter.canFormat(Boolean.class)) {
              Optional<Boolean> result = formatter.format(value, Boolean.class);
              if (result.isPresent()) admin = result.get();
            }
            break;
          }
        case ID:
          {
            // Obtém o ID do usuário
            id = Long.parseLong(valueString);
            break;
          }
        case LOGIN:
          {
            // Obtém o nome/login do usuário
            name = valueString;
            break;
          }

        default:
          {
            break;
          }
      }
    }
    if (id == Long.MIN_VALUE || name == null) {
      return Optional.absent();
    }
    return Optional.of(new Uploader(name, admin, id));
  }