1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.giraph.types.ops;
19
20 import java.io.DataInput;
21 import java.io.DataOutput;
22 import java.io.IOException;
23
24 import org.apache.giraph.utils.WritableUtils;
25 import org.apache.hadoop.io.BooleanWritable;
26 import org.apache.hadoop.io.ByteWritable;
27 import org.apache.hadoop.io.DoubleWritable;
28 import org.apache.hadoop.io.FloatWritable;
29 import org.apache.hadoop.io.IntWritable;
30 import org.apache.hadoop.io.LongWritable;
31 import org.apache.hadoop.io.MapWritable;
32 import org.apache.hadoop.io.Text;
33
34
35
36
37 @SuppressWarnings({ "unchecked", "rawtypes" })
38 public class TypeOpsUtils {
39
40 private TypeOpsUtils() { }
41
42
43
44
45
46
47
48 public static <T>
49 PrimitiveIdTypeOps<T> getPrimitiveIdTypeOpsOrNull(Class<T> type) {
50 if (type.equals(LongWritable.class)) {
51 return (PrimitiveIdTypeOps) LongTypeOps.INSTANCE;
52 } else if (type.equals(IntWritable.class)) {
53 return (PrimitiveIdTypeOps) IntTypeOps.INSTANCE;
54 } else {
55 return null;
56 }
57 }
58
59
60
61
62
63
64
65
66 public static <T>
67 PrimitiveIdTypeOps<T> getPrimitiveIdTypeOps(Class<T> type) {
68 PrimitiveIdTypeOps<T> typeOps = getPrimitiveIdTypeOpsOrNull(type);
69 if (typeOps != null) {
70 return typeOps;
71 } else {
72 throw new IllegalArgumentException(
73 type + " not supported in PrimitiveIdTypeOps");
74 }
75 }
76
77
78
79
80
81
82
83 public static <T>
84 PrimitiveTypeOps<T> getPrimitiveTypeOpsOrNull(Class<T> type) {
85 PrimitiveTypeOps<T> typeOps = getPrimitiveIdTypeOpsOrNull(type);
86 if (typeOps != null) {
87 return typeOps;
88 } else if (type.equals(FloatWritable.class)) {
89 return (PrimitiveTypeOps) FloatTypeOps.INSTANCE;
90 } else if (type.equals(DoubleWritable.class)) {
91 return (PrimitiveTypeOps) DoubleTypeOps.INSTANCE;
92 } else if (type.equals(BooleanWritable.class)) {
93 return (PrimitiveTypeOps) BooleanTypeOps.INSTANCE;
94 } else if (type.equals(ByteWritable.class)) {
95 return (PrimitiveTypeOps) ByteTypeOps.INSTANCE;
96 } else {
97 return null;
98 }
99 }
100
101
102
103
104
105
106
107
108 public static <T>
109 PrimitiveTypeOps<T> getPrimitiveTypeOps(Class<T> type) {
110 PrimitiveTypeOps<T> typeOps = getPrimitiveTypeOpsOrNull(type);
111 if (typeOps != null) {
112 return typeOps;
113 } else {
114 throw new IllegalArgumentException(
115 type + " not supported in PrimitiveTypeOps");
116 }
117 }
118
119
120
121
122
123
124
125 public static <T> TypeOps<T> getTypeOpsOrNull(Class<T> type) {
126 TypeOps<T> typeOps = getPrimitiveTypeOpsOrNull(type);
127 if (typeOps != null) {
128 return typeOps;
129 } else if (type.equals(Text.class)) {
130 return (TypeOps) TextTypeOps.INSTANCE;
131 } else if (type.equals(MapWritable.class)) {
132 return (TypeOps) MapTypeOps.INSTANCE;
133 } else {
134 return null;
135 }
136 }
137
138
139
140
141
142
143
144
145 public static <T> TypeOps<T> getTypeOps(Class<T> type) {
146 TypeOps<T> typeOps = getTypeOpsOrNull(type);
147 if (typeOps != null) {
148 return typeOps;
149 } else {
150 throw new IllegalArgumentException(
151 type + " not supported in TypeOps");
152 }
153 }
154
155
156
157
158
159
160
161 public static <T> void writeTypeOps(TypeOps<T> typeOps,
162 DataOutput output) throws IOException {
163 WritableUtils.writeEnum((Enum) typeOps, output);
164 }
165
166
167
168
169
170
171
172 public static <O extends TypeOps<?>> O readTypeOps(
173 DataInput input) throws IOException {
174 return (O) WritableUtils.readEnum(input);
175 }
176 }