struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Jerry Jalenak" <Jerry.Jale...@LABONE.com>
Subject RE: [OT - Java] How can I do this in Java?
Date Tue, 01 Oct 2002 15:29:57 GMT
Once again - thanks to EVERYONE who weighted in on this.  I certainly didn't
mean to start the exchange the ensued, but wasn't it interesting?  I learned
alot about reflection, etc. from the exchanges.

I went ahead, dumped the reflection code, and changed it all to use a static
HashMap.  Ended up reducing the overall method to 1 single return statement!

Way Cool!!!!

Jerry

> -----Original Message-----
> From: Max Cooper [mailto:max@maxcooper.com]
> Sent: Monday, September 30, 2002 5:42 PM
> To: Struts Users Mailing List
> Subject: Re: [OT - Java] How can I do this in Java?
> 
> 
> I agree that reflection is not a good solution here, but does 
> have useful
> application in general (but it isn't needed here, so don't 
> use it). Use a
> singleton or a statically-initialized Map to look up the 
> patterns based on
> the state. Save yourself some time by putting Patterns in the map,
> identified by the state represented as a String.
> 
> patternMap.add("AK", Pattern.compile("^[0-9]{1,7}$"));
> 
> Put the map in its own class (perhaps a singleton), so you would get
> patterns from it with a call like one of these:
> 
> Pattern pattern = 
> LicensePatterns.getInstance().getPattern(licenseState);
> 
> -or-
> 
> Pattern pattern = LicensePatterns.getPattern(licenseState);
> 
> Putting the patterns is a Properties file seems like a good 
> idea, too. How
> often do the patterns change? Perhaps you could add a 
> refresh() method to
> the singleton class to reload the patterns from the 
> properties file if you
> anticipate that they will change while the app is supposed to 
> be running.
> This is usually the case, but it is often overlooked when setting such
> "constants".
> 
> -Max
> 
> ----- Original Message -----
> From: "Jerry Jalenak" <Jerry.Jalenak@LABONE.com>
> To: <struts-user@jakarta.apache.org>
> Sent: Monday, September 30, 2002 7:49 AM
> Subject: [OT - Java] How can I do this in Java?
> 
> 
> > OK - off topic, but Sun's java forum sucks, and there are 
> an incredible
> > number of Java guru's on this list, so I thought I'd throw 
> this out here.
> > (That and I am using this in a custom validation routine  
> :-))    Any help
> > would be GREATLY appreciated!
> >
> > Here's the scenario - I've got a series of static constants 
> that represent
> > Java regular expressions.  These RE's are used to validate 
> driver license
> > formats for the 50 states + DC.  The strings look like this:
> >
> > public static final String AK = "^[0-9]{1,7}$";
> > public static final String AL = "^[0-9]{7}$";
> > public static final String AR = "^[0-9]{8,9}$";
> > public static final String AZ =
> > "^[0-9ABDY][0-9]{8}$|^[A-Z][0-9]{3,6}$|^[A-Z]{2}[0-9]{3,5}$";
> > public static final String CA = "^[A-Z][0-9]{4,7}$";
> > public static final String CO =
> > "^[A-Z][0-9]{1,6}$|^[A-Z]{2}[0-9]{1,6}$|^[0-9]{9}$";
> > etc. etc. etc.
> >
> > On my form I have a drop-down box of states, and a field 
> for the license
> > number.  In my custom validator routine, I pick up the 
> value of the state,
> > and build a string to represent the constant - i.e.
> >
> > private static boolean validateDriversLicenseNumber(String
> > licenseState, String licenseNumber)
> > {
> > String licenseConstant = "Constants." + licenseState;
> >
> > I then want to use "licenseConstant" in a Pattern / Match:
> >
> > Pattern p = Pattern.compile(licenseConstant,
> > Pattern.CASE_INSENSITIVE);
> > Match m = p.matcher(licenseNumber);
> > return (m.find());
> > }
> >
> > Obviously the line "String licenseConstant = "Constants." + 
> licenseState;"
> > does not give me the value of Constant.<state name>; the 
> question I have
> is,
> > is there a method (or something) that will allow me to build such a
> string,
> > and return the value (i.e. the regular expression)?  Or is 
> there a better
> > way of doing this?
> >
> > TIA!
> >
> > Jerry Jalenak
> > Web Publishing
> > LabOne, Inc.
> > 10101 Renner Blvd.
> > Lenexa, KS  66219
> > (913) 577-1496
> > jerry.jalenak@labone.com
> >
> >
> > This transmission (and any information attached to it) may 
> be confidential
> and is intended solely for the use of the individual or 
> entity to which it
> is addressed. If you are not the intended recipient or the person
> responsible for delivering the transmission to the intended 
> recipient, be
> advised that you have received this transmission in error and 
> that any use,
> dissemination, forwarding, printing, or copying of this information is
> strictly prohibited. If you have received this transmission 
> in error, please
> immediately notify LabOne at (800)388-4675.
> >
> >
> >
> > --
> > To unsubscribe, e-mail:
> <mailto:struts-user-unsubscribe@jakarta.apache.org>
> > For additional commands, e-mail:
> <mailto:struts-user-help@jakarta.apache.org>
> >
> >
> 
> 
> 
> --
> To unsubscribe, e-mail:   
<mailto:struts-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail:
<mailto:struts-user-help@jakarta.apache.org>


This transmission (and any information attached to it) may be confidential and is intended
solely for the use of the individual or entity to which it is addressed. If you are not the
intended recipient or the person responsible for delivering the transmission to the intended
recipient, be advised that you have received this transmission in error and that any use,
dissemination, forwarding, printing, or copying of this information is strictly prohibited.
If you have received this transmission in error, please immediately notify LabOne at (800)388-4675.



--
To unsubscribe, e-mail:   <mailto:struts-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:struts-user-help@jakarta.apache.org>


Mime
View raw message