com.bristle.javalib.bean
Class MapBean

java.lang.Object
  extended by com.bristle.javalib.bean.MapBean
All Implemented Interfaces:
Map
Direct Known Subclasses:
SecurableObjectMapBean

public class MapBean
extends Object
implements Map

This class encapsulates a Map in a Java Bean so that it can accessed as a bean from JSP/JSTL EL expressions, and in a Collection so that it can be iterated by the JSTL tag. All this requires is some additional Bean-style method names that invoke existing non-Bean-style methods. The Collection methods operate on the collection of entries in the map.

 Note:  Can't get it to work.  For some reason, all the lines I could think
        to try like:
           
           <jsp:useBean id="userIdAccessAttrMapBean" 
                        class="com.infratrac.itf.bean.UserIdAccessAttrMapBean" 
                        scope='page'
           <c:forEach items="${userIdAccessAttrMapBean.values}" var="val">
           <c:forEach items="${userIdAccessAttrMapBean.iterator}" var="entry">
           <c:forEach items="${userIdAccessAttrMapBean.entrySet.iterator}" var="entry">
           
        iterate zero times.  What am I doing wrong?

 Note:  May not be necessary anyhow, since forEach is supposed to iterate
        the entries of Maps even without this class, according to:
           http://www.theserverside.com/discussions/thread.tss?thread_id=35477
        and:
           http://www.ibm.com/developerworks/java/library/j-jstl0318/

        So, why is it not working for me?  When I iterate over the same 
        object, in the same page with the following Java code instead, it
        works fine:
           
           <%
           for (Iterator i = userIdAccessAttrMapBean.getIterator(); i.hasNext();)
           {
               Map.Entry      entry = (Map.Entry) i.next();
               String         key   = (String)entry.getKey();
               AccessAttrBean attr  = (AccessAttrBean)entry.getValue();
               String         role  = attr.getName();
               logutil.log("JSP Username = " + key);
               logutil.log("JSP Role     = " + role);
           }
           %>
           

Usage:

   - The typical scenario for using this class is:
           
           <jsp:useBean id='myMapBean' 
                        class='com.bristle.javalib.bean.MapBean' 
           />
           <% myMapBean.setMap(myMap); // No way to do this line in JSP? %>
           <c:forEach items="${myMapBean.entrySet}" var="entry">
             <tr>
               <td>${entry.key.name}</td>
               <td>${entry.value.name}</td>
             </tr>
           </c:forEach>
           <c:forEach items="${myMapBean.iterator}" var="entry">
             <tr>
               <td>${entry.key.name}</td>
               <td>${entry.value.name}</td>
             </tr>
           </c:forEach>
           <c:forEach items="${myMapBean.keySet}" var="key">
             <tr><td>${key.name}</td></tr>
           </c:forEach>
           <c:forEach items="${myMapBean.values}" var="value">
             <tr><td>${value.name}</td></tr>
           </c:forEach>
           ${myMapBean.containsKey}
           ${myMapBean.containsValue}
           ${myMapBean.hashCode}
           ${myMapBean.size}
           ${myMapBean.toString}
           ${myMapBean.empty}
           ${myMapBean.toArray}
           ${myMapBean.map}
           

Assumptions:
Effects:
       - None.
Anticipated Changes:
Notes:
Implementation Notes:
       - Cannot implement both interfaces Map and Collection because they 
         have incompatible versions of remove(Object).  See the comments
         at remove(Object) below for details.
Portability Issues:
Revision History:
   $Log$


Nested Class Summary
 
Nested classes/interfaces inherited from interface java.util.Map
Map.Entry
 
Field Summary
private  Map m_map
           
private static long serialVersionUID
          This number identifies the version of the class definition, used for serialized instances.
 
Constructor Summary
MapBean()
          Default Constructor.
MapBean(Map map)
          Constructor.
 
Method Summary
 boolean add(Object arg0)
          Calls Set.add(Object) on the Set returned by Map.entrySet()
 boolean addAll(Collection arg0)
          Calls Set.addAll(Collection) on the Set returned by Map.entrySet()
 void clear()
          Calls Map.clear()
 boolean contains(Object arg0)
          Calls Set.contains(Object) on the Set returned by Map.entrySet()
 boolean containsAll(Collection arg0)
          Calls Set.containsAll(Collection) on the Set returned by Map.entrySet()
 boolean containsKey(Object arg0)
          Calls Map.containsKey(Object)
 boolean containsValue(Object arg0)
          Calls Map.containsValue(Object)
 Set entrySet()
          Calls Map.entrySet()
 boolean equals(Object obj)
          Calls Map.equals(Object)
 Object get(Object key)
          Calls Map.get(Object)
 boolean getContainsKey(Object arg0)
          Calls containsKey(Object)
 boolean getContainsValue(Object arg0)
          Calls containsValue(Object)
 Set getEntrySet()
          Calls entrySet()
 int getHashCode()
          Calls hashCode()
 Iterator getIterator()
          Calls iterator()
 Set getKeySet()
          Calls keySet()
 Map getMap()
          Return the encapsulated Map.
 int getSize()
          Calls size()
 Object[] getToArray()
          Calls toArray()
 String getToString()
          Calls toString()
 Collection getValues()
          Calls values()
 int hashCode()
          Calls Map.hashCode()
 boolean isEmpty()
          Calls Map.isEmpty()
 Iterator iterator()
          Calls Set.iterator() on the Set returned by Map.entrySet()
 Set keySet()
          Calls Map.keySet()
 Object put(Object arg0, Object arg1)
          Calls Map.put(Object, Object)
 void putAll(Map arg0)
          Calls Map.putAll(Map)
 Object remove(Object arg0)
          Calls Set.remove(Object) on the Set returned by Map.entrySet()
 boolean removeAll(Collection arg0)
          Calls Set.removeAll(Collection) on the Set returned by Map.entrySet()
 boolean retainAll(Collection arg0)
          Calls Set.retainAll(Collection) on the Set returned by Map.entrySet()
 void setMap(Map map)
          Set the encapsulated Map.
 int size()
          Calls Map.size()
 Object[] toArray()
          Calls Set.toArray() on the Set returned by Map.entrySet()
 Object[] toArray(Object[] arg0)
          Calls Set.toArray(Object[]) on the Set returned by Map.entrySet()
 String toString()
          Calls AbstractMap.toString()
 Collection values()
          Calls Map.values()
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

