View Javadoc

1   package net.sourceforge.basher.impl;
2   
3   import java.util.*;
4   
5   import net.sourceforge.basher.Task;
6   
7   /**
8    * @author Johan Lindquist
9    */
10  public class TaskTree
11  {
12      private TaskNode _root = new TaskNode("ROOT",Collections.<TaskNode>emptyList());
13      private String _includes;
14      private String _excludes;
15  
16      public TaskTree(final List<TaskContribution> availableTasksWorkingList)
17      {
18          for (TaskContribution taskContribution : availableTasksWorkingList)
19          {
20              addTask(taskContribution);
21          }
22      }
23  
24      public TaskTree()
25      {
26      }
27  
28      public void setExcludes(final String excludes)
29      {
30          _excludes = excludes;
31      }
32  
33      public void setIncludes(final String includes)
34      {
35          _includes = includes;
36      }
37  
38  
39      private class TaskNode
40      {
41          private String _identifier;
42          private List<TaskContribution> _taskContributions;
43          private List<TaskNode> _branches;
44          private List<TaskNode> _parents;
45  
46          private TaskNode(final String identifier, final List<TaskNode> parents)
47          {
48              _identifier = identifier;
49              _parents = parents;
50          }
51  
52          public TaskNode getBranch(final String name)
53          {
54              if (_branches == null)
55              {
56                  return null;
57              }
58  
59              for (TaskNode branch : _branches)
60              {
61                  if (branch._identifier.equals(name))
62                  {
63                      return branch;
64                  }
65              }
66              return null;
67          }
68  
69          public List<TaskContribution> getTaskContributions()
70          {
71              if (_taskContributions == null)
72              {
73                  return Collections.emptyList();
74              }
75              return _taskContributions;
76          }
77  
78          public List<TaskContribution> getAllTasks()
79          {
80              List<TaskContribution> allTasks = new ArrayList<TaskContribution>((_taskContributions == null ? Collections.<TaskContribution>emptyList() : _taskContributions));
81              if (_branches == null)
82              {
83                  return allTasks;
84              }
85              for (TaskNode branch : _branches)
86              {
87                  allTasks.addAll(branch.getAllTasks());
88              }
89              return allTasks;
90          }
91  
92          public void addBranch(final TaskNode taskNode)
93          {
94              if (_branches == null)
95              {
96                  _branches = new ArrayList<TaskNode>();
97              }
98  
99              // Check if the branch exists already
100             for (TaskNode branch : _branches)
101             {
102                 if (branch._identifier.equals(taskNode._identifier))
103                 {
104                     return;
105                 }
106             }
107             _branches.add(taskNode);
108         }
109 
110         public void addTask(final TaskContribution taskContribution)
111         {
112             if (_taskContributions == null)
113             {
114                 _taskContributions = new ArrayList<TaskContribution>();
115             }
116             _taskContributions.add(taskContribution);
117         }
118     }
119 
120     /**
121      * Retrieves tasks based on the globally set include/exclude patterns.
122      *
123      * @return The filtered tasks
124      */
125     public List<TaskContribution> getTasks()
126     {
127         final Set<TaskContribution> includeTasks = new HashSet<TaskContribution>();
128 
129         if (_includes == null)
130         {
131             includeTasks.addAll(getTasks("*"));
132         }
133         else
134         {
135             String[] includePatterns = _includes.split(",");
136             if (includePatterns == null || includePatterns.length == 0)
137             {
138                 includeTasks.addAll(getTasks("*"));
139             }
140             else
141             {
142                 for (String includePattern : includePatterns)
143                 {
144                     includeTasks.addAll(getTasks(includePattern));
145                 }
146             }
147         }
148 
149         final List<TaskContribution> excludeTasks = new ArrayList<TaskContribution>();
150         if (_excludes != null)
151         {
152             String[] excludePatterns = _excludes.split(",");
153             if (excludePatterns != null && excludePatterns.length > 0)
154             {
155                 for (String excludePattern : excludePatterns)
156                 {
157                     excludeTasks.addAll(getTasks(excludePattern));
158                 }
159             }
160         }
161         includeTasks.removeAll(excludeTasks);
162 
163         return new ArrayList<TaskContribution>(includeTasks);
164     }
165 
166     public List<TaskContribution> getTasks(final String pattern)
167     {
168         // Pattern is something like
169         // net.sourceforge.basher.**.*
170         //                        ^  ^
171         //                        |  '- Wildcard for a set of tasks within the package
172         //                        |
173         //                        '- Wildcard for a set of packages
174 
175         String[] splitBranches = pattern.split("\\.");
176 
177         final String lastPartTaskName = splitBranches[splitBranches.length-1];
178 
179         TaskNode taskNode = _root;
180         for (int i = 0; i < splitBranches.length - 1; i++)
181         {
182             String branch = splitBranches[i];
183 
184             // If pattern == wildcard (**) this should evaluate all
185             // branches at this level
186             if (branch.equals("**"))
187             {
188                 // hmmm
189             }
190 
191 
192             final TaskNode nextTaskNode = taskNode.getBranch(branch);
193             if (nextTaskNode == null)
194             {
195                 // No more splitBranches - return tasks in this node
196                 return filterTasks(lastPartTaskName, taskNode.getTaskContributions());
197             }
198             else
199             {
200                 taskNode = nextTaskNode;
201             }
202 
203         }
204         return taskNode.getAllTasks();
205 
206 
207     }
208 
209     private List<TaskContribution> filterTasks(final String lastPartTaskName, final List<TaskContribution> tasks)
210     {
211        /* List<Task> filteredTasks = new ArrayList<Task>();
212         for (Task task : tasks)
213         {
214             final String[] strings = task.getName().split(".");
215             final String lastPart = strings[strings.length-1];
216 
217             final Pattern pattern = Pattern.compile(lastPartTaskName);
218             if (pattern.matcher(lastPart).matches())
219             {
220                 filteredTasks.add(task);
221             }
222 
223         }
224 
225         return filteredTasks;*/
226         return tasks;
227     }
228 
229     public void addTask(TaskContribution taskContribution)
230     {
231         String taskName = taskContribution.getTaskName();
232         String[] branches = taskName.split("\\.");
233 
234         TaskNode taskNode = _root;
235 
236         List<TaskNode> parents = new ArrayList<TaskNode>();
237         parents.add(_root);
238         for (int i = 0; i < branches.length - 1; i++)
239         {
240             String branch = branches[i];
241             TaskNode node = taskNode.getBranch(branch);
242             if (node == null)
243             {
244                 final TaskNode newNode = new TaskNode(branch, new ArrayList<TaskNode>(parents));
245                 taskNode.addBranch(newNode);
246                 node = newNode;
247             }
248             parents.add(0,node);
249             taskNode = node;
250         }
251         
252         taskNode.addTask(taskContribution);
253 
254     }
255 
256 }