class GoogleCloudStorageStrings {

  public static String matchListPrefix(
      String objectNamePrefix, String objectName, ListObjectOptions listOptions) {
    checkArgument(
        !isNullOrEmpty(objectName),
        "matchListPrefix(%s, %s, %s): objectName must not be null or empty.",
        objectNamePrefix,
        objectName,
        listOptions);

    // The suffix that we'll use to check for the delimiter is just the whole name if no prefix
    // was supplied.
    String suffix = objectName;
    int suffixIndex = 0;
    if (objectNamePrefix != null) {
      // The underlying GCS API does return objectName when it equals the prefix, but our
      // GoogleCloudStorage wrapper filters this case if the objectName also ends with
      // PATH_DELIMITER.
      if (!objectName.startsWith(objectNamePrefix)
          || (objectName.equals(objectNamePrefix)
              && objectNamePrefix.endsWith(PATH_DELIMITER)
              && !listOptions.isIncludePrefix())) {
        return null;
      }
      suffixIndex = objectNamePrefix.length();
      suffix = objectName.substring(suffixIndex);
    }

    String delimiter = listOptions.getDelimiter();
    return !isNullOrEmpty(delimiter) && suffix.contains(delimiter)
        // Return the full prefix and suffix up through first occurrence of delimiter after
        // the prefix, inclusive of the delimiter.
        ? objectName.substring(0, objectName.indexOf(delimiter, suffixIndex) + delimiter.length())
        : objectName;
  }

}