KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > plankton > data > DefaultStateMap


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: DefaultStateMap.java,v 1.8 2004/02/01 05:16:28 christianc Exp $
19  */

20 package org.enhydra.barracuda.plankton.data;
21
22 import java.io.*;
23 import java.util.*;
24
25 import org.apache.log4j.*;
26
27 /**
28  * <p>A StateMap is an object that is capable of carrying state
29  * information along with it--you can put properties into the
30  * state and then get them back out.
31  *
32  * <p>This class provides the default implementation of a StateMap.
33  * The underlying storage structure is a HashMap, so it a) isn't
34  * threadsafe and b) accepts nulls. If you need more than the minimal
35  * functionality exposed in the StateMap interface, you should work
36  * with the underlying Map structure.
37  *
38  * <p>Key entities that implement StateMap:
39  * <ul>
40  * <li>BaseEvent</li>
41  * <li>EventContext</li>
42  * <li>FormMap</li>
43  * </ul>
44  */

45 public class DefaultStateMap implements StateMap {
46
47     //public vars
48
protected static final Logger logger = Logger.getLogger(DefaultStateMap.class.getName());
49
50     //private vars
51
protected Map props = null; //private property map
52

53     /**
54      * set a property in this StateMap
55      *
56      * @param key the key object
57      * @param val the value object
58      */

59     public void putState(Object JavaDoc key, Object JavaDoc val) {
60         if (props==null) props = new HashMap();
61         if (logger.isDebugEnabled()) logger.debug("Setting queue state: [key]:"+key+" [val]:"+val);
62         props.put(key,val);
63     }
64     
65     /**
66      * get a property in this StateMap
67      *
68      * @param key the key object
69      * @return the value for the given key
70      */

71     public Object JavaDoc getState(Object JavaDoc key) {
72         if (props==null) return null;
73         else return props.get(key);
74     }
75     
76     /**
77      * remove a property in this StateMap. This function was expanded in csc_101803_2 to
78      * support the notion of wildcarding, allowing you to remove multiple keys in one fell
79      * swoop. Basically, if the key is a String, which ends with an '*', then any keys that
80      * start with that string will be removed (and in this case, the method returns a Map of
81      * key/val pairs that got removed, rather than a single object that got removed). This
82      * approach is not quite as flexible as using regular expressions, but that would make us
83      * dependent on jdk1.4 (so we won't go there for now). Note that this class backs the
84      * ObjectRepository data structures, so this functionality applies there as well.
85      *
86      * @param key the key object
87      * @return the object which was removed
88      */

89     public Object JavaDoc removeState(Object JavaDoc key) {
90 //csc_101803_2_start
91
// if (props==null) return null;
92
// else return props.remove(key);
93

94         //eliminate the obvious
95
if (props==null) return null;
96         
97         //remove all keys that match
98
if (key!=null && (key instanceof String JavaDoc) && ((String JavaDoc) key).endsWith("*")) {
99             Map removed = new HashMap();
100             String JavaDoc keystr = (String JavaDoc) key;
101             String JavaDoc targetstr = keystr.substring(0,keystr.length()-1);
102             List keys = getStateKeys();
103             Iterator it = keys.iterator();
104             while (it.hasNext()) {
105                 Object JavaDoc okey = it.next();
106                 if ((okey instanceof String JavaDoc) && ((String JavaDoc) okey).startsWith(targetstr)) {
107                     Object JavaDoc oval = props.get(okey);
108                     removed.put(okey, oval);
109                     props.remove(okey);
110                 }
111             }
112             return (removed.size()>0 ? removed : null);
113         } else {
114             return props.remove(key);
115         }
116 //csc_101803_2_end
117
}
118     
119     /**
120      * get a List of the keys for this StateMap (implementation
121      * is an ArrayList)
122      *
123      * @return a List the keys for this StateMap
124      */

125     public List getStateKeys() {
126         if (props==null) return null;
127         else return new ArrayList(props.keySet());
128     }
129     
130     /**
131      * get a copy of the underlying Map that holds
132      * the state values
133      *
134      * @return a copy of the underlying state Map
135      */

136     public Map getStateValues() {
137         if (props==null) props = new HashMap();
138         return new HashMap(props);
139 // return props;
140
}
141
142     //csc_052803_2 - added
143
/**
144      * clear all state information
145      */

146     public void clearState() {
147         if (props!=null) props.clear();
148     }
149
150 }
151
Popular Tags