001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.io;
018:
019: import java.io.Serializable;
020:
021: /**
022: * Enumeration of IO case sensitivity.
023: * <p>
024: * Different filing systems have different rules for case-sensitivity.
025: * Windows is case-insensitive, Unix is case-sensitive.
026: * <p>
027: * This class captures that difference, providing an enumeration to
028: * control how filename comparisons should be performed. It also provides
029: * methods that use the enumeration to perform comparisons.
030: * <p>
031: * Wherever possible, you should use the <code>check</code> methods in this
032: * class to compare filenames.
033: *
034: * @author Stephen Colebourne
035: * @version $Id: IOCase.java 484844 2006-12-08 23:27:18Z ggregory $
036: * @since Commons IO 1.3
037: */
038: public final class IOCase implements Serializable {
039:
040: /**
041: * The constant for case sensitive regardless of operating system.
042: */
043: public static final IOCase SENSITIVE = new IOCase("Sensitive", true);
044: /**
045: * The constant for case insensitive regardless of operating system.
046: */
047: public static final IOCase INSENSITIVE = new IOCase("Insensitive",
048: false);
049: /**
050: * The constant for case sensitivity determined by the current operating system.
051: * Windows is case-insensitive when comparing filenames, Unix is case-sensitive.
052: * <p>
053: * If you derialize this constant of Windows, and deserialize on Unix, or vice
054: * versa, then the value of the case-sensitivity flag will change.
055: */
056: public static final IOCase SYSTEM = new IOCase("System",
057: !FilenameUtils.isSystemWindows());
058:
059: /** Serialization version. */
060: private static final long serialVersionUID = -6343169151696340687L;
061:
062: /** The enumeration name. */
063: private final String name;
064: /** The sensitivity flag. */
065: private final transient boolean sensitive;
066:
067: //-----------------------------------------------------------------------
068: /**
069: * Factory method to create an IOCase from a name.
070: *
071: * @param name the name to find
072: * @return the IOCase object
073: * @throws IllegalArgumentException if the name is invalid
074: */
075: public static IOCase forName(String name) {
076: if (IOCase.SENSITIVE.name.equals(name)) {
077: return IOCase.SENSITIVE;
078: }
079: if (IOCase.INSENSITIVE.name.equals(name)) {
080: return IOCase.INSENSITIVE;
081: }
082: if (IOCase.SYSTEM.name.equals(name)) {
083: return IOCase.SYSTEM;
084: }
085: throw new IllegalArgumentException("Invalid IOCase name: "
086: + name);
087: }
088:
089: //-----------------------------------------------------------------------
090: /**
091: * Private constructor.
092: *
093: * @param name the name
094: * @param sensitive the sensitivity
095: */
096: private IOCase(String name, boolean sensitive) {
097: this .name = name;
098: this .sensitive = sensitive;
099: }
100:
101: /**
102: * Replaces the enumeration from the stream with a real one.
103: * This ensures that the correct flag is set for SYSTEM.
104: *
105: * @return the resolved object
106: */
107: private Object readResolve() {
108: return forName(name);
109: }
110:
111: //-----------------------------------------------------------------------
112: /**
113: * Gets the name of the constant.
114: *
115: * @return the name of the constant
116: */
117: public String getName() {
118: return name;
119: }
120:
121: /**
122: * Does the object represent case sensitive comparison.
123: *
124: * @return true if case sensitive
125: */
126: public boolean isCaseSensitive() {
127: return sensitive;
128: }
129:
130: //-----------------------------------------------------------------------
131: /**
132: * Compares two strings using the case-sensitivity rule.
133: * <p>
134: * This method mimics {@link String#equals} but takes case-sensitivity
135: * into account.
136: *
137: * @param str1 the first string to compare, not null
138: * @param str2 the second string to compare, not null
139: * @return true if equal using the case rules
140: * @throws NullPointerException if either string is null
141: */
142: public boolean checkEquals(String str1, String str2) {
143: if (str1 == null || str2 == null) {
144: throw new NullPointerException(
145: "The strings must not be null");
146: }
147: return sensitive ? str1.equals(str2) : str1
148: .equalsIgnoreCase(str2);
149: }
150:
151: /**
152: * Checks if one string starts with another using the case-sensitivity rule.
153: * <p>
154: * This method mimics {@link String#startsWith(String)} but takes case-sensitivity
155: * into account.
156: *
157: * @param str the string to check, not null
158: * @param start the start to compare against, not null
159: * @return true if equal using the case rules
160: * @throws NullPointerException if either string is null
161: */
162: public boolean checkStartsWith(String str, String start) {
163: return str.regionMatches(!sensitive, 0, start, 0, start
164: .length());
165: }
166:
167: /**
168: * Checks if one string ends with another using the case-sensitivity rule.
169: * <p>
170: * This method mimics {@link String#endsWith} but takes case-sensitivity
171: * into account.
172: *
173: * @param str the string to check, not null
174: * @param end the end to compare against, not null
175: * @return true if equal using the case rules
176: * @throws NullPointerException if either string is null
177: */
178: public boolean checkEndsWith(String str, String end) {
179: int endLen = end.length();
180: return str.regionMatches(!sensitive, str.length() - endLen,
181: end, 0, endLen);
182: }
183:
184: /**
185: * Checks if one string contains another at a specific index using the case-sensitivity rule.
186: * <p>
187: * This method mimics parts of {@link String#regionMatches(boolean, int, String, int, int)}
188: * but takes case-sensitivity into account.
189: *
190: * @param str the string to check, not null
191: * @param strStartIndex the index to start at in str
192: * @param search the start to search for, not null
193: * @return true if equal using the case rules
194: * @throws NullPointerException if either string is null
195: */
196: public boolean checkRegionMatches(String str, int strStartIndex,
197: String search) {
198: return str.regionMatches(!sensitive, strStartIndex, search, 0,
199: search.length());
200: }
201:
202: /**
203: * Converts the case of the input String to a standard format.
204: * Subsequent operations can then use standard String methods.
205: *
206: * @param str the string to convert, null returns null
207: * @return the lower-case version if case-insensitive
208: */
209: String convertCase(String str) {
210: if (str == null) {
211: return null;
212: }
213: return sensitive ? str : str.toLowerCase();
214: }
215:
216: //-----------------------------------------------------------------------
217: /**
218: * Gets a string describing the sensitivity.
219: *
220: * @return a string describing the sensitivity
221: */
222: public String toString() {
223: return name;
224: }
225:
226: }
|