You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Dan Allen <da...@mojavelinux.com> on 2003/03/17 07:23:25 UTC

[OT] suggestion for getting properties

I have a model class for my company which has numbers.  I would like
to create a method like

getNumbers()

which returns all of the different phone numbers that are in the
database as a collection.  However, when I get them, I need to know
which ones they are for display purposes (just a list of numbers
won't do much good or else you end up calling a fax machine).  I
thought of using a HashMap like

HashMap numbers = new HashMap();
numbers.put("phone", getPhone());
numbers.put("fax", getPhone());

etc.

but then I realized that HashMaps screw up the order.  I need to
keep the order that I place them in the Hash so I can do

<c:forEach items="${company.numbers}" var="number">
    <bean:message key="${number.key}"/>: <c:out value="${number.value}"/>
</c:forEach>

I tried to implement a FifoMap but failed because I didn't know how
to subclass Map.Entry

POSSIBLE SOLUTION:

I guess the solution would be to create a Bean for a Phone Number
and then have getNumbers() return a Collection of phone number
beans.  Is that the best way or is a map still possible?

Dan

-- 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
Daniel Allen, <da...@mojavelinux.com>
http://www.mojavelinux.com/
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
"I'm old enough to know better, but still too young to care."
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

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


Re: [OT] suggestion for getting properties

Posted by Dan Allen <da...@mojavelinux.com>.
> I don't think this would give the result you are looking for.
> A map will map a key to a value, and so in the above example, no mattery how 
> many numbers you add to the map, there will only be 2 numbers in the end: the 
> one mapped to "phone" and the one mapped to "fax".

I don't think you quite understood my question, so let me rephrase.
For a single company, I want to get a map of the variety of phone
numbers.  There are many different types of phone numbers these days
and when displaying on the jsp page, it is messy to have so many
checks for empty numbers to find the type the company actually uses.
My goal was, for a single company, to get a map of the phone number
varieties to iterate on.

The problem was, when I used the type as the key and the number as
the value, the map would reorder the number arbitrarily, so when I
display them "fax" would come first and "phone" would come last.

Now do you see where I am going?

Dan

-- 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
Daniel Allen, <da...@mojavelinux.com>
http://www.mojavelinux.com/
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
Real programmers just hate to get up in the morning, and 
contrary to Ordinary People, they're in better shape as 
it gets closer to nighttime.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

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


Re: [OT] suggestion for getting properties

Posted by Karl Stenerud <ka...@webartjapan.com>.
2003 3月 17 月曜日 15:23、Dan Allen さんは書きました:
> I have a model class for my company which has numbers.  I would like
> to create a method like
>
> getNumbers()
>
> which returns all of the different phone numbers that are in the
> database as a collection.  However, when I get them, I need to know
> which ones they are for display purposes (just a list of numbers
> won't do much good or else you end up calling a fax machine).  I
> thought of using a HashMap like
>
> HashMap numbers = new HashMap();
> numbers.put("phone", getPhone());
> numbers.put("fax", getPhone());

I don't think this would give the result you are looking for.
A map will map a key to a value, and so in the above example, no mattery how 
many numbers you add to the map, there will only be 2 numbers in the end: the 
one mapped to "phone" and the one mapped to "fax".

2 things come to mind depending on what you want to accomplish:
put 2 methods in your model:

    public Colleciton getPhoneNumbers() {
        ...
    }

    public Collection getFaxNumbers() {
        ....
    }


Or, if you need to get all of the numbers in one go, try making a business 
data object:

public class TelephoneNumberBDO {

    // This kinda sucks. you'd probably do better with a typesafe enumeration
    public static String PHONE_TYPE_VOICE = "voice";
    public static String PHONE_TYPE_FAX = "fax";

    private String phoneNumber = null;
    public String getPhoneNumber() {
        return phoneNumber;
    }
    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    private string type = null;
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }

Then get your data from the database, build your business data objects, and 
put them into a collection (LinkedList will do fine for this) that will be 
returned by getNumbers().

Now you can iterate over TelephoneNumberBDOs in your jsp and pull out type and 
phoneNumber.


>
> etc.
>
> but then I realized that HashMaps screw up the order.  I need to
> keep the order that I place them in the Hash so I can do
>
> <c:forEach items="${company.numbers}" var="number">
>     <bean:message key="${number.key}"/>: <c:out value="${number.value}"/>
> </c:forEach>
>
> I tried to implement a FifoMap but failed because I didn't know how
> to subclass Map.Entry

I don't think you'd want to subclass Map.Entry.  This only holds a key-value 
pair for when you view the map as a collection.


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