AbstractNodesFilterSearchCallback.java

  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. import java.util.HashSet;
  23. import java.util.Set;

  24. import org.slf4j.Logger;
  25. import org.slf4j.LoggerFactory;

  26. /**
  27.  * Super-class for ISearchCallback implementations that needs to filter which
  28.  * nodes should be included or excluded from the search.<br>
  29.  * This class implements the <code>searchNode()</code> based on its internal
  30.  * mode, which could be <code>ALLOW_MODE</code>, <code>DENY_MODE</code> or
  31.  * <code>NO_MODE</code>:
  32.  * <ul>
  33.  * <li><code>NO_MODE</code> is the default mode and means <code>searchNode()</code>
  34.  * always return true</li>
  35.  * <li><code>ALLOW_MODE</code> is set when <code>setAllowedNodes()</code> is called
  36.  * and it means <code>searchNode()</code> will return true only if the node is
  37.  * contained on the Set (or array) passed to <code>setAllowedNodes()</code>
  38.  * <li><code>DENY_MODE</code> is set when <code>setDeniedNodes()</code> is called
  39.  * and it means <code>searchNode()</code> will return true only if the node is
  40.  * not contained on the Set (or array) passed to <code>setDeniedNodes()</code>
  41.  * </ul>
  42.  *
  43.  * @author Felipe Leme (dbunit@felipeal.net)
  44.  * @version $Revision$
  45.  * @since Aug 25, 2005
  46.  *
  47.  */
  48. public abstract class AbstractNodesFilterSearchCallback implements
  49.     ISearchCallback {
  50.  
  51.   protected final Logger logger = LoggerFactory.getLogger(getClass());

  52.   // internal modes
  53.   protected static final int NO_MODE = 0;
  54.   protected static final int ALLOW_MODE = 1;
  55.   protected static final int DENY_MODE = 2;
  56.  
  57.   private int filteringMode = NO_MODE;
  58.  
  59.   private Set filteredNodes = new HashSet();
  60.  
  61.   /**
  62.    * Default constructor.
  63.    *
  64.    */
  65.   public AbstractNodesFilterSearchCallback() {    
  66.   }

  67.   /**
  68.    * Get which modes are allowed/denied, depending on the operation mode.
  69.    * @return which modes are allowed/denied, depending on the operation mode.
  70.    */
  71.   protected Set getFilteredNodes() {
  72.     return this.filteredNodes;
  73.   }
  74.  
  75.   /**
  76.    * Get the operation mode
  77.    * @return operation mode
  78.    */
  79.   protected int getFilteringMode() {
  80.     return this.filteringMode;
  81.   }
  82.  
  83.   /**
  84.    * Set which modes are allowed on the search.
  85.    * @param filteredNodes which modes are allowed on the search.
  86.    */  
  87.   protected void setAllowedNodes(Set filteredNodes) {
  88.         logger.debug("setAllowedNodes(filteredNodes=" + filteredNodes + ") - start");

  89.     setFilteredNodes(filteredNodes);
  90.     this.filteringMode = ALLOW_MODE;
  91.   }
  92.  
  93.   /**
  94.    * Set which modes are allowed on the search.
  95.    * @param filteredNodes which modes are allowed on the search.
  96.    */  
  97.   protected void setAllowedNodes(Object[] filteredNodes) {
  98.         logger.debug("setAllowedNodes(filteredNodes=" + filteredNodes + ") - start");

  99.     setFilteredNodes(filteredNodes);
  100.     this.filteringMode = ALLOW_MODE;
  101.   }
  102.  
  103.   /**
  104.    * Set which modes are not allowed on the search.
  105.    * @param filteredNodes which modes are not allowed on the search.
  106.    */  
  107.   protected void setDeniedNodes(Set filteredNodes) {
  108.         logger.debug("setDeniedNodes(filteredNodes=" + filteredNodes + ") - start");

  109.     setFilteredNodes(filteredNodes);
  110.     this.filteringMode = DENY_MODE;
  111.   }

  112.   /**
  113.    * Set which modes are not allowed on the search.
  114.    * @param filteredNodes which modes are not allowed on the search.
  115.    */  
  116.   protected void setDeniedNodes(Object[] filteredNodes) {
  117.         logger.debug("setDeniedNodes(filteredNodes=" + filteredNodes + ") - start");

  118.     setFilteredNodes(filteredNodes);
  119.     this.filteringMode = DENY_MODE;
  120.   }

  121.   /**
  122.    * Do nothing...
  123.    */
  124.   public void nodeAdded(Object fromNode) throws SearchException {
  125.     // do nothing
  126.   }
  127.  
  128.   public boolean searchNode(Object node) throws SearchException {
  129.         logger.debug("searchNode(node=" + node + ") - start");

  130.     switch( this.filteringMode ) {
  131.     case ALLOW_MODE:
  132.       return getFilteredNodes().contains(node);
  133.     case DENY_MODE:
  134.       return !getFilteredNodes().contains(node);
  135.     default:
  136.         return true;
  137.     }
  138.   }
  139.  
  140.   private void setFilteredNodes(Set filteredNodes) {
  141.         logger.debug("setFilteredNodes(filteredNodes=" + filteredNodes + ") - start");

  142.     this.filteredNodes = new HashSet(filteredNodes);
  143.   }

  144.   private void setFilteredNodes(Object[] filteredNodes) {
  145.         logger.debug("setFilteredNodes(filteredNodes=" + filteredNodes + ") - start");

  146.     this.filteredNodes = new HashSet(filteredNodes.length);
  147.     for (int i = 0; i < filteredNodes.length; i++) {
  148.       this.filteredNodes.add(filteredNodes[i]);
  149.     }
  150.   }
  151.  
  152. }