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 | 0 | private TypeOpsUtils() { } |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
public static <T> |
49 | |
PrimitiveIdTypeOps<T> getPrimitiveIdTypeOpsOrNull(Class<T> type) { |
50 | 0 | if (type.equals(LongWritable.class)) { |
51 | 0 | return (PrimitiveIdTypeOps) LongTypeOps.INSTANCE; |
52 | 0 | } else if (type.equals(IntWritable.class)) { |
53 | 0 | return (PrimitiveIdTypeOps) IntTypeOps.INSTANCE; |
54 | |
} else { |
55 | 0 | return null; |
56 | |
} |
57 | |
} |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
public static <T> |
67 | |
PrimitiveIdTypeOps<T> getPrimitiveIdTypeOps(Class<T> type) { |
68 | 0 | PrimitiveIdTypeOps<T> typeOps = getPrimitiveIdTypeOpsOrNull(type); |
69 | 0 | if (typeOps != null) { |
70 | 0 | return typeOps; |
71 | |
} else { |
72 | 0 | 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 | 0 | PrimitiveTypeOps<T> typeOps = getPrimitiveIdTypeOpsOrNull(type); |
86 | 0 | if (typeOps != null) { |
87 | 0 | return typeOps; |
88 | 0 | } else if (type.equals(FloatWritable.class)) { |
89 | 0 | return (PrimitiveTypeOps) FloatTypeOps.INSTANCE; |
90 | 0 | } else if (type.equals(DoubleWritable.class)) { |
91 | 0 | return (PrimitiveTypeOps) DoubleTypeOps.INSTANCE; |
92 | 0 | } else if (type.equals(BooleanWritable.class)) { |
93 | 0 | return (PrimitiveTypeOps) BooleanTypeOps.INSTANCE; |
94 | 0 | } else if (type.equals(ByteWritable.class)) { |
95 | 0 | return (PrimitiveTypeOps) ByteTypeOps.INSTANCE; |
96 | |
} else { |
97 | 0 | return null; |
98 | |
} |
99 | |
} |
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
public static <T> |
109 | |
PrimitiveTypeOps<T> getPrimitiveTypeOps(Class<T> type) { |
110 | 0 | PrimitiveTypeOps<T> typeOps = getPrimitiveTypeOpsOrNull(type); |
111 | 0 | if (typeOps != null) { |
112 | 0 | return typeOps; |
113 | |
} else { |
114 | 0 | 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 | 0 | TypeOps<T> typeOps = getPrimitiveTypeOpsOrNull(type); |
127 | 0 | if (typeOps != null) { |
128 | 0 | return typeOps; |
129 | 0 | } else if (type.equals(Text.class)) { |
130 | 0 | return (TypeOps) TextTypeOps.INSTANCE; |
131 | 0 | } else if (type.equals(MapWritable.class)) { |
132 | 0 | return (TypeOps) MapTypeOps.INSTANCE; |
133 | |
} else { |
134 | 0 | return null; |
135 | |
} |
136 | |
} |
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
public static <T> TypeOps<T> getTypeOps(Class<T> type) { |
146 | 0 | TypeOps<T> typeOps = getTypeOpsOrNull(type); |
147 | 0 | if (typeOps != null) { |
148 | 0 | return typeOps; |
149 | |
} else { |
150 | 0 | 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 | 0 | WritableUtils.writeEnum((Enum) typeOps, output); |
164 | 0 | } |
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
public static <O extends TypeOps<?>> O readTypeOps( |
173 | |
DataInput input) throws IOException { |
174 | 0 | return (O) WritableUtils.readEnum(input); |
175 | |
} |
176 | |
} |