litesoft
@ 936
litesoft / trunk / DeviceDesktopTest / src / org / litesoft / core / util / stringmatching / StringMatcherFactory.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt package org.litesoft.core.util.stringmatching; import org.litesoft.core.typeutils.*; public class StringMatcherFactory { /** * Is the string 'pInQuestion' made up of the 'pParts' where if only 1, then must be equal, otherwise * the pInQuestion.startsWith(first) && pInQuestion.endsWith(last) && the middle must be found in * order with no overlap between. * * @param pParts - Null treated as empty & Null Elements treated as "" * * @return Appropriate matcher or 'null' if pParts is empty and all elements are empty */ public static StringMatcher createEquals( String... pParts ) { return createEquals( false, pParts ); } /** * Is the string 'pInQuestion' made up of the 'pParts' where if only 1, then must be equal, otherwise * the pInQuestion.startsWith(first) && pInQuestion.endsWith(last) && the middle must be found in * order with no overlap between. * * @param pParts - Null treated as empty & Null Elements treated as "" * * @return Appropriate matcher or 'null' if pParts is empty and all elements are empty */ public static StringMatcher createEqualsIgnoreCase( String... pParts ) { return createEquals( true, pParts ); } /** * Is the string 'pInQuestion' made up of the 'pParts' where if only 1, then must be equal, otherwise * the pInQuestion.startsWith(first) && pInQuestion.endsWith(last) && the middle must be found in * order with no overlap between. * * @param pIgnoreCase - true if Character "case" should be ignored * @param pParts - Null treated as empty & Null Elements treated as "" * * @return Appropriate matcher or 'null' if pParts is empty and all elements are empty */ public static StringMatcher createEquals( boolean pIgnoreCase, String... pParts ) { return LLcreate( pParts, false, pIgnoreCase ); } /** * Is the string 'pInQuestion' made up of the 'pParts' where if only 1, then treated as a "StartsWith", otherwise * the pInQuestion.startsWith(first) && pInQuestion.endsWith(last) && the middle must be found in * order with no overlap between. * * @param pParts - Null treated as empty & Null Elements treated as "" * * @return Appropriate matcher or 'null' if pParts is empty and all elements are empty */ public static StringMatcher createStartsWith( String... pParts ) { return createStartsWith( false, pParts ); } /** * Is the string 'pInQuestion' made up of the 'pParts' where if only 1, then treated as a "StartsWith", otherwise * the pInQuestion.startsWith(first) && pInQuestion.endsWith(last) && the middle must be found in * order with no overlap between. * * @param pParts - Null treated as empty & Null Elements treated as "" * * @return Appropriate matcher or 'null' if pParts is empty and all elements are empty */ public static StringMatcher createStartsWithIgnoreCase( String... pParts ) { return createStartsWith( true, pParts ); } /** * Is the string 'pInQuestion' made up of the 'pParts' where if only 1, then treated as a "StartsWith", otherwise * the pInQuestion.startsWith(first) && pInQuestion.endsWith(last) && the middle must be found in * order with no overlap between. * * @param pIgnoreCase - true if Character "case" should be ignored * @param pParts - Null treated as empty & Null Elements treated as "" * * @return Appropriate matcher or 'null' if pParts is empty and all elements are empty */ public static StringMatcher createStartsWith( boolean pIgnoreCase, String... pParts ) { return LLcreate( pParts, true, pIgnoreCase ); } private static StringMatcher LLcreate( String[] pParts, boolean pSingleIsNotEqualsButStartsWith, boolean pIgnoreCase ) { pParts = Strings.deNull( pParts ); String[] zParts = new String[pParts.length]; int zPartsLength = 0; for ( int i = 0; i < pParts.length; i++ ) { String part = pParts[i]; if ( part == null ) { part = ""; } zParts[i] = pIgnoreCase ? part.toLowerCase() : part; zPartsLength += part.length(); } if ( zPartsLength == 0 ) { return null; } switch ( zParts.length ) { case 1: if ( pSingleIsNotEqualsButStartsWith ) { return new StartsWithStringMatcher( zPartsLength, pIgnoreCase, zParts[0] ); } else { return new EqualsStringMatcher( zPartsLength, pIgnoreCase, zParts[0] ); } case 2: if ( zParts[1].length() == 0 ) { return new StartsWithStringMatcher( zPartsLength, pIgnoreCase, zParts[0] ); } if ( zParts[0].length() == 0 ) { return new EndsWithStringMatcher( zPartsLength, pIgnoreCase, zParts[1] ); } return new StartsAndEndsWithStringMatcher( zPartsLength, pIgnoreCase, zParts[0], zParts[1] ); default: return new PartsStringMatcher( zPartsLength, pIgnoreCase, zParts ); } } } |
Commits for litesoft/trunk/DeviceDesktopTest/src/org/litesoft/core/util/stringmatching/StringMatcherFactory.java
Revision | Author | Commited | Message |
---|---|---|---|
936 | GeorgeS | Sun 01 Jun, 2014 20:19:38 +0000 | Language Support |