1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. 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 package org.apache.giraph.edge;
20
21 import org.apache.hadoop.io.NullWritable;
22 import org.apache.hadoop.io.Writable;
23 import org.apache.hadoop.io.WritableComparable;
24
25 /**
26 * Factory for creating Edges
27 */
28 public class EdgeFactory {
29 /** Do not construct */
30 private EdgeFactory() { }
31
32 /**
33 * Create an edge pointing to a given ID with a value
34 *
35 * @param id target ID
36 * @param value edge value
37 * @param <I> Vertex ID type
38 * @param <E> Edge Value type
39 * @return Edge pointing to ID with value
40 */
41 public static <I extends WritableComparable,
42 E extends Writable>
43 Edge<I, E> create(I id, E value) {
44 return createReusable(id, value);
45 }
46
47 /**
48 * Create an edge pointing to a given ID without a value
49 *
50 * @param id target ID
51 * @param <I> Vertex ID type
52 * @return Edge pointing to ID without a value
53 */
54 public static <I extends WritableComparable>
55 Edge<I, NullWritable> create(I id) {
56 return createReusable(id);
57 }
58
59 /**
60 * Create a reusable edge pointing to a given ID with a value
61 *
62 * @param id target ID
63 * @param value edge value
64 * @param <I> Vertex ID type
65 * @param <E> Edge Value type
66 * @return Edge pointing to ID with value
67 */
68 public static <I extends WritableComparable,
69 E extends Writable>
70 ReusableEdge<I, E> createReusable(I id, E value) {
71 if (value instanceof NullWritable) {
72 return (ReusableEdge<I, E>) createReusable(id);
73 } else {
74 return new DefaultEdge<I, E>(id, value);
75 }
76 }
77
78 /**
79 * Create a reusable edge pointing to a given ID with a value
80 *
81 * @param id target ID
82 * @param <I> Vertex ID type
83 * @return Edge pointing to ID with value
84 */
85 public static <I extends WritableComparable>
86 ReusableEdge<I, NullWritable> createReusable(I id) {
87 return new EdgeNoValue<I>(id);
88 }
89 }