| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
package jaggregate; |
| 7 | |
|
| 8 | |
import java.util.regex.Pattern; |
| 9 | |
|
| 10 | |
import static jaggregate.internal.ArgumentChecks.*; |
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
public class Strings { |
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 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 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | 1 | protected Strings() { |
| 37 | 1 | throw new UnsupportedOperationException(); |
| 38 | |
} |
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 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 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 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 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 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 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 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 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 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 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
|
| 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 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 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 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 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 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 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 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | |
|
| 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 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
public static UnaryFunction<String, Character> charAt( int index ) { |
| 198 | 6 | return new CharAt().bindSecond( index ); |
| 199 | |
} |
| 200 | |
|
| 201 | |
|
| 202 | |
|
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
public static UnaryFunction<String, Integer> indexOf( int ch ) { |
| 211 | 5 | return new IndexOfCharacter().bindSecond( ch ); |
| 212 | |
} |
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
public static UnaryFunction<String, Integer> indexOf( String substring ) { |
| 224 | 7 | return new IndexOfString().bindSecond( substring ); |
| 225 | |
} |
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | |
|
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
public static UnaryFunction<String, Integer> lastIndexOf( int ch ) { |
| 236 | 5 | return new LastIndexOfCharacter().bindSecond( ch ); |
| 237 | |
} |
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | |
|
| 246 | |
|
| 247 | |
|
| 248 | |
public static UnaryFunction<String, Integer> lastIndexOf( String substring ) { |
| 249 | 7 | return new LastIndexOfString().bindSecond( substring ); |
| 250 | |
} |
| 251 | |
|
| 252 | |
|
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | |
|
| 257 | |
|
| 258 | |
|
| 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 | |
} |