NodeFactory.java

  1. /*
  2.  *  Copyright (c) 2001-2024, 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. package com.jeantessier.dependency;

  33. import java.util.*;

  34. import org.apache.logging.log4j.*;
  35. import org.apache.oro.text.perl.*;

  36. public class NodeFactory {
  37.     private static final Perl5Util perl = new Perl5Util();

  38.     private final Map<String, PackageNode> packages = new HashMap<>();
  39.     private final Map<String, ClassNode> classes = new HashMap<>();
  40.     private final Map<String, FeatureNode> features = new HashMap<>();

  41.     public PackageNode createPackage(String packageName) {
  42.         return createPackage(packageName, false);
  43.     }
  44.    
  45.     public PackageNode createPackage(String packageName, boolean confirmed) {
  46.         LogManager.getLogger(getClass()).debug("Create package \"{}\"", packageName);

  47.         PackageNode result = packages.get(packageName);

  48.         if (result == null) {
  49.             result = new PackageNode(packageName, confirmed);
  50.             packages.put(packageName, result);
  51.             LogManager.getLogger(getClass()).debug("Added package \"{}\"", packageName);
  52.         }

  53.         if (confirmed && !result.isConfirmed()) {
  54.             result.setConfirmed(confirmed);
  55.             LogManager.getLogger(getClass()).debug("Package \"{}\" is confirmed", packageName);
  56.         }

  57.         return result;
  58.     }
  59.    
  60.     // Only to be used by DeletingVisitor
  61.     void deletePackage(PackageNode node) {
  62.         LogManager.getLogger(getClass()).debug("Delete package \"{}\"", node);

  63.         packages.remove(node.getName());
  64.     }

  65.     public Map<String, PackageNode> getPackages() {
  66.         return Collections.unmodifiableMap(packages);
  67.     }

  68.     public ClassNode createClass(String className) {
  69.         return createClass(className, false);
  70.     }
  71.    
  72.     public ClassNode createClass(String className, boolean confirmed) {
  73.         LogManager.getLogger(getClass()).debug("Create class \"{}\"", className);

  74.         ClassNode result = classes.get(className);

  75.         if (result == null) {
  76.             String packageName = "";
  77.             int pos = className.lastIndexOf('.');
  78.             if (pos != -1) {
  79.                 packageName = className.substring(0, pos);
  80.             }
  81.             PackageNode parent = createPackage(packageName, confirmed);
  82.             result = new ClassNode(parent, className, confirmed);
  83.             parent.addClass(result);
  84.             classes.put(className, result);
  85.             LogManager.getLogger(getClass()).debug("Added class \"{}\"", className);
  86.         }

  87.         if (confirmed && !result.isConfirmed()) {
  88.             result.setConfirmed(confirmed);
  89.             LogManager.getLogger(getClass()).debug("Class \"{}\" is confirmed", className);
  90.         }

  91.         return result;
  92.     }

  93.     // Only to be used by DeletingVisitor
  94.     void deleteClass(ClassNode node) {
  95.         LogManager.getLogger(getClass()).debug("Delete class \"{}\"", node);

  96.         node.getPackageNode().removeClass(node);
  97.         classes.remove(node.getName());
  98.     }

  99.     public Map<String, ClassNode> getClasses() {
  100.         return Collections.unmodifiableMap(classes);
  101.     }

  102.     public FeatureNode createFeature(String featureName) {
  103.         return createFeature(featureName, false);
  104.     }
  105.    
  106.     public FeatureNode createFeature(String featureName, boolean confirmed) {
  107.         LogManager.getLogger(getClass()).debug("Create feature \"{}\"", featureName);

  108.         FeatureNode result = features.get(featureName);

  109.         if (result == null) {
  110.             String parentName;

  111.             if (perl.match("/^(.*)\\.[^\\.]*\\(.*\\)(: \\S.*)?$/", featureName)) {
  112.                 parentName = perl.group(1);
  113.             } else if (perl.match("/^(.*)\\.[^\\.]*$/", featureName)) {
  114.                 parentName = perl.group(1);
  115.             } else {
  116.                 parentName = "";
  117.             }

  118.             ClassNode parent = createClass(parentName, confirmed);
  119.             result = new FeatureNode(parent, featureName, confirmed);
  120.             parent.addFeature(result);
  121.             features.put(featureName, result);
  122.             LogManager.getLogger(getClass()).debug("Added feature \"{}\"", featureName);
  123.         }

  124.         if (confirmed && !result.isConfirmed()) {
  125.             result.setConfirmed(confirmed);
  126.             LogManager.getLogger(getClass()).debug("Feature \"{}\" is confirmed", featureName);
  127.         }

  128.         return result;
  129.     }
  130.    
  131.     // Only to be used by DeletingVisitor
  132.     void deleteFeature(FeatureNode node) {
  133.         LogManager.getLogger(getClass()).debug("Delete feature \"{}\"", node);

  134.         node.getClassNode().removeFeature(node);
  135.         features.remove(node.getName());
  136.     }
  137.    
  138.     public Map<String, FeatureNode> getFeatures() {
  139.         return Collections.unmodifiableMap(features);
  140.     }
  141. }