View Javadoc

1   /*
2    * Grapht, an open source dependency injector.
3    * Copyright 2014-2015 various contributors (see CONTRIBUTORS.txt)
4    * Copyright 2010-2014 Regents of the University of Minnesota
5    *
6    * This program is free software; you can redistribute it and/or modify
7    * it under the terms of the GNU Lesser General Public License as
8    * published by the Free Software Foundation; either version 2.1 of the
9    * License, or (at your option) any later version.
10   *
11   * This program is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13   * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14   * details.
15   *
16   * You should have received a copy of the GNU General Public License along with
17   * this program; if not, write to the Free Software Foundation, Inc., 51
18   * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19   */
20  package org.grouplens.grapht.graph;
21  
22  import com.google.common.base.Preconditions;
23  import com.google.common.collect.Sets;
24  import org.apache.commons.lang3.tuple.Pair;
25  
26  import javax.annotation.Nonnull;
27  import java.util.Set;
28  
29  /**
30   * A builder for {@linkplain DAGNode DAG nodes}.  You can create one with {@link DAGNode#newBuilder()}
31   * or {@link DAGNode#newBuilder(Object)}.
32   *
33   * @since 0.7.0
34   * @author <a href="http://www.grouplens.org">GroupLens Research</a>
35   */
36  public class DAGNodeBuilder<V,E> {
37      private V label;
38      private Set<Pair<DAGNode<V,E>,E>> edges;
39  
40      public DAGNodeBuilder() {
41          this(null);
42      }
43  
44      public DAGNodeBuilder(V lbl) {
45          label = lbl;
46          edges = Sets.newHashSet();
47      }
48  
49      /**
50       * Set the node's label.
51       * @param lbl The node's label.
52       * @return The builder (for chaining).
53       */
54      @Nonnull
55      public DAGNodeBuilder<V,E> setLabel(@Nonnull V lbl) {
56          Preconditions.checkNotNull(lbl, "node label");
57          label = lbl;
58          return this;
59      }
60  
61      /**
62       * Get the label set for this node.
63       * @return The label currently set for the node builder.
64       */
65      public V getLabel() {
66          return label;
67      }
68  
69      /**
70       * Add an edge.
71       * @param target The target node.
72       * @param label The edge label.
73       * @return The builder (for chaining).
74       */
75      @Nonnull
76      public DAGNodeBuilder<V,E> addEdge(@Nonnull DAGNode<V,E> target,
77                                         @Nonnull E label) {
78          Preconditions.checkNotNull(target, "edge target");
79          Preconditions.checkNotNull(label, "edge label");
80          return addEdge(Pair.of(target, label));
81      }
82  
83      /**
84       * Add an edge.
85       * @param edge The target node and label for the edge.
86       * @return The builder (for chaining).
87       */
88      @Nonnull
89      public DAGNodeBuilder<V,E> addEdge(Pair<DAGNode<V,E>,E> edge) {
90          Preconditions.checkNotNull(edge, "edge");
91          Preconditions.checkNotNull(edge.getLeft(), "edge target");
92          Preconditions.checkNotNull(edge.getRight(), "edge label");
93          edges.add(edge);
94          return this;
95      }
96  
97      /**
98       * Get the set of edges.  This set is live, and can be used to modify the edges that will
99       * be put in the final builder.
100      *
101      * @return The set of edges.
102      */
103     @Nonnull
104     public Set<Pair<DAGNode<V,E>,E>> getEdges() {
105         return edges;
106     }
107 
108     @Nonnull
109     public DAGNode<V,E> build() {
110         Preconditions.checkState(label != null, "no node label set");
111         return new DAGNode<V,E>(label, edges);
112     }
113 
114     @Override
115     public String toString() {
116         return String.format("node builder with label %s and %d edges", label, edges.size());
117     }
118 }