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
28      implements Cloneable, Serializable
29  {
30      // ------------------------------------------------------------
31      // Fields
32      // ------------------------------------------------------------
33      private String label = null;
34  
35      List<Vertex> children = new ArrayList<>();
36  
37      List<Vertex> parents = new ArrayList<>();
38  
39      // ------------------------------------------------------------
40      // Constructors
41      // ------------------------------------------------------------
42  
43      public Vertex( final String label )
44      {
45          this.label = label;
46      }
47  
48      // ------------------------------------------------------------
49      // Accessors
50      // ------------------------------------------------------------
51  
52      public String getLabel()
53      {
54          return label;
55      }
56  
57      public void addEdgeTo( final Vertex vertex )
58      {
59          children.add( vertex );
60      }
61  
62      public void removeEdgeTo( final Vertex vertex )
63      {
64          children.remove( vertex );
65      }
66  
67      public void addEdgeFrom( final Vertex vertex )
68      {
69          parents.add( vertex );
70      }
71  
72      public void removeEdgeFrom( final Vertex vertex )
73      {
74          parents.remove( vertex );
75      }
76  
77      public List<Vertex> getChildren()
78      {
79          return children;
80      }
81  
82      /**
83       * Get the labels used by the most direct children.
84       *
85       * @return the labels used by the most direct children.
86       */
87      public List<String> getChildLabels()
88      {
89          final List<String> retValue = new ArrayList<>( children.size() );
90  
91          for ( Vertex vertex : children )
92          {
93              retValue.add( vertex.getLabel() );
94          }
95          return retValue;
96      }
97  
98      /**
99       * Get the list the most direct ancestors (parents).
100      *
101      * @return list of parents
102      */
103     public List<Vertex> getParents()
104     {
105         return parents;
106     }
107 
108     /**
109      * Get the labels used by the most direct ancestors (parents).
110      *
111      * @return the labels used parents
112      */
113     public List<String> getParentLabels()
114     {
115         final List<String> retValue = new ArrayList<>( parents.size() );
116 
117         for ( Vertex vertex : parents )
118         {
119             retValue.add( vertex.getLabel() );
120         }
121         return retValue;
122     }
123 
124     /**
125      * Indicates if given vertex has no child
126      *
127      * @return <code>true</code> if this vertex has no child, <code>false</code> otherwise
128      */
129     public boolean isLeaf()
130     {
131         return children.size() == 0;
132     }
133 
134     /**
135      * Indicates if given vertex has no parent
136      *
137      * @return <code>true</code> if this vertex has no parent, <code>false</code> otherwise
138      */
139     public boolean isRoot()
140     {
141         return parents.size() == 0;
142     }
143 
144     /**
145      * Indicates if there is at least one edee leading to or from given vertex
146      *
147      * @return <code>true</code> if this vertex is connected with other vertex,<code>false</code> otherwise
148      */
149     public boolean isConnected()
150     {
151         return isRoot() || isLeaf();
152     }
153 
154     @Override
155     public Object clone()
156         throws CloneNotSupportedException
157     {
158         // this is what's failing..
159         final Object retValue = super.clone();
160 
161         return retValue;
162     }
163 
164     @Override
165     public String toString()
166     {
167         return "Vertex{" + "label='" + label + "'" + "}";
168     }
169 
170 }