Why not create specific classes, where all the parameters are of the
specific type. Override the constructor so that it checks the types of a
collection it is passed

So you method would instead be

        public ExchangeRate set(int index, ExchangeRate element) 
        {
                return (ExchangeRate )super.set(index, element);
        }

This is as type safe as you are going to get. There is no way that this can
throw a class cast, as you can only pass ExchangeRate objects to it

A Constructor would be

public class ExchangeRateSet
{
        public ExchangeRateSet ( Collection col )
        {
                Iterator I = col.iterator ();
                        // Check all items
                        // Add them to this collection
                        // Guaranteed type sage
        }
}

keith

 

------------------------------------------------------------------------- 
Keith Sterling  MIAP       
Jacobs Rimell
VP of Engineering

Jacobs Rimell Ltd
24 Chiswell St
London EC1Y 4TY             
Tel : +44 207 786 4000
Mob : +44 7771 597 630 
Fax : +44 207 786 4004 

Email : [EMAIL PROTECTED]

MSN : [EMAIL PROTECTED]
http://www.jacobsrimell.com 
------------------------------------------------------------------------- 
IMPORTANT NOTICE: 
Unless posted by the originating sender to a public news group, this email
should be considered confidential, may be legally privileged, and is for the
intended recipient only.  If you are not the intended recipient, please
inform the sender and delete the email immediately.
WARNING: 
It is impossible to guarantee the content of this message when it is
delivered across the internet, therefore the  sender accepts no liability
for any claims arising from the information contained.

 


-----Original Message-----
From: Scot Mcphee [mailto:[EMAIL PROTECTED]] 
Sent: 06 September 2002 08:58
To: JDJList
Subject: [jdjlist] type safe collections


Could anyone pls point me to good discussions on implementing type-safe
collections. I'm pretty familar with the Collections API and I have got +
read the Zukowski book, Java Collections, which is excellent (I recommend
this  heartily if you are still stuck in using Vectors, Stacks, Hashtables,
Enumerations or worse). I'd like to get some good hard info on extending the
collections classes or implementing the interfaces to make collections which
are type safe.

By type safe I mean making sure all elements of a particular collection are
of type T.  Currently I have done it by extending a collection
implementation such as ArrayList and overriding certain methods;

        /**
         * @see java.util.List#set(int, Object)
         */
        public Object set(int index, Object element) {
                if (element instanceof ExchangeRate) {
                        return super.set(index, element);
                } else {
                        throw new ClassCastException(eMsg);
                }
        }

However I'm not sure what to do with the constructors that take a collection
interface as the argument. Currently they throw
OperationNotSupportedException.

Also I just want to sanity check what I'm doing. Any tips or pointers
appreciated!

regs
scot.

___________________________________________
Scot Mcphee -                 Snr Developer
            -       (mobile) +61-412-957414
___________________________________________
Tigerex     -        http://www.tigerex.net
            -          (bus) +61-2-82593613
___________________________________________



To change your JDJList options, please visit:
http://www.sys-con.com/java/list.cfm

To change your JDJList options, please visit: http://www.sys-con.com/java/list.cfm

Reply via email to