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