On Mon, Sep 30, 2002 at 07:59:18AM -0700, Taylor, Jason wrote:

> sounds like a job for reflection (java.lang.reflect.*;) Have
> you looked at java.lang.Class and java.lang.reflect.Field?

I suppose reflection would work, but what's wrong with a simple
HashMap or possibly even a simple array as a lookup table?
(Of course, I wouldn't put the HashMap itself in any public
API, but even doing that is better than using reflection, IMO.)

If you just make your states ints or some data type that can return 
a simple int identifier, you can use them as indexes into an array
of the regexp string.  If you don't like that approach, simply
build a HashMap with the state as the key, and the regexp string
as the value.   

// Simple example of using the HashMap that lets you keep
// the API you've shown us, and you don't have to resort to
// using reflection.

public class StateAbbrToLicenseRegExpThingy {

  private static final HashMap s_stateToLicenseRE = new HashMap();
  static {
    s_stateToLicenseRE.put("AK", "^[0-9]{1,7}$");
    s_stateToLicenseRE.put("AL", "^[0-9]{7}$");
    s_stateToLicenseRE.put("AR", "^[0-9]{8,9}$"); 
    // etc.
    }

    public static String getLicenseRegExpForState(String state) {
        // Add error checking as desired here.
        return (String)s_stateToLicenseRE.get(state);
    }

}

> -----Original Message-----
> From: Jerry Jalenak [mailto:[EMAIL PROTECTED]]

> 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}$";

> 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?

Use a lookup table as shown above to get the regexp for the state.

Cheers,
John
-- 
end of line


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to