m_map

private Map m_map

serialVersionUID

private static final long serialVersionUID
This number identifies the version of the class definition, used for serialized instances. Be sure to increment it when adding/modifying instance variable definitions or making any other change to the class definition. Omitting this declaration causes a compiler warning for any class that implements java.io.Serializable.

See Also:
Constant Field Values
Constructor Detail

MapBean

public MapBean()
Default Constructor. Note: This constructor with no params is required for the class to be a Java Bean, so that it can be accessed via <jsp:useBean>. With no params, this constructor cannot initialize the wrapped Map. Therefore, m_map can't be a "blank final", so we initialize it to a Map to support all the methods that use it, Also, we have a setter for it, so the caller can pass in an entire map at once, like they can do with the non-default constructor. We have a getter also for completeness, even though it is really not necessary since this class itself is a Map that could always be used by any caller in place of the encapsulated Map.


MapBean

public MapBean(Map map)
Constructor.

Parameters:
map - The Map to encapsulate.
Method Detail

getMap

public Map getMap()
Return the encapsulated Map.

Returns:
The encapsulated Map.

setMap

public void setMap(Map map)
Set the encapsulated Map.

Parameters:
map - The Map to encapsulate.

equals

public boolean equals(Object obj)
Calls Map.equals(Object)

Specified by:
equals in interface Map
Overrides:
equals in class Object

hashCode

public int hashCode()
Calls Map.hashCode()

Specified by:
hashCode in interface Map
Overrides:
hashCode in class Object

toString

public String toString()
Calls AbstractMap.toString()

Overrides:
toString in class Object

clear

public void clear()
Calls Map.clear()

Specified by:
clear in interface Map

isEmpty

public boolean isEmpty()
Calls Map.isEmpty()

Specified by:
isEmpty in interface Map

size

public int size()
Calls Map.size()

Specified by:
size in interface Map

containsKey

public boolean containsKey(Object arg0)
Calls Map.containsKey(Object)

Specified by:
containsKey in interface Map

containsValue

public boolean containsValue(Object arg0)
Calls Map.containsValue(Object)

Specified by:
containsValue in interface Map

entrySet

public Set entrySet()
Calls Map.entrySet()

Specified by:
entrySet in interface Map

get

public Object get(Object key)
Calls Map.get(Object)

Specified by:
get in interface Map

keySet

public Set keySet()
Calls Map.keySet()

Specified by:
keySet in interface Map

put

public Object put(Object arg0,
                  Object arg1)
Calls Map.put(Object, Object)

Specified by:
put in interface Map

putAll

public void putAll(Map arg0)
Calls Map.putAll(Map)

Specified by:
putAll in interface Map

values

public Collection values()
Calls Map.values()

Specified by:
values in interface Map

add

public boolean add(Object arg0)
Calls Set.add(Object) on the Set returned by Map.entrySet()


addAll

public boolean addAll(Collection arg0)
Calls Set.addAll(Collection) on the Set returned by Map.entrySet()


contains

public boolean contains(Object arg0)
Calls Set.contains(Object) on the Set returned by Map.entrySet()


containsAll

public boolean containsAll(Collection arg0)
Calls Set.containsAll(Collection) on the Set returned by Map.entrySet()


iterator

public Iterator iterator()
Calls Set.iterator() on the Set returned by Map.entrySet()


remove

public Object remove(Object arg0)
Calls Set.remove(Object) on the Set returned by Map.entrySet()

Specified by:
remove in interface Map

removeAll

public boolean removeAll(Collection arg0)
Calls Set.removeAll(Collection) on the Set returned by Map.entrySet()


retainAll

public boolean retainAll(Collection arg0)
Calls Set.retainAll(Collection) on the Set returned by Map.entrySet()


toArray

public Object[] toArray()
Calls Set.toArray() on the Set returned by Map.entrySet()


toArray

public Object[] toArray(Object[] arg0)
Calls Set.toArray(Object[]) on the Set returned by Map.entrySet()


getHashCode

public int getHashCode()
Calls hashCode()


getToString

public String getToString()
Calls toString()


getSize

public int getSize()
Calls size()


getContainsKey

public boolean getContainsKey(Object arg0)
Calls containsKey(Object)


getContainsValue

public boolean getContainsValue(Object arg0)
Calls containsValue(Object)


getEntrySet

public Set getEntrySet()
Calls entrySet()


getKeySet

public Set getKeySet()
Calls keySet()


getValues

public Collection getValues()
Calls values()


getIterator

public Iterator getIterator()
Calls iterator()


getToArray

public Object[] getToArray()
Calls toArray()