No question today just sharing my frustration. I got tired of not have a way
of storing name value pairs on the client side the way that I can on the
server so I through togather this little js file that I named Collection. It
is not modeled after the java.util.collection but rather more like a
HashMap. If anyone else can benifit from this utility have at it.

/*
Collection.js

AUTHOR:
        Bryan LaPlante
        [EMAIL PROTECTED]
        Network Web Applications
        http://www.netwebapps.com

DESCRIPTION: Collection
creates a new collection of name=value pairs and provides methods for
retrieving one or a list of keys or values.

CREATE A COLLECTION:
        myCollection = new Collection();

METHODS
        myCollection.clear(); // Removes all of the key value pairs but does
not destroy the collection.
        myCollection.put(key,value); //Creates an entry into the collection
        myCollection.getValue(key); // does a case sensitive search for a
key in the collection and returns it's value
        myCollection.getValueNoCase(key); // does a case insensitive search
for a key in the collection and returns it's value
        myCollection.getKeyArray(); //returns an array of keys
        myCollection.getValueArray(); // returns an array of values

EXAMPLE:
        Un comment the code at the bottom of the script to see
        an example of how to use the collection.
*/

        function Map(key,val){
                this.key = key;
                this.val = val;
        }

        function Collection(){
                this.collectionMap = new Array();
                this.clear = clearMap;
                this.put = putMap;
                this.getValue = getValue;
                this.getValueNoCase = getValueNoCase;
                this.getKeyArray = getKeyArray;
                this.getValueArray = getValueArray;
        }

        function putMap(key,val){
                existingKeys = this.getKeyArray();
                isUniqueKey = true;

                for(i=0; i<existingKeys.length; i++){
                        if(existingKeys[i] == key){
                                isUniqueKey = false;
                                this.collectionMap[i].val=val;
                                break;
                        }
                }

                if(isUniqueKey){
                        this.collectionMap[this.collectionMap.length]= new
Map(key,val);
                }
        }

        function clearMap(){
                this.collectionMap.length=0;
        }

        function getValue(key){
                for(i=0; i<this.collectionMap.length; i++){
                        if(this.collectionMap[i].key == key){
                                return this.collectionMap[i].val;
                        }
                }
                return null;
        }

        function getValueNoCase(key){
                for(i=0; i<this.collectionMap.length; i++){
                        if(this.collectionMap[i].key.toLowerCase() ==
key.toLowerCase()){
                                return this.collectionMap[i].val;
                        }
                }
                return null;
        }

        function getKeyArray(){
                keyArray = new Array();
                for(i=0; i<this.collectionMap.length; i++){
                        keyArray[keyArray.length]=this.collectionMap[i].key;
                }
                return keyArray;
        }

        function getValueArray(){
                valueArray = new Array();
                for(i=0; i<this.collectionMap.length; i++){

valueArray[valueArray.length]=this.collectionMap[i].val;
                }
                return valueArray;
        }
        /*
        /////------- Uncomment this section for testing
--------///////////////////////

        //create the collection
        collection = new Collection();

        // clear the collection
        collection.clear();

        // add name value pairs to the collection
        collection.put("bryan","user1");
        collection.put("bruce","user2");
        collection.put("dan","user3");

        // putting bryan again will update the previous entry's value to
user4
        collection.put("bryan","user4");

        // get an array of keys
        myKeys = collection.getKeyArray();

        // get an array of values
        myVals = collection.getValueArray();

        //Output the names and their values into an html list item
        document.write("<ul>");
        for(c=0; c<myKeys.length; c++){
                document.write("<li>" + myKeys[c].toString() +"=" +
myVals[c].toString());
        }
        document.write("<ul>");
        */

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to