FileDocCategorySizeDatePackage
IOCase.javaAPI DocAndroid 1.5 API8654Wed May 06 22:42:46 BST 2009org.apache.commons.io

IOCase

public final class IOCase extends Object implements Serializable
Enumeration of IO case sensitivity.

Different filing systems have different rules for case-sensitivity. Windows is case-insensitive, Unix is case-sensitive.

This class captures that difference, providing an enumeration to control how filename comparisons should be performed. It also provides methods that use the enumeration to perform comparisons.

Wherever possible, you should use the check methods in this class to compare filenames.

author
Stephen Colebourne
version
$Id: IOCase.java 606345 2007-12-21 23:43:01Z ggregory $
since
Commons IO 1.3

Fields Summary
public static final IOCase
SENSITIVE
The constant for case sensitive regardless of operating system.
public static final IOCase
INSENSITIVE
The constant for case insensitive regardless of operating system.
public static final IOCase
SYSTEM
The constant for case sensitivity determined by the current operating system. Windows is case-insensitive when comparing filenames, Unix is case-sensitive.

If you derialize this constant of Windows, and deserialize on Unix, or vice versa, then the value of the case-sensitivity flag will change.

private static final long
serialVersionUID
Serialization version.
private final String
name
The enumeration name.
private final transient boolean
sensitive
The sensitivity flag.
Constructors Summary
private IOCase(String name, boolean sensitive)
Private constructor.

param
name the name
param
sensitive the sensitivity

        this.name = name;
        this.sensitive = sensitive;
    
Methods Summary
public intcheckCompareTo(java.lang.String str1, java.lang.String str2)
Compares two strings using the case-sensitivity rule.

This method mimics {@link String#compareTo} but takes case-sensitivity into account.

param
str1 the first string to compare, not null
param
str2 the second string to compare, not null
return
true if equal using the case rules
throws
NullPointerException if either string is null

        if (str1 == null || str2 == null) {
            throw new NullPointerException("The strings must not be null");
        }
        return sensitive ? str1.compareTo(str2) : str1.compareToIgnoreCase(str2);
    
public booleancheckEndsWith(java.lang.String str, java.lang.String end)
Checks if one string ends with another using the case-sensitivity rule.

This method mimics {@link String#endsWith} but takes case-sensitivity into account.

param
str the string to check, not null
param
end the end to compare against, not null
return
true if equal using the case rules
throws
NullPointerException if either string is null

        int endLen = end.length();
        return str.regionMatches(!sensitive, str.length() - endLen, end, 0, endLen);
    
public booleancheckEquals(java.lang.String str1, java.lang.String str2)
Compares two strings using the case-sensitivity rule.

This method mimics {@link String#equals} but takes case-sensitivity into account.

param
str1 the first string to compare, not null
param
str2 the second string to compare, not null
return
true if equal using the case rules
throws
NullPointerException if either string is null

        if (str1 == null || str2 == null) {
            throw new NullPointerException("The strings must not be null");
        }
        return sensitive ? str1.equals(str2) : str1.equalsIgnoreCase(str2);
    
public booleancheckRegionMatches(java.lang.String str, int strStartIndex, java.lang.String search)
Checks if one string contains another at a specific index using the case-sensitivity rule.

This method mimics parts of {@link String#regionMatches(boolean, int, String, int, int)} but takes case-sensitivity into account.

param
str the string to check, not null
param
strStartIndex the index to start at in str
param
search the start to search for, not null
return
true if equal using the case rules
throws
NullPointerException if either string is null

        return str.regionMatches(!sensitive, strStartIndex, search, 0, search.length());
    
public booleancheckStartsWith(java.lang.String str, java.lang.String start)
Checks if one string starts with another using the case-sensitivity rule.

This method mimics {@link String#startsWith(String)} but takes case-sensitivity into account.

param
str the string to check, not null
param
start the start to compare against, not null
return
true if equal using the case rules
throws
NullPointerException if either string is null

        return str.regionMatches(!sensitive, 0, start, 0, start.length());
    
java.lang.StringconvertCase(java.lang.String str)
Converts the case of the input String to a standard format. Subsequent operations can then use standard String methods.

param
str the string to convert, null returns null
return
the lower-case version if case-insensitive

        if (str == null) {
            return null;
        }
        return sensitive ? str : str.toLowerCase();
    
public static org.apache.commons.io.IOCaseforName(java.lang.String name)
Factory method to create an IOCase from a name.

param
name the name to find
return
the IOCase object
throws
IllegalArgumentException if the name is invalid


    //-----------------------------------------------------------------------
                                     
         
        if (IOCase.SENSITIVE.name.equals(name)){
            return IOCase.SENSITIVE;
        }
        if (IOCase.INSENSITIVE.name.equals(name)){
            return IOCase.INSENSITIVE;
        }
        if (IOCase.SYSTEM.name.equals(name)){
            return IOCase.SYSTEM;
        }
        throw new IllegalArgumentException("Invalid IOCase name: " + name);
    
public java.lang.StringgetName()
Gets the name of the constant.

return
the name of the constant

        return name;
    
public booleanisCaseSensitive()
Does the object represent case sensitive comparison.

return
true if case sensitive

        return sensitive;
    
private java.lang.ObjectreadResolve()
Replaces the enumeration from the stream with a real one. This ensures that the correct flag is set for SYSTEM.

return
the resolved object

        return forName(name);
    
public java.lang.StringtoString()
Gets a string describing the sensitivity.

return
a string describing the sensitivity

        return name;