|
| 1 | +/* |
| 2 | + * Copyright 2006 The Apache Software Foundation. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.apache.commons.io; |
| 17 | + |
| 18 | +import java.io.Serializable; |
| 19 | + |
| 20 | +/** |
| 21 | + * Enumeration of IO case sensitivity. |
| 22 | + * <p> |
| 23 | + * Different filing systems have different rules for case-sensitivity. |
| 24 | + * Windows is case-insensitive, Unix is case-sensitive. |
| 25 | + * <p> |
| 26 | + * This class captures that difference, providing an enumeration to |
| 27 | + * control how filename comparisons should be performed. It also provides |
| 28 | + * methods that use the enumeration to perform comparisons. |
| 29 | + * <p> |
| 30 | + * Wherever possible, you should use the <code>check</code> methods in this |
| 31 | + * class to compare filenames. |
| 32 | + * |
| 33 | + * @author Stephen Colebourne |
| 34 | + * @version $Id$ |
| 35 | + * @since Commons IO 1.3 |
| 36 | + */ |
| 37 | +public final class IOCase implements Serializable { |
| 38 | + |
| 39 | + /** |
| 40 | + * The constant for case sensitive regardless of operating system. |
| 41 | + */ |
| 42 | + public static final IOCase SENSITIVE = new IOCase("Sensitive", true); |
| 43 | + /** |
| 44 | + * The constant for case insensitive regardless of operating system. |
| 45 | + */ |
| 46 | + public static final IOCase INSENSITIVE = new IOCase("Insensitive", false); |
| 47 | + /** |
| 48 | + * The constant for case sensitivity determined by the current operating system. |
| 49 | + * Windows is case-insensitive when comparing filenames, Unix is case-sensitive. |
| 50 | + * <p> |
| 51 | + * If you derialize this constant of Windows, and deserialize on Unix, or vice |
| 52 | + * versa, then the value of the case-sensitivity flag will change. |
| 53 | + */ |
| 54 | + public static final IOCase SYSTEM = new IOCase("System", !FilenameUtils.isSystemWindows()); |
| 55 | + |
| 56 | + /** Serialization version. */ |
| 57 | + private static final long serialVersionUID = -6343169151696340687L; |
| 58 | + |
| 59 | + /** The enumeration name. */ |
| 60 | + private final String name; |
| 61 | + /** The sensitivity flag. */ |
| 62 | + private final transient boolean sensitive; |
| 63 | + |
| 64 | + //----------------------------------------------------------------------- |
| 65 | + /** |
| 66 | + * Factory method to create an IOCase from a name. |
| 67 | + * |
| 68 | + * @param name the name to find |
| 69 | + * @return the IOCase object |
| 70 | + * @throws IllegalArgumentException if the name is invalid |
| 71 | + */ |
| 72 | + public static IOCase forName(String name) { |
| 73 | + if (IOCase.SENSITIVE.name.equals(name)){ |
| 74 | + return IOCase.SENSITIVE; |
| 75 | + } |
| 76 | + if (IOCase.INSENSITIVE.name.equals(name)){ |
| 77 | + return IOCase.INSENSITIVE; |
| 78 | + } |
| 79 | + if (IOCase.SYSTEM.name.equals(name)){ |
| 80 | + return IOCase.SYSTEM; |
| 81 | + } |
| 82 | + throw new IllegalArgumentException("Invalid IOCase name: " + name); |
| 83 | + } |
| 84 | + |
| 85 | + //----------------------------------------------------------------------- |
| 86 | + /** |
| 87 | + * Private constructor. |
| 88 | + * |
| 89 | + * @param name the name |
| 90 | + * @param sensitive the sensitivity |
| 91 | + */ |
| 92 | + private IOCase(String name, boolean sensitive) { |
| 93 | + this.name = name; |
| 94 | + this.sensitive = sensitive; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Replaces the enumeration from the stream with a real one. |
| 99 | + * This ensures that the correct flag is set for SYSTEM. |
| 100 | + * |
| 101 | + * @return the resolved object |
| 102 | + */ |
| 103 | + private Object readResolve() { |
| 104 | + return forName(name); |
| 105 | + } |
| 106 | + |
| 107 | + //----------------------------------------------------------------------- |
| 108 | + /** |
| 109 | + * Gets the name of the constant. |
| 110 | + * |
| 111 | + * @return the name of the constant |
| 112 | + */ |
| 113 | + public String getName() { |
| 114 | + return name; |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Does the object represent case sensitive comparison. |
| 119 | + * |
| 120 | + * @return true if case sensitive |
| 121 | + */ |
| 122 | + public boolean isCaseSensitive() { |
| 123 | + return sensitive; |
| 124 | + } |
| 125 | + |
| 126 | + //----------------------------------------------------------------------- |
| 127 | + /** |
| 128 | + * Compares two strings using the case-sensitivity rule. |
| 129 | + * <p> |
| 130 | + * This method mimics {@link String#equals} but takes case-sensitivity |
| 131 | + * into account. |
| 132 | + * |
| 133 | + * @param str1 the first string to compare, not null |
| 134 | + * @param str2 the second string to compare, not null |
| 135 | + * @return true if equal using the case rules |
| 136 | + * @throws NullPointerException if either string is null |
| 137 | + */ |
| 138 | + public boolean checkEquals(String str1, String str2) { |
| 139 | + if (str1 == null || str2 == null) { |
| 140 | + throw new NullPointerException("The strings must not be null"); |
| 141 | + } |
| 142 | + return sensitive ? str1.equals(str2) : str1.equalsIgnoreCase(str2); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Checks if one string starts with another using the case-sensitivity rule. |
| 147 | + * <p> |
| 148 | + * This method mimics {@link String#startsWith} but takes case-sensitivity |
| 149 | + * into account. |
| 150 | + * |
| 151 | + * @param str the string to check, not null |
| 152 | + * @param start the start to compare against, not null |
| 153 | + * @return true if equal using the case rules |
| 154 | + * @throws NullPointerException if either string is null |
| 155 | + */ |
| 156 | + public boolean checkStartsWith(String str, String start) { |
| 157 | + return str.regionMatches(!sensitive, 0, start, 0, start.length()); |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Checks if one string ends with another using the case-sensitivity rule. |
| 162 | + * <p> |
| 163 | + * This method mimics {@link String#endsWith} but takes case-sensitivity |
| 164 | + * into account. |
| 165 | + * |
| 166 | + * @param str the string to check, not null |
| 167 | + * @param end the end to compare against, not null |
| 168 | + * @return true if equal using the case rules |
| 169 | + * @throws NullPointerException if either string is null |
| 170 | + */ |
| 171 | + public boolean checkEndsWith(String str, String end) { |
| 172 | + int endLen = end.length(); |
| 173 | + return str.regionMatches(!sensitive, str.length() - endLen, end, 0, endLen); |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Checks if one string contains another at a specific index using the case-sensitivity rule. |
| 178 | + * <p> |
| 179 | + * This method mimics parts of {@link String#regionMatches} but takes case-sensitivity |
| 180 | + * into account. |
| 181 | + * |
| 182 | + * @param str the string to check, not null |
| 183 | + * @param strStartIndex the index to start at in str |
| 184 | + * @param search the start to search for, not null |
| 185 | + * @return true if equal using the case rules |
| 186 | + * @throws NullPointerException if either string is null |
| 187 | + */ |
| 188 | + public boolean checkRegionMatches(String str, int strStartIndex, String search) { |
| 189 | + return str.regionMatches(!sensitive, strStartIndex, search, 0, search.length()); |
| 190 | + } |
| 191 | + |
| 192 | + //----------------------------------------------------------------------- |
| 193 | + /** |
| 194 | + * Gets a string describing the sensitivity. |
| 195 | + * |
| 196 | + * @return a string describing the sensitivity |
| 197 | + */ |
| 198 | + public String toString() { |
| 199 | + return name; |
| 200 | + } |
| 201 | + |
| 202 | +} |
0 commit comments