Loop to handle many field condition statements

T

teser3

I have some code that works great in my Bean class that looks like the
below where I have around 15 fields. For example I condensed it to 2
fields:

//bean model part
HashMap errors = new HashMap();
String firstname = "Joe";
String lastname = "Miller";

if (firstname.equals(""))
{
errors.put("firstname","missing firstname".);
}
if (lastname.equals(""))
{
errors.put("lastname","missing lastname");
}


//Each field is then called like this in a View page:
errors.get("firstname"));
errors.get("lastname"));Anyway I can create a loop to go through all
15 fields or how is the best way to do this??


//use Array or Iterator or List or what to get the 15 fields then
loop?
for(int i= 0;i < 15;i++)
{
/*
how would I make this part work?
if (firstname.equals(""))
{
errors.put("firstname","missing firstname".);
}
if (lastname.equals(""))
{
errors.put("lastname","missing lastname");
}
/*
}

Please advise.
 
J

Joshua Cranmer

Anyway I can create a loop to go through all
15 fields or how is the best way to do this??


NOTE: I do not have experience with beans, so there might be an
exception to my rules here. Soliciting advice from anyone with more
knowledge here to supersede me.

The best way is to internally store the fields as a HashMap and iterate
over that.

The second best way is to internally store the fields as a TreeMap and
iterate over that.

The third best way is to internally store the fields as anything else
that implements the Map interface and iterate over that.

The fourth best way is to manually go through each field.

There is a fifth way that I really don't recommend. Try using the first
method instead.

Still want to hear it?

This uses reflection:


Field[] fields = {< classname >}.class.getDeclaredFields();
for (Field f : fields) {
if (f.getType().getName().equals("java.lang.String")) {
errors.put(f.getName(),(String)f.get(this));
}
}

Like all reflection, that would have to be wrapped with a myriad of
exceptions; it is also prone to breaking if one is not careful (although
annotations would considerably help if this were being done). Finally,
it is also considerably slower than using a HashMap.
 
G

GArlington

Anyway I can create a loop to go through all
15 fields or how is the best way to do this??

NOTE: I do not have experience with beans, so there might be an
exception to my rules here. Soliciting advice from anyone with more
knowledge here to supersede me.

The best way is to internally store the fields as a HashMap and iterate
over that.

The second best way is to internally store the fields as a TreeMap and
iterate over that.

The third best way is to internally store the fields as anything else
that implements the Map interface and iterate over that.

The fourth best way is to manually go through each field.

There is a fifth way that I really don't recommend. Try using the first
method instead.

Still want to hear it?

This uses reflection:

Field[] fields = {< classname >}.class.getDeclaredFields();
for (Field f : fields) {
if (f.getType().getName().equals("java.lang.String")) {
errors.put(f.getName(),(String)f.get(this));
}

}

Like all reflection, that would have to be wrapped with a myriad of
exceptions; it is also prone to breaking if one is not careful (although
annotations would considerably help if this were being done). Finally,
it is also considerably slower than using a HashMap.

How about using reflection once (on init) to populate the fields
hashmap and then use the hashmap on each validation run?
 
L

Lew

Joshua said:
There is a fifth way that I really don't recommend. Try using the first
method instead.

Still want to hear it?

This uses reflection:

Field[] fields = {< classname >}.class.getDeclaredFields();
for (Field f : fields) {
if (f.getType().getName().equals("java.lang.String")) {
errors.put(f.getName(),(String)f.get(this));
}

}

Like all reflection, that would have to be wrapped with a myriad of
exceptions; it is also prone to breaking if one is not careful (although
annotations would considerably help if this were being done). Finally,
it is also considerably slower than using a HashMap.
How about using reflection once

One should avoid using reflection almost always. When one does use
reflection, it should be limited to Class.forName() almost always.
 
O

Oliver Wong

I have some code that works great in my Bean class that looks like the
below where I have around 15 fields. For example I condensed it to 2
fields:

//bean model part
HashMap errors = new HashMap();
String firstname = "Joe";
String lastname = "Miller";

if (firstname.equals(""))
{
errors.put("firstname","missing firstname".);
}
if (lastname.equals(""))
{
errors.put("lastname","missing lastname");
}


//Each field is then called like this in a View page:
errors.get("firstname"));
errors.get("lastname"));Anyway I can create a loop to go through all
15 fields or how is the best way to do this??
[...]

This is the third time I've seen you post this question. Why haven't
you accepted the answers you were already given?

http://groups.google.com/group/comp.lang.java.programmer/msg/eb3f492ca7ae8c5b

- Oliver
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top