class BloombergDataUtils {

  public static ExternalIdBundleWithDates addTwoDigitYearCode(final ExternalIdBundleWithDates bundleWithDates) {
    ArgumentChecker.notNull(bundleWithDates, "bundleWithDates");
    final Set<ExternalIdWithDates> identifiers = new HashSet<ExternalIdWithDates>();
    for (final ExternalIdWithDates identifierWithDates : bundleWithDates) {
      final ExternalId identifier = identifierWithDates.toExternalId();
      final String identifierValue = identifier.getValue();
      if (identifierValue.contains(BloombergConstants.MARKET_SECTOR_COMDTY) && identifierValue.indexOf(' ') == identifierValue.lastIndexOf(' ')) {
        //found a future code
        final int splitIndex = identifierValue.lastIndexOf(' ');
        final String secCode = identifierValue.substring(0, splitIndex);

        //get two year digit code
        final int length = secCode.length();
        String yearStr = secCode.substring(length - 2);
        try {
          Integer.parseInt(yearStr);
          identifiers.add(ExternalIdWithDates.of(identifierWithDates.toExternalId(), identifierWithDates.getValidTo().plusDays(1), null));

          //add single digit as well
          final StringBuilder buf = new StringBuilder(secCode.substring(0, secCode.indexOf(yearStr)));
          buf.append(yearStr.charAt(yearStr.length() - 1));
          buf.append(identifierValue.substring(splitIndex));
          final ExternalId singleYearId = ExternalSchemes.bloombergTickerSecurityId(buf.toString());
          identifiers.add(ExternalIdWithDates.of(singleYearId, identifierWithDates.getValidFrom(), identifierWithDates.getValidTo()));
        } catch (final NumberFormatException ex) {
          // try the single digit
          yearStr = secCode.substring(length - 1);
          try {
            Integer.parseInt(yearStr);
            identifiers.add(identifierWithDates);
            //add double digit as well
            final LocalDate validTo = identifierWithDates.getValidTo();
            final String endYear = String.valueOf(validTo.getYear());
            final StringBuilder buf = new StringBuilder(secCode.substring(0, secCode.indexOf(yearStr)));
            buf.append(endYear.substring(endYear.length() - 2));
            buf.append(identifierValue.substring(splitIndex));
            final ExternalId doubleDigitYearId = ExternalSchemes.bloombergTickerSecurityId(buf.toString());
            identifiers.add(ExternalIdWithDates.of(doubleDigitYearId, identifierWithDates.getValidTo().plusDays(1), null));
          } catch (final NumberFormatException ex2) {
            s_logger.warn("cannot make out year code for {}", identifier);
          }
        }
      } else {
        identifiers.add(identifierWithDates);
      }
    }
    return new ExternalIdBundleWithDates(identifiers);
  }

  private  BloombergDataUtils();

}