Coverage Report - org.apache.myfaces.shared_impl.util.el.ActionsMap
 
Classes in this File Line Coverage Branch Coverage Complexity
ActionsMap
0%
0/37
0%
0/16
1.474
ActionsMap$CommandEntry
0%
0/10
0%
0/4
1.474
 
 1  
 /*
 2  
  *  Licensed to the Apache Software Foundation (ASF) under one
 3  
  *  or more contributor license agreements.  See the NOTICE file
 4  
  *  distributed with this work for additional information
 5  
  *  regarding copyright ownership.  The ASF licenses this file
 6  
  *  to you under the Apache License, Version 2.0 (the
 7  
  *  "License"); you may not use this file except in compliance
 8  
  *  with the License.  You may obtain a copy of the License at
 9  
  * 
 10  
  *  http://www.apache.org/licenses/LICENSE-2.0
 11  
  * 
 12  
  *  Unless required by applicable law or agreed to in writing,
 13  
  *  software distributed under the License is distributed on an
 14  
  *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  *  KIND, either express or implied.  See the License for the
 16  
  *  specific language governing permissions and limitations
 17  
  *  under the License.
 18  
  */
 19  
 package org.apache.myfaces.shared_impl.util.el;
 20  
 
 21  
 import java.util.*;
 22  
 
 23  
 /**
 24  
  * @author Sylvain Vieujot (latest modification by $Author: skitching $)
 25  
  * @version $Revision: 355303 $ $Date: 2005-12-09 02:36:08 +0100 (Fr, 09 Dez 2005) $
 26  
  *
 27  
  * You can use this class to trigger an action when a boolean is set to true.
 28  
  *
 29  
  * Example : in JSF pages, for dataTable, to remove elements :
 30  
  * Backing bean (#{inboxFace}).
 31  
  * public ActionsMap getRemoveEmailUnid(){
 32  
  *         return new ActionsMap(){
 33  
  *            public void performAction(String unid) {
 34  
  *                InboxMailDAO<TInboxMail> dao = getInboxMailDAO();
 35  
  *                TInboxMail email = dao.getByPrimaryKey( unid );
 36  
  *                dao.remove( email );
 37  
  *            }
 38  
  *        };
 39  
  *    }
 40  
  * JSF page :
 41  
  * &lt;h:selectBooleanCheckbox value="#{inboxFace.removeEmailUnid[email.unid]}"/&gt;
 42  
  */
 43  
 public abstract class ActionsMap implements Map {
 44  
 
 45  
     private Set keys;
 46  
 
 47  0
     public ActionsMap(){
 48  
         // NoOp
 49  0
     }
 50  
 
 51  0
     public ActionsMap(Set keys){
 52  0
         this.keys = keys;
 53  0
     }
 54  
 
 55  
     /**
 56  
      * This method should fire the command.
 57  
      */
 58  
     public abstract void performAction(String command);
 59  
 
 60  
     public int size() {
 61  0
         return keys.size();
 62  
     }
 63  
 
 64  
     public boolean isEmpty() {
 65  0
         return keys.isEmpty();
 66  
     }
 67  
 
 68  
     public boolean containsKey(Object key) {
 69  0
         return keys.contains( key );
 70  
     }
 71  
 
 72  
     public boolean containsValue(Object value) {
 73  0
         if( ! (value instanceof Boolean) )
 74  0
             return false;
 75  0
         return ((Boolean)value).booleanValue();
 76  
     }
 77  
 
 78  
     public Object get( Object key) {
 79  0
         return Boolean.FALSE;
 80  
     }
 81  
 
 82  
     public Boolean put(String key, Boolean value) {
 83  0
         if( value!=null && value.booleanValue() )
 84  0
             performAction( key );
 85  0
         return Boolean.FALSE;
 86  
     }
 87  
 
 88  
     public Object remove(Object key) {
 89  0
         if( keys.remove( key ) )
 90  0
             return Boolean.FALSE;
 91  0
         return null;
 92  
     }
 93  
 
 94  
     public void putAll(Map map) {
 95  0
         Iterator it = map.entrySet().iterator();
 96  
 
 97  0
         while (it.hasNext())
 98  
         {
 99  0
             Entry entry = (Entry) it.next();
 100  0
             Object obj = entry.getValue();
 101  0
             if( (obj instanceof Boolean) && ((Boolean) obj).booleanValue() )
 102  0
                 performAction( (String) entry.getKey() );
 103  0
         }
 104  0
     }
 105  
 
 106  
     public void clear() {
 107  0
         keys.clear();
 108  0
     }
 109  
 
 110  
     public Set keySet() {
 111  0
         return keys;
 112  
     }
 113  
 
 114  
     public Collection values() {
 115  0
         return Collections.nCopies(keys.size(), Boolean.FALSE);
 116  
     }
 117  
 
 118  
     public Set entrySet() {
 119  0
         Set set = new HashSet( keys.size() );
 120  
 
 121  0
         Iterator it = keys.iterator();
 122  
 
 123  0
         while (it.hasNext())
 124  
         {
 125  0
             String command = (String) it.next();
 126  0
             set.add( new CommandEntry(command) );
 127  0
         }
 128  
 
 129  0
         return set;
 130  
     }
 131  
 
 132  
     private class CommandEntry implements Entry{
 133  
 
 134  
         private final String command;
 135  0
         private boolean commandPerformed = false;
 136  
 
 137  0
         public CommandEntry(String command){
 138  0
             this.command = command;
 139  0
         }
 140  
 
 141  
         public Object getKey() {
 142  0
             return command;
 143  
         }
 144  
 
 145  
         public Object getValue() {
 146  0
             return Boolean.valueOf(commandPerformed);
 147  
         }
 148  
 
 149  
         public Object setValue(Object performCommand) {
 150  0
             if( (performCommand instanceof Boolean) && ((Boolean)performCommand).booleanValue() ){
 151  0
                 performAction( command );
 152  0
                 commandPerformed = true;
 153  
             }
 154  0
             return Boolean.valueOf(commandPerformed);
 155  
         }
 156  
     }
 157  
 }
 158