001package com.streamconverter.command.rule.impl.string;
002
003import com.streamconverter.command.rule.IRule;
004import java.util.Locale;
005
006/**
007 * Converts strings to lowercase.
008 *
009 * <p>This rule converts all uppercase letters in the input string to lowercase using the specified
010 * locale or the default locale if none is specified.
011 *
012 * <p>Examples:
013 *
014 * <ul>
015 *   <li>{@code "HELLO"} → {@code "hello"}
016 *   <li>{@code "Hello World"} → {@code "hello world"}
017 *   <li>{@code "XMLHttpRequest"} → {@code "xmlhttprequest"}
018 * </ul>
019 *
020 * <p>Usage:
021 *
022 * <pre>{@code
023 * IRule rule = new LowerCaseRule();
024 * IRule localeRule = new LowerCaseRule(Locale.ENGLISH);
025 * IStreamCommand command = JsonNavigateCommand.create("$.name", rule);
026 * }</pre>
027 *
028 * @since 1.0
029 */
030public class LowerCaseRule implements IRule {
031
032  /** The locale to use for case conversion */
033  private final Locale locale;
034
035  /** Creates a LowerCaseRule using the default locale. */
036  public LowerCaseRule() {
037    this.locale = Locale.getDefault();
038  }
039
040  /**
041   * Creates a LowerCaseRule using the specified locale.
042   *
043   * @param locale the locale to use for case conversion
044   * @throws IllegalArgumentException if locale is null
045   */
046  public LowerCaseRule(Locale locale) {
047    if (locale == null) {
048      throw new IllegalArgumentException("Locale cannot be null");
049    }
050    this.locale = locale;
051  }
052
053  @Override
054  public String apply(String input) {
055    return input != null ? input.toLowerCase(locale) : null;
056  }
057
058  /**
059   * Gets the locale used for case conversion.
060   *
061   * @return the locale
062   */
063  public Locale getLocale() {
064    return locale;
065  }
066
067  @Override
068  public String toString() {
069    return String.format("LowerCaseRule{locale=%s}", locale);
070  }
071
072  @Override
073  public boolean equals(Object obj) {
074    if (this == obj) return true;
075    if (obj == null || getClass() != obj.getClass()) return false;
076    LowerCaseRule that = (LowerCaseRule) obj;
077    return locale.equals(that.locale);
078  }
079
080  @Override
081  public int hashCode() {
082    return locale.hashCode();
083  }
084}