Coverage Report - org.apache.commons.io.IOCase
 
Classes in this File Line Coverage Branch Coverage Complexity
IOCase
96%
31/32
100%
28/28
2.333
 
 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 1483915 2013-05-17 17:02:35Z sebb $
 35  
  * @since 1.3
 36  
  */
 37  44
 public enum IOCase implements Serializable {
 38  
 
 39  
     /**
 40  
      * The constant for case sensitive regardless of operating system.
 41  
      */
 42  32
     SENSITIVE ("Sensitive", true),
 43  
 
 44  
     /**
 45  
      * The constant for case insensitive regardless of operating system.
 46  
      */
 47  32
     INSENSITIVE ("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  32
     SYSTEM ("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  28
         for (IOCase ioCase : IOCase.values())
 82  
         {
 83  24
             if (ioCase.getName().equals(name))
 84  
             {
 85  6
                 return ioCase;
 86  
             }
 87  
         }
 88  4
         throw new IllegalArgumentException("Invalid IOCase name: " + name);
 89  
     }
 90  
 
 91  
     //-----------------------------------------------------------------------
 92  
     /**
 93  
      * Private constructor.
 94  
      *
 95  
      * @param name  the name
 96  
      * @param sensitive  the sensitivity
 97  
      */
 98  96
     private IOCase(final String name, final boolean sensitive) {
 99  96
         this.name = name;
 100  96
         this.sensitive = sensitive;
 101  96
     }
 102  
 
 103  
     /**
 104  
      * Replaces the enumeration from the stream with a real one.
 105  
      * This ensures that the correct flag is set for SYSTEM.
 106  
      *
 107  
      * @return the resolved object
 108  
      */
 109  
     private Object readResolve() {
 110  0
         return forName(name);
 111  
     }
 112  
 
 113  
     //-----------------------------------------------------------------------
 114  
     /**
 115  
      * Gets the name of the constant.
 116  
      *
 117  
      * @return the name of the constant
 118  
      */
 119  
     public String getName() {
 120  30
         return name;
 121  
     }
 122  
 
 123  
     /**
 124  
      * Does the object represent case sensitive comparison.
 125  
      *
 126  
      * @return true if case sensitive
 127  
      */
 128  
     public boolean isCaseSensitive() {
 129  8
         return sensitive;
 130  
     }
 131  
 
 132  
     //-----------------------------------------------------------------------
 133  
     /**
 134  
      * Compares two strings using the case-sensitivity rule.
 135  
      * <p>
 136  
      * This method mimics {@link String#compareTo} but takes case-sensitivity
 137  
      * into account.
 138  
      *
 139  
      * @param str1  the first string to compare, not null
 140  
      * @param str2  the second string to compare, not null
 141  
      * @return true if equal using the case rules
 142  
      * @throws NullPointerException if either string is null
 143  
      */
 144  
     public int checkCompareTo(final String str1, final String str2) {
 145  584
         if (str1 == null || str2 == null) {
 146  6
             throw new NullPointerException("The strings must not be null");
 147  
         }
 148  578
         return sensitive ? str1.compareTo(str2) : str1.compareToIgnoreCase(str2);
 149  
     }
 150  
 
 151  
     /**
 152  
      * Compares two strings using the case-sensitivity rule.
 153  
      * <p>
 154  
      * This method mimics {@link String#equals} but takes case-sensitivity
 155  
      * into account.
 156  
      *
 157  
      * @param str1  the first string to compare, not null
 158  
      * @param str2  the second string to compare, not null
 159  
      * @return true if equal using the case rules
 160  
      * @throws NullPointerException if either string is null
 161  
      */
 162  
     public boolean checkEquals(final String str1, final String str2) {
 163  5412
         if (str1 == null || str2 == null) {
 164  6
             throw new NullPointerException("The strings must not be null");
 165  
         }
 166  5406
         return sensitive ? str1.equals(str2) : str1.equalsIgnoreCase(str2);
 167  
     }
 168  
 
 169  
     /**
 170  
      * Checks if one string starts with another using the case-sensitivity rule.
 171  
      * <p>
 172  
      * This method mimics {@link String#startsWith(String)} but takes case-sensitivity
 173  
      * into account.
 174  
      *
 175  
      * @param str  the string to check, not null
 176  
      * @param start  the start to compare against, not null
 177  
      * @return true if equal using the case rules
 178  
      * @throws NullPointerException if either string is null
 179  
      */
 180  
     public boolean checkStartsWith(final String str, final String start) {
 181  156
         return str.regionMatches(!sensitive, 0, start, 0, start.length());
 182  
     }
 183  
 
 184  
     /**
 185  
      * Checks if one string ends with another using the case-sensitivity rule.
 186  
      * <p>
 187  
      * This method mimics {@link String#endsWith} but takes case-sensitivity
 188  
      * into account.
 189  
      *
 190  
      * @param str  the string to check, not null
 191  
      * @param end  the end to compare against, not null
 192  
      * @return true if equal using the case rules
 193  
      * @throws NullPointerException if either string is null
 194  
      */
 195  
     public boolean checkEndsWith(final String str, final String end) {
 196  370
         final int endLen = end.length();
 197  366
         return str.regionMatches(!sensitive, str.length() - endLen, end, 0, endLen);
 198  
     }
 199  
 
 200  
     /**
 201  
      * Checks if one string contains another starting at a specific index using the
 202  
      * case-sensitivity rule.
 203  
      * <p>
 204  
      * This method mimics parts of {@link String#indexOf(String, int)}
 205  
      * but takes case-sensitivity into account.
 206  
      *
 207  
      * @param str  the string to check, not null
 208  
      * @param strStartIndex  the index to start at in str
 209  
      * @param search  the start to search for, not null
 210  
      * @return the first index of the search String,
 211  
      *  -1 if no match or {@code null} string input
 212  
      * @throws NullPointerException if either string is null
 213  
      * @since 2.0
 214  
      */
 215  
     public int checkIndexOf(final String str, final int strStartIndex, final String search) {
 216  574
         final int endIndex = str.length() - search.length();
 217  568
         if (endIndex >= strStartIndex) {
 218  1876
             for (int i = strStartIndex; i <= endIndex; i++) {
 219  1684
                 if (checkRegionMatches(str, i, search)) {
 220  254
                     return i;
 221  
                 }
 222  
             }
 223  
         }
 224  314
         return -1;
 225  
     }
 226  
 
 227  
     /**
 228  
      * Checks if one string contains another at a specific index using the case-sensitivity rule.
 229  
      * <p>
 230  
      * This method mimics parts of {@link String#regionMatches(boolean, int, String, int, int)}
 231  
      * but takes case-sensitivity into account.
 232  
      *
 233  
      * @param str  the string to check, not null
 234  
      * @param strStartIndex  the index to start at in str
 235  
      * @param search  the start to search for, not null
 236  
      * @return true if equal using the case rules
 237  
      * @throws NullPointerException if either string is null
 238  
      */
 239  
     public boolean checkRegionMatches(final String str, final int strStartIndex, final String search) {
 240  4130
         return str.regionMatches(!sensitive, strStartIndex, search, 0, search.length());
 241  
     }
 242  
 
 243  
     //-----------------------------------------------------------------------
 244  
     /**
 245  
      * Gets a string describing the sensitivity.
 246  
      *
 247  
      * @return a string describing the sensitivity
 248  
      */
 249  
     @Override
 250  
     public String toString() {
 251  22
         return name;
 252  
     }
 253  
 
 254  
 }