Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java code duplication refactoring

I use codeclimate to statically analyze my java code. The output says: Similar blocks of code found in 3 locations. Consider refactoring.

What could be the best way to refactor the following code without repeat myself and not losing the "code readability":

public String getString(String component, String key, String defaultValue) throws ConfigException {
    try {
      SingleRequestData config = clientRest.target(this.configServiceUrl).path(CONFIG_ENDPOINT).path(component)
          .path(key).path(defaultValue).request(MediaType.APPLICATION_JSON).get(new GenericType<SingleRequestData>() {
          });
      logger.log(Level.INFO, "Fetched Remote config: {0}={1} for Component: {3}",
          new Object[] { key, config.value, component });
      return config.value;
    } catch (Exception e) {
      logger.log(Level.SEVERE, "{0} : {1}", new Object[] { e.getMessage(), ERROR_MESSAGE });
      throw new ConfigException(e.getMessage() + " " + ERROR_MESSAGE, e.getCause());
    }
  }

  @Override
  public Integer getInteger(String component, String key, int defaultValue) throws ConfigException {
    try {
      String value = this.getString(component, key, String.valueOf(defaultValue));
      return Integer.parseInt(value);
    } catch (Exception e) {
      logger.log(Level.SEVERE, e.getMessage(), e);
      throw new ConfigException(e.getMessage(), e.getCause());
    }
  }

  @Override
  public Double getDouble(String component, String key, double defaultValue) throws ConfigException {
    try {
      String value = this.getString(component, key, String.valueOf(defaultValue));
      return Double.parseDouble(value);
    } catch (Exception e) {
      logger.log(Level.SEVERE, e.getMessage(), e);
      throw new ConfigException(e.getMessage(), e.getCause());
    }
  }

  @Override
  public Boolean getBoolean(String component, String key, boolean defaultValue) throws ConfigException {
    try {
      String value = this.getString(component, key, String.valueOf(defaultValue));
      return Boolean.parseBoolean(value);
    } catch (Exception e) {
      logger.log(Level.SEVERE, e.getMessage(), e);
      throw new ConfigException(e.getMessage(), e.getCause());
    }
  }

Thank you

like image 970
Fabry Avatar asked May 22 '26 10:05

Fabry


2 Answers

Define a generic method that calls getString and uses custom parser (defined as functional interface in this case) to parse the string into a generic type:

public <T> T getValue(String component, String key, T defaultValue, Function<String, T> parser) throws ConfigException {
    try {
        String value = this.getString( component, key, String.valueOf(defaultValue) );
        return parser.apply(value);
      } catch (Exception e) {
        logger.log(Level.SEVERE, e.getMessage(), e);
        throw new ConfigException( e.getMessage(), e.getCause() );
      }
}

public Integer getInteger(String component, String key, int defaultValue) throws ConfigException {
    return getValue(component, key, defaultValue, Integer::parseInt );
}

public Double  getDouble(String component, String key, double defaultValue) throws ConfigException {
    return getValue(component, key, defaultValue, Double::parseDouble );
}

public Boolean getBoolean (String component, String key, boolean defaultValue) throws ConfigException {
    return getValue(component, key, defaultValue, Boolean::parseBoolean);
}
like image 80
tsolakp Avatar answered May 24 '26 22:05

tsolakp


We could write a private generic method
introducing T for T defaultValue and Function<String, T> parser:

private <T> T getT(String component, String key, T defaultValue, Function<String, T> parser) {
    try {
        return parser.apply(this.getString(component, key, String.valueOf(defaultValue)));
    } catch (Exception e) {
        logger.log(Level.SEVERE, e.getMessage(), e);
        throw new ConfigException(e.getMessage(), e.getCause());
    }
}

The methods getInteger, getDouble, getBoolean would call it passing their Function<String, T>:

public Integer getInteger(String component, String key, int defaultValue) throws ConfigException {
    return getT(component, key, defaultValue, Integer::parseInt);
}

public Double getDouble(String component, String key, double defaultValue) throws ConfigException {
    return getT(component, key, defaultValue, Double::parseDouble);
}

public Boolean getBoolean(String component, String key, boolean defaultValue) throws ConfigException {
    return getT(component, key, defaultValue, Boolean::parseBoolean);
}
like image 25
Andrew Tobilko Avatar answered May 24 '26 22:05

Andrew Tobilko