class PropertyHelper {

  static String insertTagInToMetricName(String tag, String metricName, String prefix) {
    String sepExpr = "\\.";
    String seperator = ".";
    if (metricName.indexOf(EXTERNAL_PATH_SEP) != -1) {
      sepExpr = Character.toString(EXTERNAL_PATH_SEP);
      seperator = sepExpr;
    }
    String prefixSep = prefix.contains(".") ? "\\." : "" + EXTERNAL_PATH_SEP;

    // Remove separator if any
    if (prefix.substring(prefix.length() - 1).equals(prefixSep)) {
      prefix = prefix.substring(0, prefix.length() - 1);
    }
    int pos = prefix.split(prefixSep).length - 1;
    String[] parts = metricName.split(sepExpr);
    StringBuilder sb = new StringBuilder();

    for (int i = 0; i < parts.length; i++) {
      sb.append(parts[i]);
      if (i < parts.length - 1) {
        sb.append(seperator);
      }
      if (i == pos) { // append the tag
        sb.append(tag);
        sb.append(seperator);
      }
    }
    return sb.toString();
  }

}