Coverage Report - com.jeantessier.dependency.DeletingVisitor
 
Classes in this File Line Coverage Branch Coverage Complexity
DeletingVisitor
80%
41/51
92%
24/26
1.65
 
 1  
 /*
 2  
  *  Copyright (c) 2001-2009, Jean Tessier
 3  
  *  All rights reserved.
 4  
  *  
 5  
  *  Redistribution and use in source and binary forms, with or without
 6  
  *  modification, are permitted provided that the following conditions
 7  
  *  are met:
 8  
  *  
 9  
  *      * Redistributions of source code must retain the above copyright
 10  
  *        notice, this list of conditions and the following disclaimer.
 11  
  *  
 12  
  *      * Redistributions in binary form must reproduce the above copyright
 13  
  *        notice, this list of conditions and the following disclaimer in the
 14  
  *        documentation and/or other materials provided with the distribution.
 15  
  *  
 16  
  *      * Neither the name of Jean Tessier nor the names of his contributors
 17  
  *        may be used to endorse or promote products derived from this software
 18  
  *        without specific prior written permission.
 19  
  *  
 20  
  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 21  
  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 22  
  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 23  
  *  A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR
 24  
  *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 25  
  *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 26  
  *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 27  
  *  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 28  
  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 29  
  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 30  
  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 31  
  */
 32  
 
 33  
 package com.jeantessier.dependency;
 34  
 
 35  
 import java.util.*;
 36  
 
 37  
 import org.apache.log4j.*;
 38  
 
 39  
 import com.jeantessier.classreader.*;
 40  
 
 41  
 public class DeletingVisitor implements Visitor, RemoveVisitor {
 42  
     private NodeFactory factory;
 43  
     
 44  38
     public DeletingVisitor(NodeFactory factory) {
 45  38
         this.factory = factory;
 46  38
     }
 47  
 
 48  
     public NodeFactory getFactory() {
 49  1
         return factory;
 50  
     }
 51  
     
 52  
     public void traverseNodes(Collection<? extends Node> nodes) {
 53  0
         throw new UnsupportedOperationException("not implemented yet.");
 54  
     }
 55  
 
 56  
     public void traverseInbound(Collection<? extends Node> nodes) {
 57  0
         throw new UnsupportedOperationException("not implemented yet.");
 58  
     }
 59  
 
 60  
     public void traverseOutbound(Collection<? extends Node> nodes) {
 61  0
         throw new UnsupportedOperationException("not implemented yet.");
 62  
     }
 63  
 /*
 64  
      *  Regular visits are used to completely delete sections
 65  
      */
 66  
     
 67  
     public void visitPackageNode(PackageNode node) {
 68  6
         Logger.getLogger(getClass()).debug("visitPackageNode(" + node + ")");
 69  
 
 70  6
         for (ClassNode classNode : new ArrayList<ClassNode>(node.getClasses())) {
 71  1
             classNode.accept(this);
 72  
         }
 73  
 
 74  6
         visitNode(node);
 75  6
     }
 76  
 
 77  
     public void visitClassNode(ClassNode node) {
 78  11
         Logger.getLogger(getClass()).debug("visitClassNode(" + node + ")");
 79  
 
 80  11
         for (FeatureNode featureNode : new ArrayList<FeatureNode>(node.getFeatures())) {
 81  4
             featureNode.accept(this);
 82  
         }
 83  
 
 84  11
         visitNode(node);
 85  11
     }
 86  
 
 87  
     public void visitFeatureNode(FeatureNode node) {
 88  10
         Logger.getLogger(getClass()).debug("visitFeatureNode(" + node + ")");
 89  
         
 90  10
         visitNode(node);
 91  10
     }
 92  
 
 93  
     private void visitNode(Node node) {
 94  27
         node.setConfirmed(false);
 95  
 
 96  27
         for (Node outbound : new ArrayList<Node>(node.getOutboundDependencies())) {
 97  8
             node.removeDependency(outbound);
 98  8
             outbound.acceptOutbound(this);
 99  
         }
 100  
         
 101  27
         node.acceptOutbound(this);
 102  27
     }
 103  
 
 104  
     /*
 105  
      *  Outbound visits are used to clean up sections
 106  
      */
 107  
     
 108  
     public void visitOutboundPackageNode(PackageNode node) {
 109  46
         Logger.getLogger(getClass()).debug("visitOutboundPackageNode(" + node + ")");
 110  
         
 111  46
         if (canDeletePackage(node)) {
 112  18
             factory.deletePackage(node);
 113  
         }
 114  46
     }
 115  
     
 116  
     public void visitOutboundClassNode(ClassNode node) {
 117  34
         Logger.getLogger(getClass()).debug("visitOutboundClassNode(" + node + ")");
 118  
         
 119  34
         if (canDeleteClass(node)) {
 120  14
             factory.deleteClass(node);
 121  
         }
 122  
 
 123  34
         node.getPackageNode().acceptOutbound(this);
 124  34
     }
 125  
     
 126  
     public void visitOutboundFeatureNode(FeatureNode node) {
 127  17
         Logger.getLogger(getClass()).debug("visitOutboundFeatureNode(" + node + ")");
 128  
         
 129  17
         if (canDeleteFeature(node)) {
 130  11
             factory.deleteFeature(node);
 131  
         }
 132  
 
 133  17
         node.getClassNode().acceptOutbound(this);
 134  17
     }
 135  
 
 136  
     private boolean canDelete(Node node) {
 137  97
         return !node.isConfirmed() && node.getInboundDependencies().isEmpty();
 138  
     }
 139  
 
 140  
     private boolean canDeletePackage(PackageNode node) {
 141  46
         return canDelete(node) && node.getClasses().isEmpty();
 142  
     }
 143  
 
 144  
     private boolean canDeleteClass(ClassNode node) {
 145  34
         return canDelete(node) && node.getFeatures().isEmpty();
 146  
     }
 147  
 
 148  
     private boolean canDeleteFeature(FeatureNode node) {
 149  17
         return canDelete(node);
 150  
     }
 151  
 
 152  
     /*
 153  
      *  Inbound visits are not used
 154  
      */
 155  
     
 156  
     public void visitInboundPackageNode(PackageNode node) {
 157  
         // Do nothing
 158  0
     }
 159  
     
 160  
     public void visitInboundClassNode(ClassNode node) {
 161  
         // Do nothing
 162  0
     }
 163  
     
 164  
     public void visitInboundFeatureNode(FeatureNode node) {
 165  
         // Do nothing
 166  0
     }
 167  
 
 168  
     /*
 169  
      * RemoveVisitor methods
 170  
      */
 171  
     
 172  
     public void removeClass(String classname) {
 173  0
         Node node = factory.getClasses().get(classname);
 174  0
         if (node != null) {
 175  0
             node.accept(this);
 176  
         }
 177  0
     }
 178  
 }