litesoft
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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt package org.litesoft.util; import org.litesoft.commonfoundation.typeutils.*; import java.util.*; import org.litesoft.core.util.*; import org.litesoft.core.util.HintValidator.*; import org.litesoft.core.util.Hintable.*; import org.litesoft.logger.*; public final class HintController { public static final String PER_PROBLEM_PREFIX = "\n "; public static Logger LOGGER = LoggerFactory.getLogger( HintController.class ); public static final String HINT_PROPERTY_PREFIX = "Hint."; public static String noValidatorText( String pCategory, String pGroup, String pName ) { return "No Validator for: Cat(" + pCategory + "), Group(" + pGroup + "), Name(" + pName + ")"; } public static void noValidator( String pCategory, String pGroup, String pName ) { if ( LOGGER.warn.isEnabled() ) { LOGGER.warn.log( noValidatorText( pCategory, pGroup, pName ) ); } } /** * @param pName !null or empty * @param pValue !null or empty * * @return null is no problem, "" is no validator, !null & !"" is the problem */ public synchronized static String validate( String pCategory, String pGroup, String pName, String pValue ) { pName = Strings.assertNotNullNotEmpty( "Name", pName ); pValue = Strings.assertNotNullNotEmpty( "Value", pValue ); int zSwitchOn = ((pCategory == null) ? 0 : 2) + // ((pGroup == null) ? 0 : 1); Set<HintValidator> zValidators = new HashSet<HintValidator>(); appendValidators( zValidators, null, null, pName ); // Do wild cards switch ( zSwitchOn ) { default: case 0: // Nether break; case 1: // Group Only appendValidators( zValidators, null, pGroup, pName ); break; case 2: // Cat Only appendValidators( zValidators, pCategory, null, pName ); break; case 3: // Both appendValidators( zValidators, pCategory, pGroup, pName ); appendValidators( zValidators, pCategory, null, pName ); appendValidators( zValidators, null, pGroup, pName ); break; } List<String> zProblems = new ArrayList<String>(); for ( HintValidator zValidator : zValidators ) { String problem = zValidator.validate( pName, pValue ); if ( problem == null ) { return null; } zProblems.add( problem ); } if ( zSwitchOn == 3 ) // Stop future complaining if Cat & Group exists { add( new NullHintValidator( pCategory, pGroup, pName ) ); } if ( zProblems.isEmpty() ) { return ""; // Indicate that no Validators were found! } StringBuilder problems = new StringBuilder(); Collections.sort( zProblems ); for ( String zProblem : zProblems ) { problems.append( PER_PROBLEM_PREFIX ).append( zProblem ); } return problems.toString(); } private static void appendValidators( Set<HintValidator> pValidators, String pCategory, String pGroup, String pName ) { ValidatorsForCatGroup zValidatorsForCatGroup = sValidators.get( new CategoryGroup( pCategory, // pGroup ) ); if ( zValidatorsForCatGroup != null ) { ValidatorsProxyForName zProxyForName = zValidatorsForCatGroup.getProxy( pName ); if ( zProxyForName != null ) { zProxyForName.appendValidators( pValidators ); } } } private static Map<CategoryGroup, ValidatorsForCatGroup> sValidators = // new HashMap<CategoryGroup, ValidatorsForCatGroup>(); public synchronized static void add( HintValidator pValidator ) { if ( pValidator != null ) { CategoryGroup[] zCategoryGroups = pValidator.getRegisterForCategoryGroups(); if ( zCategoryGroups != null ) { for ( CategoryGroup cg : zCategoryGroups ) { ValidatorsForCatGroup zValidatorsForCatGroup = sValidators.get( cg ); if ( zValidatorsForCatGroup == null ) { sValidators.put( cg, new ValidatorsForCatGroup( pValidator ) ); } else { zValidatorsForCatGroup.add( pValidator ); } } } } } /** * @return !null and No Nulls */ public static Hint[] getGlobalHints() { return getGlobalHintManager().getHints(); } /** * Return the value associated with pName if any. * * @param pName !null or empty * * @return null or value associated with pName * * @throws IllegalArgumentException if pName was null or empty */ public static String getGlobalHintValue( String pName ) throws IllegalArgumentException { return getGlobalHintManager().getHintValue( pName ); } /** * Add the Hint (if pValue is not null or empty) and return any previous Hint's Value with the same pName * * @param pName !null or empty * * @return null or previous Hint's Value with the same pName * * @throws IllegalArgumentException if pName was null or empty */ public static String addGlobalHint( String pName, String pValue ) throws IllegalArgumentException { if ( null == (pValue = Strings.noEmpty( pValue )) ) { return null; } return sHintManager.backDoorAddHint( new Hint( pName, pValue ) ); } public static HintManager getGlobalHintManager() { return sHintManager; } private static GlobalHintManager sHintManager = new GlobalHintManager(); public static class GlobalHintManager extends HintManager { public HintManager aboutToAdd() { return NULL.aboutToAdd().addAll( this ); } public String addHint( Hint pHint ) { throw new IllegalStateException( "Attempt to add a Hint to the Global HintManager" ); } String backDoorAddHint( Hint pHint ) { return super.addHint( pHint ); } } private static class ValidatorsForCatGroup { private Map<String, ValidatorsProxyForName> mValidatorProxiesByName = // new HashMap<String, ValidatorsProxyForName>(); public ValidatorsForCatGroup( HintValidator pInitialValidator ) { add( pInitialValidator ); } public ValidatorsProxyForName getProxy( String pName ) { return mValidatorProxiesByName.get( pName ); } public void add( HintValidator pValidator ) { String[] names = pValidator.getRegisterForHintNames(); if ( names != null ) { for ( String name : names ) { ValidatorsProxyForName zProxyForName = mValidatorProxiesByName.get( name ); if ( zProxyForName == null ) { mValidatorProxiesByName.put( name, new ValidatorsProxyForName( pValidator ) ); } else { zProxyForName.add( pValidator ); } } } } public String toString() { return "ValidatorsForCatGroup: " + mValidatorProxiesByName; } } private static class ValidatorsProxyForName { private List<HintValidator> mValidators = new ArrayList<HintValidator>(); public ValidatorsProxyForName( HintValidator pInitialValidator ) { mValidators.add( pInitialValidator ); } public void add( HintValidator pValidator ) { mValidators.add( pValidator ); } public void appendValidators( Set<HintValidator> pValidators ) { pValidators.addAll( mValidators ); } public String toString() { return "ValidatorsProxyForName: " + mValidators; } } private static class NullHintValidator implements HintValidator { private CategoryGroup mCG; private String mName; public NullHintValidator( String pCategory, String pGroup, String pName ) { mCG = new CategoryGroup( pCategory, pGroup ); mName = pName; } public CategoryGroup[] getRegisterForCategoryGroups() { return new CategoryGroup[]{mCG}; } public String[] getRegisterForHintNames() { return new String[]{mName}; } public String validate( String pName, String pValue ) { return null; } public String toString() { return "NullHintValidator( " + mCG + ", '" + mName + "' )"; } } } |
Commits for litesoft/trunk/Java/core/jvm1.5/src/org/litesoft/util/HintController.java
Revision | Author | Commited | Message |
---|---|---|---|
939 Diff | GeorgeS | Mon 02 Jun, 2014 21:30:31 +0000 | Extracting commonfoundation |
821 Diff | GeorgeS | Sun 19 Aug, 2012 00:08:41 +0000 | |
801 Diff | GeorgeS | Wed 15 Aug, 2012 03:59:02 +0000 | |
49 Diff | GeorgeS | Mon 12 Apr, 2010 02:59:10 +0000 | License Text |
2 | GeorgeS | Sun 07 Feb, 2010 12:50:58 +0000 |