View Javadoc
1   package org.codehaus.plexus.util.dag;
2   
3   /*
4    * Copyright The Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.Serializable;
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  /**
24   * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
25   *
26   */
27  public class Vertex implements Cloneable, Serializable {
28      // ------------------------------------------------------------
29      // Fields
30      // ------------------------------------------------------------
31      private String label = null;
32  
33      List<Vertex> children = new ArrayList<>();
34  
35      List<Vertex> parents = new ArrayList<>();
36  
37      // ------------------------------------------------------------
38      // Constructors
39      // ------------------------------------------------------------
40  
41      public Vertex(final String label) {
42          this.label = label;
43      }
44  
45      // ------------------------------------------------------------
46      // Accessors
47      // ------------------------------------------------------------
48  
49      public String getLabel() {
50          return label;
51      }
52  
53      public void addEdgeTo(final Vertex vertex) {
54          children.add(vertex);
55      }
56  
57      public void removeEdgeTo(final Vertex vertex) {
58          children.remove(vertex);
59      }
60  
61      public void addEdgeFrom(final Vertex vertex) {
62          parents.add(vertex);
63      }
64  
65      public void removeEdgeFrom(final Vertex vertex) {
66          parents.remove(vertex);
67      }
68  
69      public List<Vertex> getChildren() {
70          return children;
71      }
72  
73      /**
74       * Get the labels used by the most direct children.
75       *
76       * @return the labels used by the most direct children.
77       */
78      public List<String> getChildLabels() {
79          final List<String> retValue = new ArrayList<>(children.size());
80  
81          for (Vertex vertex : children) {
82              retValue.add(vertex.getLabel());
83          }
84          return retValue;
85      }
86  
87      /**
88       * Get the list the most direct ancestors (parents).
89       *
90       * @return list of parents
91       */
92      public List<Vertex> getParents() {
93          return parents;
94      }
95  
96      /**
97       * Get the labels used by the most direct ancestors (parents).
98       *
99       * @return the labels used parents
100      */
101     public List<String> getParentLabels() {
102         final List<String> retValue = new ArrayList<>(parents.size());
103 
104         for (Vertex vertex : parents) {
105             retValue.add(vertex.getLabel());
106         }
107         return retValue;
108     }
109 
110     /**
111      * Indicates if given vertex has no child
112      *
113      * @return <code>true</code> if this vertex has no child, <code>false</code> otherwise
114      */
115     public boolean isLeaf() {
116         return children.size() == 0;
117     }
118 
119     /**
120      * Indicates if given vertex has no parent
121      *
122      * @return <code>true</code> if this vertex has no parent, <code>false</code> otherwise
123      */
124     public boolean isRoot() {
125         return parents.size() == 0;
126     }
127 
128     /**
129      * Indicates if there is at least one edee leading to or from given vertex
130      *
131      * @return <code>true</code> if this vertex is connected with other vertex,<code>false</code> otherwise
132      */
133     public boolean isConnected() {
134         return isRoot() || isLeaf();
135     }
136 
137     @Override
138     public Object clone() throws CloneNotSupportedException {
139         // this is what's failing..
140         final Object retValue = super.clone();
141 
142         return retValue;
143     }
144 
145     @Override
146     public String toString() {
147         return "Vertex{" + "label='" + label + "'" + "}";
148     }
149 }