View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2015 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.start.graph;
20  
21  import java.util.ArrayList;
22  import java.util.HashSet;
23  import java.util.LinkedHashSet;
24  import java.util.List;
25  import java.util.Set;
26  
27  /**
28   * Basic Graph Node
29   * @param <T> the node type
30   */
31  public abstract class Node<T>
32  {
33      /** The logical name of this Node */
34      private String logicalName;
35      /** The depth of the Node in the tree */
36      private int depth = 0;
37      /** The set of selections for how this node was selected */
38      private Set<Selection> selections = new LinkedHashSet<>();
39      /** Set of Nodes, by name, that this Node depends on */
40      private List<String> parentNames = new ArrayList<>();
41      /** Set of Nodes, by name, that this Node optionally depend on */
42      private List<String> optionalParentNames = new ArrayList<>();
43  
44      /** The Edges to parent Nodes */
45      private Set<T> parentEdges = new LinkedHashSet<>();
46      /** The Edges to child Nodes */
47      private Set<T> childEdges = new LinkedHashSet<>();
48  
49      public void addChildEdge(T child)
50      {
51          if (childEdges.contains(child))
52          {
53              // already present, skip
54              return;
55          }
56          this.childEdges.add(child);
57      }
58  
59      public void addOptionalParentName(String name)
60      {
61          if (this.optionalParentNames.contains(name))
62          {
63              // skip, name already exists
64              return;
65          }
66          this.optionalParentNames.add(name);
67      }
68  
69      public void addParentEdge(T parent)
70      {
71          if (parentEdges.contains(parent))
72          {
73              // already present, skip
74              return;
75          }
76          this.parentEdges.add(parent);
77      }
78  
79      public void addParentName(String name)
80      {
81          if (this.parentNames.contains(name))
82          {
83              // skip, name already exists
84              return;
85          }
86          this.parentNames.add(name);
87      }
88  
89      public void addSelection(Selection selection)
90      {
91          this.selections.add(selection);
92      }
93  
94      public Set<T> getChildEdges()
95      {
96          return childEdges;
97      }
98  
99      public int getDepth()
100     {
101         return depth;
102     }
103 
104     @Deprecated
105     public String getLogicalName()
106     {
107         return logicalName;
108     }
109 
110     public String getName()
111     {
112         return logicalName;
113     }
114 
115     public List<String> getOptionalParentNames()
116     {
117         return optionalParentNames;
118     }
119 
120     public Set<T> getParentEdges()
121     {
122         return parentEdges;
123     }
124 
125     public List<String> getParentNames()
126     {
127         return parentNames;
128     }
129 
130     public Set<Selection> getSelections()
131     {
132         return selections;
133     }
134 
135     public Set<String> getSelectedCriteriaSet()
136     {
137         Set<String> criteriaSet = new HashSet<>();
138         for (Selection selection : selections)
139         {
140             criteriaSet.add(selection.getCriteria());
141         }
142         return criteriaSet;
143     }
144 
145     public boolean isSelected()
146     {
147         return !selections.isEmpty();
148     }
149 
150     public boolean matches(Predicate predicate)
151     {
152         return predicate.match(this);
153     }
154 
155     public void setDepth(int depth)
156     {
157         this.depth = depth;
158     }
159 
160     public void setName(String name)
161     {
162         this.logicalName = name;
163     }
164 
165     public void setParentNames(List<String> parents)
166     {
167         this.parentNames.clear();
168         this.parentEdges.clear();
169         if (parents != null)
170         {
171             this.parentNames.addAll(parents);
172         }
173     }
174 
175     public void setSelections(Set<Selection> selection)
176     {
177         this.selections = selection;
178     }
179 }