Coverage Report - jaggregate.Strings
 
Classes in this File Line Coverage Branch Coverage Complexity
Strings
100%
29/29
N/A
0
Strings$1
100%
2/2
N/A
0
Strings$CharAt
100%
4/4
100%
1/1
0
Strings$EndsWith
100%
4/4
N/A
0
Strings$EqualsIgnoreCase
100%
3/3
N/A
0
Strings$HasSameContents
100%
3/3
N/A
0
Strings$HasSubstring
100%
4/4
N/A
0
Strings$IndexOfCharacter
100%
2/2
N/A
0
Strings$IndexOfString
100%
2/2
N/A
0
Strings$LastIndexOfCharacter
100%
2/2
N/A
0
Strings$LastIndexOfString
100%
2/2
N/A
0
Strings$MatchesPattern
100%
4/4
N/A
0
Strings$StartsWith
100%
4/4
N/A
0
Strings$Substring
100%
2/2
N/A
0
 
 1  
 /*
 2  
  Copyright 2004-2008 Paul R. Holser, Jr.  All rights reserved.
 3  
  Licensed under the Academic Free License version 3.0
 4  
  */
 5  
 
 6  
 package jaggregate;
 7  
 
 8  
 import java.util.regex.Pattern;
 9  
 
 10  
 import static jaggregate.internal.ArgumentChecks.*;
 11  
 
 12  
 /**
 13  
  * Utility class that offers common operations on strings.
 14  
  *
 15  
  * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a>
 16  
  * @version $Id: Strings.java,v 1.7 2008/10/03 19:01:23 pholser Exp $
 17  
  */
 18  
 public class Strings {
 19  
     /**
 20  
      * A function which gives the length of its argument.
 21  
      *
 22  
      * @see String#length()
 23  
      */
 24  1
     public static final UnaryFunctor<String, Integer> LENGTH =
 25  
         new UnaryFunctor<String, Integer>() {
 26  4
             public Integer evaluate( String argument ) {
 27  3
                 return argument.length();
 28  
             }
 29  
         };
 30  
 
 31  
     /**
 32  
      * Discourages instantiation.
 33  
      *
 34  
      * @throws UnsupportedOperationException always
 35  
      */
 36  1
     protected Strings() {
 37  1
         throw new UnsupportedOperationException();
 38  
     }
 39  
 
 40  
     /**
 41  
      * Answers a predicate which will answer {@code true} if the given substring occurs
 42  
      * anywhere within its argument.
 43  
      *
 44  
      * @see String#contains(CharSequence)
 45  
      * @param substring the comparand to use with the returned predicate
 46  
      * @return a predicate that can be used to compare arguments with {@code substring}
 47  
      * @throws NullPointerException if {@code substring} is {@code null}
 48  
      */
 49  
     public static UnaryPredicate<String> hasSubstring( CharSequence substring ) {
 50  15
         ensureNotNull( substring, "substring comparand" );
 51  
 
 52  14
         return new HasSubstring().bindSecond( substring );
 53  
     }
 54  
 
 55  
     /**
 56  
      * Answers a predicate which will answer {@code true} if the given string has the
 57  
      * predicate's argument as a substring.
 58  
      *
 59  
      * @see String#contains(CharSequence)
 60  
      * @param target the comparand to use with the returned predicate
 61  
      * @return a predicate that can be used to compare arguments with {@code target}
 62  
      * @throws NullPointerException if {@code target} is {@code null}
 63  
      */
 64  
     public static UnaryPredicate<CharSequence> isSubstringOf( String target ) {
 65  11
         ensureNotNull( target, "string comparand" );
 66  
 
 67  10
         return new HasSubstring().bindFirst( target );
 68  
     }
 69  
 
 70  
     /**
 71  
      * Answers a predicate which will answer {@code true} if its argument has the same
 72  
      * string contents as the given comparand.
 73  
      *
 74  
      * @see String#contentEquals(CharSequence)
 75  
      * @param comparand the comparand to use with the returned predicate
 76  
      * @return a predicate that can be used to compare arguments with {@code comparand}
 77  
      * @throws NullPointerException if {@code comparand} is {@code null}
 78  
      */
 79  
     public static UnaryPredicate<String> hasSameContentsAs( CharSequence comparand ) {
 80  19
         ensureNotNull( comparand, "CharSequence comparand" );
 81  
 
 82  18
         return new HasSameContents().bindSecond( comparand );
 83  
     }
 84  
 
 85  
     /**
 86  
      * Answers a predicate which will answer {@code true} if its argument ends with the
 87  
      * given suffix.
 88  
      *
 89  
      * @see String#endsWith(String)
 90  
      * @param suffix the comparand to use with the returned predicate
 91  
      * @return a predicate that can be used to compare arguments with {@code suffix}
 92  
      * @throws NullPointerException if {@code suffix} is {@code null}
 93  
      */
 94  
     public static UnaryPredicate<String> endsWith( String suffix ) {
 95  7
         ensureNotNull( suffix, "suffix" );
 96  
 
 97  6
         return new EndsWith().bindSecond( suffix );
 98  
     }
 99  
 
 100  
     /**
 101  
      * Answers a predicate which will answer {@code true} if its argument is a suffix of
 102  
      * the given string.
 103  
      *
 104  
      * @see String#endsWith(String)
 105  
      * @param target the comparand to use with the returned predicate
 106  
      * @return a predicate that can be used to compare arguments with {@code target}
 107  
      * @throws NullPointerException if {@code target} is {@code null}
 108  
      */
 109  
     public static UnaryPredicate<String> isSuffixOf( String target ) {
 110  7
         ensureNotNull( target, "suffix target" );
 111  
 
 112  6
         return new EndsWith().bindFirst( target );
 113  
     }
 114  
 
 115  
     /**
 116  
      * Answers a predicate which will answer {@code true} if its argument starts with
 117  
      * the given prefix.
 118  
      *
 119  
      * @param prefix the comparand to use with the returned predicate
 120  
      * @return a predicate that can be used to compare arguments with {@code prefix}
 121  
      * @throws NullPointerException if {@code prefix} is {@code null}
 122  
      */
 123  
     public static UnaryPredicate<String> startsWith( String prefix ) {
 124  174
         ensureNotNull( prefix, "prefix" );
 125  
 
 126  173
         return new StartsWith().bindSecond( prefix );
 127  
     }
 128  
 
 129  
     /**
 130  
      * Answers a predicate which will answer {@code true} if its argument is a prefix
 131  
      * of the given target.
 132  
      *
 133  
      * @param target the comparand to use with the returned predicate
 134  
      * @return a predicate that can be used to compare arguments with {@code target}
 135  
      * @throws NullPointerException if {@code target} is {@code null}
 136  
      */
 137  
     public static UnaryPredicate<String> isPrefixOf( String target ) {
 138  8
         ensureNotNull( target, "prefix target" );
 139  
 
 140  7
         return new StartsWith().bindFirst( target );
 141  
     }
 142  
 
 143  
     /**
 144  
      * Answers a predicate which will answer {@code true} if its argument is equivalent
 145  
      * to the given comparand, disregarding case.
 146  
      *
 147  
      * @see String#equalsIgnoreCase(String)
 148  
      * @param comparand the comparand to use with the returned predicate
 149  
      * @return a predicate that can be used to compare arguments with {@code comparand}
 150  
      * @throws NullPointerException if {@code comparand} is {@code null}
 151  
      */
 152  
     public static UnaryPredicate<String> equalsIgnoreCase( String comparand ) {
 153  7
         ensureNotNull( comparand, "comparand" );
 154  
 
 155  6
         return new EqualsIgnoreCase().bindSecond( comparand );
 156  
     }
 157  
 
 158  
     /**
 159  
      * Answers a predicate which will answer {@code true} if any part of its argument
 160  
      * matches the given regular expression pattern.
 161  
      *
 162  
      * @see java.util.regex.Matcher#find()
 163  
      * @param pattern the comparand to use with the returned predicate
 164  
      * @return a predicate that can be used to compare arguments with {@code pattern}
 165  
      * @throws NullPointerException if {@code pattern} is {@code null}
 166  
      */
 167  
     public static UnaryPredicate<CharSequence> matchesPattern( String pattern ) {
 168  7
         ensureNotNull( pattern, "regex pattern" );
 169  
 
 170  6
         return new MatchesPattern().bindSecond( pattern );
 171  
     }
 172  
 
 173  
     /**
 174  
      * Answers a predicate which will answer {@code true} if its regular expression
 175  
      * pattern argument matches the given target.
 176  
      *
 177  
      * @see java.util.regex.Matcher#find()
 178  
      * @param target the comparand to use with the returned predicate
 179  
      * @return a predicate that can be used to compare arguments with {@code target}
 180  
      * @throws NullPointerException if {@code target} is {@code null}
 181  
      */
 182  
     public static UnaryPredicate<String> patternMatches( CharSequence target ) {
 183  7
         ensureNotNull( target, "regex pattern target" );
 184  
 
 185  6
         return new MatchesPattern().bindFirst( target );
 186  
     }
 187  
 
 188  
     /**
 189  
      * Answers a function which will give the character value of a string target at the
 190  
      * given index.
 191  
      *
 192  
      * @see String#charAt(int)
 193  
      * @param index index into a target string
 194  
      * @return a function that will interrogate strings for the character at
 195  
      * {@code index}
 196  
      */
 197  
     public static UnaryFunction<String, Character> charAt( int index ) {
 198  6
         return new CharAt().bindSecond( index );
 199  
     }
 200  
 
 201  
     /**
 202  
      * Answers a function which will give the first index of a given character value
 203  
      * in a given string.
 204  
      *
 205  
      * @see String#indexOf(int)
 206  
      * @param ch desired character in the target string
 207  
      * @return a function that will interrogate strings for the first index of
 208  
      * {@code ch}
 209  
      */
 210  
     public static UnaryFunction<String, Integer> indexOf( int ch ) {
 211  5
         return new IndexOfCharacter().bindSecond( ch );
 212  
     }
 213  
 
 214  
     /**
 215  
      * Answers a function which will give the first index of a given substring
 216  
      * in a given string.
 217  
      *
 218  
      * @see String#indexOf(String)
 219  
      * @param substring desired substring in the target string
 220  
      * @return a function that will interrogate strings for the first index of
 221  
      * {@code substring}
 222  
      */
 223  
     public static UnaryFunction<String, Integer> indexOf( String substring ) {
 224  7
         return new IndexOfString().bindSecond( substring );
 225  
     }
 226  
 
 227  
     /**
 228  
      * Answers a function which will give the last index of a given character value
 229  
      * in a given string.
 230  
      *
 231  
      * @see String#lastIndexOf(int)
 232  
      * @param ch desired character in the target string
 233  
      * @return a function that will interrogate strings for the last index of {@code ch}
 234  
      */
 235  
     public static UnaryFunction<String, Integer> lastIndexOf( int ch ) {
 236  5
         return new LastIndexOfCharacter().bindSecond( ch );
 237  
     }
 238  
 
 239  
     /**
 240  
      * Answers a function which will give the last index of a given substring
 241  
      * in a given string.
 242  
      *
 243  
      * @see String#lastIndexOf(String)
 244  
      * @param substring desired substring in the target string
 245  
      * @return a function that will interrogate strings for the last index of
 246  
      * {@code substring}
 247  
      */
 248  
     public static UnaryFunction<String, Integer> lastIndexOf( String substring ) {
 249  7
         return new LastIndexOfString().bindSecond( substring );
 250  
     }
 251  
 
 252  
     /**
 253  
      * Answers a function which will give the substring of a given string beginning at
 254  
      * the given index.
 255  
      *
 256  
      * @see String#substring(int)
 257  
      * @param index index at which to start the substring
 258  
      * @return a function that will interrogate strings for substrings
 259  
      */
 260  
     public static UnaryFunction<String, String> substring( int index ) {
 261  4
         return new Substring().bindSecond( index );
 262  
     }
 263  
 
 264  82
     private static class HasSubstring extends BinaryPredicate<String, CharSequence> {
 265  
         public boolean matches( String first, CharSequence second ) {
 266  34
             return first.contains( second );
 267  
         }
 268  
 
 269  
         @Override
 270  
         protected String getFirstBoundDescription() {
 271  1
             return "a string that is a substring of <%1$s>";
 272  
         }
 273  
 
 274  
         @Override
 275  
         protected String getSecondBoundDescription() {
 276  1
             return "a string that contains the string <%1$s>";
 277  
         }
 278  
     }
 279  
 
 280  61
     private static class HasSameContents extends BinaryPredicate<String, CharSequence> {
 281  
         public boolean matches( String first, CharSequence second ) {
 282  25
             return first.contentEquals( second );
 283  
         }
 284  
 
 285  
         @Override
 286  
         protected String getFirstBoundDescription() {
 287  1
             return "<%1$s>";
 288  
         }
 289  
     }
 290  
 
 291  33
     private static class EndsWith extends BinaryPredicate<String, String> {
 292  
         public boolean matches( String first, String second ) {
 293  9
             return first.endsWith( second );
 294  
         }
 295  
 
 296  
         @Override
 297  
         protected String getFirstBoundDescription() {
 298  1
             return "a string that is a suffix of <%1$s>";
 299  
         }
 300  
 
 301  
         @Override
 302  
         protected String getSecondBoundDescription() {
 303  1
             return "a string that ends with <%1$s>";
 304  
         }
 305  
     }
 306  
 
 307  542
     private static class StartsWith extends BinaryPredicate<String, String> {
 308  
         public boolean matches( String first, String second ) {
 309  182
             return first.startsWith( second );
 310  
         }
 311  
 
 312  
         @Override
 313  
         protected String getFirstBoundDescription() {
 314  1
             return "a string that is a prefix of <%1$s>";
 315  
         }
 316  
 
 317  
         @Override
 318  
         protected String getSecondBoundDescription() {
 319  1
             return "a string that starts with <%1$s>";
 320  
         }
 321  
     }
 322  
 
 323  16
     private static class EqualsIgnoreCase extends BinaryPredicate<String, String> {
 324  
         public boolean matches( String first, String second ) {
 325  4
             return first.equalsIgnoreCase( second );
 326  
         }
 327  
 
 328  
         @Override
 329  
         protected String getFirstBoundDescription() {
 330  1
             return "a string equal to <%1$s> ignoring case";
 331  
         }
 332  
     }
 333  
 
 334  32
     private static class MatchesPattern extends BinaryPredicate<CharSequence, String> {
 335  
         public boolean matches( CharSequence first, String second ) {
 336  8
             return Pattern.compile( second ).matcher( first ).find();
 337  
         }
 338  
 
 339  
         @Override
 340  
         protected String getFirstBoundDescription() {
 341  1
             return "a regex that matches <%1$s>";
 342  
         }
 343  
 
 344  
         @Override
 345  
         protected String getSecondBoundDescription() {
 346  1
             return "a string that matches <%1$s>";
 347  
         }
 348  
     }
 349  
 
 350  18
     private static class CharAt extends BinaryFunction<String, Integer, Character> {
 351  
         public Character evaluate( String first, Integer second ) {
 352  6
             if ( second < 0 )
 353  2
                 return first.charAt( first.length() + second );
 354  
 
 355  4
             return first.charAt( second );
 356  
         }
 357  
     }
 358  
 
 359  15
     private static class IndexOfCharacter
 360  
         extends BinaryFunction<String, Integer, Integer> {
 361  
 
 362  
         public Integer evaluate( String first, Integer second ) {
 363  5
             return first.indexOf( second );
 364  
         }
 365  
     }
 366  
 
 367  21
     private static class IndexOfString extends BinaryFunction<String, String, Integer> {
 368  
         public Integer evaluate( String first, String second ) {
 369  7
             return first.indexOf( second );
 370  
         }
 371  
     }
 372  
 
 373  15
     private static class LastIndexOfCharacter
 374  
         extends BinaryFunction<String, Integer, Integer> {
 375  
 
 376  
         public Integer evaluate( String first, Integer second ) {
 377  5
             return first.lastIndexOf( second );
 378  
         }
 379  
     }
 380  
 
 381  21
     private static class LastIndexOfString
 382  
         extends BinaryFunction<String, String, Integer> {
 383  
 
 384  
         public Integer evaluate( String first, String second ) {
 385  7
             return first.lastIndexOf( second );
 386  
         }
 387  
     }
 388  
 
 389  12
     private static class Substring extends BinaryFunction<String, Integer, String> {
 390  
         public String evaluate( String first, Integer second ) {
 391  4
             return first.substring( second );
 392  
         }
 393  
     }
 394  
 }