| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| IOCase |
|
| 2.5833333333333335;2.583 |
| 1 | /* | |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
| 3 | * contributor license agreements. See the NOTICE file distributed with | |
| 4 | * this work for additional information regarding copyright ownership. | |
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
| 6 | * (the "License"); you may not use this file except in compliance with | |
| 7 | * the License. You may obtain a copy of the License at | |
| 8 | * | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
| 10 | * | |
| 11 | * Unless required by applicable law or agreed to in writing, software | |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 14 | * See the License for the specific language governing permissions and | |
| 15 | * limitations under the License. | |
| 16 | */ | |
| 17 | package org.apache.commons.io; | |
| 18 | ||
| 19 | import java.io.Serializable; | |
| 20 | ||
| 21 | /** | |
| 22 | * Enumeration of IO case sensitivity. | |
| 23 | * <p> | |
| 24 | * Different filing systems have different rules for case-sensitivity. | |
| 25 | * Windows is case-insensitive, Unix is case-sensitive. | |
| 26 | * <p> | |
| 27 | * This class captures that difference, providing an enumeration to | |
| 28 | * control how filename comparisons should be performed. It also provides | |
| 29 | * methods that use the enumeration to perform comparisons. | |
| 30 | * <p> | |
| 31 | * Wherever possible, you should use the <code>check</code> methods in this | |
| 32 | * class to compare filenames. | |
| 33 | * | |
| 34 | * @version $Id: IOCase.java 1471767 2013-04-24 23:24:19Z sebb $ | |
| 35 | * @since 1.3 | |
| 36 | */ | |
| 37 | public final class IOCase implements Serializable { | |
| 38 | ||
| 39 | /** | |
| 40 | * The constant for case sensitive regardless of operating system. | |
| 41 | */ | |
| 42 | 16 | public static final IOCase SENSITIVE = new IOCase("Sensitive", true); |
| 43 | ||
| 44 | /** | |
| 45 | * The constant for case insensitive regardless of operating system. | |
| 46 | */ | |
| 47 | 16 | public static final IOCase INSENSITIVE = new IOCase("Insensitive", false); |
| 48 | ||
| 49 | /** | |
| 50 | * The constant for case sensitivity determined by the current operating system. | |
| 51 | * Windows is case-insensitive when comparing filenames, Unix is case-sensitive. | |
| 52 | * <p> | |
| 53 | * <strong>Note:</strong> This only caters for Windows and Unix. Other operating | |
| 54 | * systems (e.g. OSX and OpenVMS) are treated as case sensitive if they use the | |
| 55 | * Unix file separator and case-insensitive if they use the Windows file separator | |
| 56 | * (see {@link java.io.File#separatorChar}). | |
| 57 | * <p> | |
| 58 | * If you derialize this constant of Windows, and deserialize on Unix, or vice | |
| 59 | * versa, then the value of the case-sensitivity flag will change. | |
| 60 | */ | |
| 61 | 16 | public static final IOCase SYSTEM = new IOCase("System", !FilenameUtils.isSystemWindows()); |
| 62 | ||
| 63 | /** Serialization version. */ | |
| 64 | private static final long serialVersionUID = -6343169151696340687L; | |
| 65 | ||
| 66 | /** The enumeration name. */ | |
| 67 | private final String name; | |
| 68 | ||
| 69 | /** The sensitivity flag. */ | |
| 70 | private final transient boolean sensitive; | |
| 71 | ||
| 72 | //----------------------------------------------------------------------- | |
| 73 | /** | |
| 74 | * Factory method to create an IOCase from a name. | |
| 75 | * | |
| 76 | * @param name the name to find | |
| 77 | * @return the IOCase object | |
| 78 | * @throws IllegalArgumentException if the name is invalid | |
| 79 | */ | |
| 80 | public static IOCase forName(final String name) { | |
| 81 | 8 | if (IOCase.SENSITIVE.name.equals(name)){ |
| 82 | 2 | return IOCase.SENSITIVE; |
| 83 | } | |
| 84 | 6 | if (IOCase.INSENSITIVE.name.equals(name)){ |
| 85 | 2 | return IOCase.INSENSITIVE; |
| 86 | } | |
| 87 | 4 | if (IOCase.SYSTEM.name.equals(name)){ |
| 88 | 2 | return IOCase.SYSTEM; |
| 89 | } | |
| 90 | 2 | throw new IllegalArgumentException("Invalid IOCase name: " + name); |
| 91 | } | |
| 92 | ||
| 93 | //----------------------------------------------------------------------- | |
| 94 | /** | |
| 95 | * Private constructor. | |
| 96 | * | |
| 97 | * @param name the name | |
| 98 | * @param sensitive the sensitivity | |
| 99 | */ | |
| 100 | 48 | private IOCase(final String name, final boolean sensitive) { |
| 101 | 48 | this.name = name; |
| 102 | 48 | this.sensitive = sensitive; |
| 103 | 48 | } |
| 104 | ||
| 105 | /** | |
| 106 | * Replaces the enumeration from the stream with a real one. | |
| 107 | * This ensures that the correct flag is set for SYSTEM. | |
| 108 | * | |
| 109 | * @return the resolved object | |
| 110 | */ | |
| 111 | private Object readResolve() { | |
| 112 | 3 | return forName(name); |
| 113 | } | |
| 114 | ||
| 115 | //----------------------------------------------------------------------- | |
| 116 | /** | |
| 117 | * Gets the name of the constant. | |
| 118 | * | |
| 119 | * @return the name of the constant | |
| 120 | */ | |
| 121 | public String getName() { | |
| 122 | 3 | return name; |
| 123 | } | |
| 124 | ||
| 125 | /** | |
| 126 | * Does the object represent case sensitive comparison. | |
| 127 | * | |
| 128 | * @return true if case sensitive | |
| 129 | */ | |
| 130 | public boolean isCaseSensitive() { | |
| 131 | 4 | return sensitive; |
| 132 | } | |
| 133 | ||
| 134 | //----------------------------------------------------------------------- | |
| 135 | /** | |
| 136 | * Compares two strings using the case-sensitivity rule. | |
| 137 | * <p> | |
| 138 | * This method mimics {@link String#compareTo} but takes case-sensitivity | |
| 139 | * into account. | |
| 140 | * | |
| 141 | * @param str1 the first string to compare, not null | |
| 142 | * @param str2 the second string to compare, not null | |
| 143 | * @return true if equal using the case rules | |
| 144 | * @throws NullPointerException if either string is null | |
| 145 | */ | |
| 146 | public int checkCompareTo(final String str1, final String str2) { | |
| 147 | 292 | if (str1 == null || str2 == null) { |
| 148 | 3 | throw new NullPointerException("The strings must not be null"); |
| 149 | } | |
| 150 | 289 | return sensitive ? str1.compareTo(str2) : str1.compareToIgnoreCase(str2); |
| 151 | } | |
| 152 | ||
| 153 | /** | |
| 154 | * Compares two strings using the case-sensitivity rule. | |
| 155 | * <p> | |
| 156 | * This method mimics {@link String#equals} but takes case-sensitivity | |
| 157 | * into account. | |
| 158 | * | |
| 159 | * @param str1 the first string to compare, not null | |
| 160 | * @param str2 the second string to compare, not null | |
| 161 | * @return true if equal using the case rules | |
| 162 | * @throws NullPointerException if either string is null | |
| 163 | */ | |
| 164 | public boolean checkEquals(final String str1, final String str2) { | |
| 165 | 2636 | if (str1 == null || str2 == null) { |
| 166 | 3 | throw new NullPointerException("The strings must not be null"); |
| 167 | } | |
| 168 | 2633 | return sensitive ? str1.equals(str2) : str1.equalsIgnoreCase(str2); |
| 169 | } | |
| 170 | ||
| 171 | /** | |
| 172 | * Checks if one string starts with another using the case-sensitivity rule. | |
| 173 | * <p> | |
| 174 | * This method mimics {@link String#startsWith(String)} but takes case-sensitivity | |
| 175 | * into account. | |
| 176 | * | |
| 177 | * @param str the string to check, not null | |
| 178 | * @param start the start to compare against, not null | |
| 179 | * @return true if equal using the case rules | |
| 180 | * @throws NullPointerException if either string is null | |
| 181 | */ | |
| 182 | public boolean checkStartsWith(final String str, final String start) { | |
| 183 | 78 | return str.regionMatches(!sensitive, 0, start, 0, start.length()); |
| 184 | } | |
| 185 | ||
| 186 | /** | |
| 187 | * Checks if one string ends with another using the case-sensitivity rule. | |
| 188 | * <p> | |
| 189 | * This method mimics {@link String#endsWith} but takes case-sensitivity | |
| 190 | * into account. | |
| 191 | * | |
| 192 | * @param str the string to check, not null | |
| 193 | * @param end the end to compare against, not null | |
| 194 | * @return true if equal using the case rules | |
| 195 | * @throws NullPointerException if either string is null | |
| 196 | */ | |
| 197 | public boolean checkEndsWith(final String str, final String end) { | |
| 198 | 185 | final int endLen = end.length(); |
| 199 | 183 | return str.regionMatches(!sensitive, str.length() - endLen, end, 0, endLen); |
| 200 | } | |
| 201 | ||
| 202 | /** | |
| 203 | * Checks if one string contains another starting at a specific index using the | |
| 204 | * case-sensitivity rule. | |
| 205 | * <p> | |
| 206 | * This method mimics parts of {@link String#indexOf(String, int)} | |
| 207 | * but takes case-sensitivity into account. | |
| 208 | * | |
| 209 | * @param str the string to check, not null | |
| 210 | * @param strStartIndex the index to start at in str | |
| 211 | * @param search the start to search for, not null | |
| 212 | * @return the first index of the search String, | |
| 213 | * -1 if no match or {@code null} string input | |
| 214 | * @throws NullPointerException if either string is null | |
| 215 | * @since 2.0 | |
| 216 | */ | |
| 217 | public int checkIndexOf(final String str, final int strStartIndex, final String search) { | |
| 218 | 287 | final int endIndex = str.length() - search.length(); |
| 219 | 284 | if (endIndex >= strStartIndex) { |
| 220 | 938 | for (int i = strStartIndex; i <= endIndex; i++) { |
| 221 | 842 | if (checkRegionMatches(str, i, search)) { |
| 222 | 127 | return i; |
| 223 | } | |
| 224 | } | |
| 225 | } | |
| 226 | 157 | return -1; |
| 227 | } | |
| 228 | ||
| 229 | /** | |
| 230 | * Checks if one string contains another at a specific index using the case-sensitivity rule. | |
| 231 | * <p> | |
| 232 | * This method mimics parts of {@link String#regionMatches(boolean, int, String, int, int)} | |
| 233 | * but takes case-sensitivity into account. | |
| 234 | * | |
| 235 | * @param str the string to check, not null | |
| 236 | * @param strStartIndex the index to start at in str | |
| 237 | * @param search the start to search for, not null | |
| 238 | * @return true if equal using the case rules | |
| 239 | * @throws NullPointerException if either string is null | |
| 240 | */ | |
| 241 | public boolean checkRegionMatches(final String str, final int strStartIndex, final String search) { | |
| 242 | 2065 | return str.regionMatches(!sensitive, strStartIndex, search, 0, search.length()); |
| 243 | } | |
| 244 | ||
| 245 | //----------------------------------------------------------------------- | |
| 246 | /** | |
| 247 | * Gets a string describing the sensitivity. | |
| 248 | * | |
| 249 | * @return a string describing the sensitivity | |
| 250 | */ | |
| 251 | @Override | |
| 252 | public String toString() { | |
| 253 | 11 | return name; |
| 254 | } | |
| 255 | ||
| 256 | } |