View Javadoc
1   /*
2    *
3    * The DbUnit Database Testing Framework
4    * Copyright (C)2005, DbUnit.org
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2.1 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   *
20   */
21  package org.dbunit.util.search;
22  
23  import java.util.HashSet;
24  import java.util.Set;
25  
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  /**
30   * Super-class for ISearchCallback implementations that needs to filter which
31   * nodes should be included or excluded from the search.<br>
32   * This class implements the <code>searchNode()</code> based on its internal 
33   * mode, which could be <code>ALLOW_MODE</code>, <code>DENY_MODE</code> or 
34   * <code>NO_MODE</code>:
35   * <ul>
36   * <li><code>NO_MODE</code> is the default mode and means <code>searchNode()</code>
37   * always return true</li>
38   * <li><code>ALLOW_MODE</code> is set when <code>setAllowedNodes()</code> is called
39   * and it means <code>searchNode()</code> will return true only if the node is
40   * contained on the Set (or array) passed to <code>setAllowedNodes()</code>
41   * <li><code>DENY_MODE</code> is set when <code>setDeniedNodes()</code> is called
42   * and it means <code>searchNode()</code> will return true only if the node is
43   * not contained on the Set (or array) passed to <code>setDeniedNodes()</code>
44   * </ul>
45   * 
46   * @author Felipe Leme (dbunit@felipeal.net)
47   * @version $Revision$
48   * @since Aug 25, 2005
49   * 
50   */
51  public abstract class AbstractNodesFilterSearchCallback implements
52      ISearchCallback {
53    
54    protected final Logger logger = LoggerFactory.getLogger(getClass());
55  
56    // internal modes
57    protected static final int NO_MODE = 0;
58    protected static final int ALLOW_MODE = 1;
59    protected static final int DENY_MODE = 2;
60    
61    private int filteringMode = NO_MODE;
62    
63    private Set filteredNodes = new HashSet();
64    
65    /**
66     * Default constructor.
67     *
68     */
69    public AbstractNodesFilterSearchCallback() {    
70    }
71  
72    /**
73     * Get which modes are allowed/denied, depending on the operation mode.
74     * @return which modes are allowed/denied, depending on the operation mode.
75     */
76    protected Set getFilteredNodes() {
77      return this.filteredNodes;
78    }
79    
80    /**
81     * Get the operation mode
82     * @return operation mode
83     */
84    protected int getFilteringMode() {
85      return this.filteringMode;
86    }
87    
88    /**
89     * Set which modes are allowed on the search.
90     * @param filteredNodes which modes are allowed on the search.
91     */  
92    protected void setAllowedNodes(Set filteredNodes) {
93          logger.debug("setAllowedNodes(filteredNodes=" + filteredNodes + ") - start");
94  
95      setFilteredNodes(filteredNodes);
96      this.filteringMode = ALLOW_MODE;
97    }
98    
99    /**
100    * Set which modes are allowed on the search.
101    * @param filteredNodes which modes are allowed on the search.
102    */  
103   protected void setAllowedNodes(Object[] filteredNodes) {
104         logger.debug("setAllowedNodes(filteredNodes=" + filteredNodes + ") - start");
105 
106     setFilteredNodes(filteredNodes);
107     this.filteringMode = ALLOW_MODE;
108   }
109   
110   /**
111    * Set which modes are not allowed on the search.
112    * @param filteredNodes which modes are not allowed on the search.
113    */  
114   protected void setDeniedNodes(Set filteredNodes) {
115         logger.debug("setDeniedNodes(filteredNodes=" + filteredNodes + ") - start");
116 
117     setFilteredNodes(filteredNodes);
118     this.filteringMode = DENY_MODE;
119   }
120 
121   /**
122    * Set which modes are not allowed on the search.
123    * @param filteredNodes which modes are not allowed on the search.
124    */  
125   protected void setDeniedNodes(Object[] filteredNodes) {
126         logger.debug("setDeniedNodes(filteredNodes=" + filteredNodes + ") - start");
127 
128     setFilteredNodes(filteredNodes);
129     this.filteringMode = DENY_MODE;
130   }
131 
132   /**
133    * Do nothing...
134    */
135   public void nodeAdded(Object fromNode) throws SearchException {
136     // do nothing
137   }
138   
139   public boolean searchNode(Object node) throws SearchException {
140         logger.debug("searchNode(node=" + node + ") - start");
141 
142     switch( this.filteringMode ) {
143     case ALLOW_MODE:
144       return getFilteredNodes().contains(node); 
145     case DENY_MODE:
146       return !getFilteredNodes().contains(node);
147     default:
148         return true;
149     }
150   }
151   
152   private void setFilteredNodes(Set filteredNodes) {
153         logger.debug("setFilteredNodes(filteredNodes=" + filteredNodes + ") - start");
154 
155     this.filteredNodes = new HashSet(filteredNodes);
156   }
157 
158   private void setFilteredNodes(Object[] filteredNodes) {
159         logger.debug("setFilteredNodes(filteredNodes=" + filteredNodes + ") - start");
160 
161     this.filteredNodes = new HashSet(filteredNodes.length);
162     for (int i = 0; i < filteredNodes.length; i++) {
163       this.filteredNodes.add(filteredNodes[i]);
164     }
165   }
166   
167 }