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 private LowerCaseRule(Locale locale) { 047 this.locale = locale; 048 } 049 050 /** 051 * Creates a LowerCaseRule using the specified locale. 052 * 053 * @param locale the locale to use for case conversion 054 * @return a LowerCaseRule instance 055 * @throws IllegalArgumentException if locale is null 056 */ 057 public static LowerCaseRule create(Locale locale) { 058 if (locale == null) { 059 throw new IllegalArgumentException("Locale cannot be null"); 060 } 061 return new LowerCaseRule(locale); 062 } 063 064 @Override 065 public String apply(String input) { 066 return input != null ? input.toLowerCase(locale) : null; 067 } 068 069 /** 070 * Gets the locale used for case conversion. 071 * 072 * @return the locale 073 */ 074 public Locale getLocale() { 075 return locale; 076 } 077 078 @Override 079 public String toString() { 080 return String.format("LowerCaseRule{locale=%s}", locale); 081 } 082 083 @Override 084 public boolean equals(Object obj) { 085 if (this == obj) return true; 086 if (obj == null || getClass() != obj.getClass()) return false; 087 LowerCaseRule that = (LowerCaseRule) obj; 088 return locale.equals(that.locale); 089 } 090 091 @Override 092 public int hashCode() { 093 return locale.hashCode(); 094 } 095}