1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * 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 and16 * limitations under the License.17 */18package org.apache.giraph.types.ops;
1920import java.io.DataInput;
21import java.io.DataOutput;
22import java.io.IOException;
2324import org.apache.giraph.utils.WritableUtils;
25import org.apache.hadoop.io.BooleanWritable;
26import org.apache.hadoop.io.ByteWritable;
27import org.apache.hadoop.io.DoubleWritable;
28import org.apache.hadoop.io.FloatWritable;
29import org.apache.hadoop.io.IntWritable;
30import org.apache.hadoop.io.LongWritable;
31import org.apache.hadoop.io.MapWritable;
32import org.apache.hadoop.io.Text;
3334/**35 * Utility functions for getting TypeOps instances from class types.36 */37 @SuppressWarnings({ "unchecked", "rawtypes" })
38publicclassTypeOpsUtils {
39/** No instances */40privateTypeOpsUtils() { }
4142/**43 * Get PrimitiveIdTypeOps for given type, or null if there is none.44 * @param type Class type45 * @param <T> Type46 * @return PrimitiveIdTypeOps47 */48publicstatic <T>
49 PrimitiveIdTypeOps<T> getPrimitiveIdTypeOpsOrNull(Class<T> type) {
50if (type.equals(LongWritable.class)) {
51return (PrimitiveIdTypeOps) LongTypeOps.INSTANCE;
52 } elseif (type.equals(IntWritable.class)) {
53return (PrimitiveIdTypeOps) IntTypeOps.INSTANCE;
54 } else {
55returnnull;
56 }
57 }
5859/**60 * Get PrimitiveIdTypeOps for given type.61 * Exception will be thrown if there is none.62 * @param type Class type63 * @param <T> Type64 * @return PrimitiveIdTypeOps65 */66publicstatic <T>
67 PrimitiveIdTypeOps<T> getPrimitiveIdTypeOps(Class<T> type) {
68 PrimitiveIdTypeOps<T> typeOps = getPrimitiveIdTypeOpsOrNull(type);
69if (typeOps != null) {
70return typeOps;
71 } else {
72thrownew IllegalArgumentException(
73 type + " not supported in PrimitiveIdTypeOps");
74 }
75 }
7677/**78 * Get PrimitiveTypeOps for given type, or null if there is none.79 * @param type Class type80 * @param <T> Type81 * @return PrimitiveTypeOps82 */83publicstatic <T>
84 PrimitiveTypeOps<T> getPrimitiveTypeOpsOrNull(Class<T> type) {
85 PrimitiveTypeOps<T> typeOps = getPrimitiveIdTypeOpsOrNull(type);
86if (typeOps != null) {
87return typeOps;
88 } elseif (type.equals(FloatWritable.class)) {
89return (PrimitiveTypeOps) FloatTypeOps.INSTANCE;
90 } elseif (type.equals(DoubleWritable.class)) {
91return (PrimitiveTypeOps) DoubleTypeOps.INSTANCE;
92 } elseif (type.equals(BooleanWritable.class)) {
93return (PrimitiveTypeOps) BooleanTypeOps.INSTANCE;
94 } elseif (type.equals(ByteWritable.class)) {
95return (PrimitiveTypeOps) ByteTypeOps.INSTANCE;
96 } else {
97returnnull;
98 }
99 }
100101/**102 * Get PrimitiveTypeOps for given type.103 * Exception will be thrown if there is none.104 * @param type Class type105 * @param <T> Type106 * @return PrimitiveTypeOps107 */108publicstatic <T>
109 PrimitiveTypeOps<T> getPrimitiveTypeOps(Class<T> type) {
110 PrimitiveTypeOps<T> typeOps = getPrimitiveTypeOpsOrNull(type);
111if (typeOps != null) {
112return typeOps;
113 } else {
114thrownew IllegalArgumentException(
115 type + " not supported in PrimitiveTypeOps");
116 }
117 }
118119/**120 * Get TypeOps for given type, or null if there is none.121 * @param type Class type122 * @param <T> Type123 * @return TypeOps124 */125publicstatic <T> TypeOps<T> getTypeOpsOrNull(Class<T> type) {
126 TypeOps<T> typeOps = getPrimitiveTypeOpsOrNull(type);
127if (typeOps != null) {
128return typeOps;
129 } elseif (type.equals(Text.class)) {
130return (TypeOps) TextTypeOps.INSTANCE;
131 } elseif (type.equals(MapWritable.class)) {
132return (TypeOps) MapTypeOps.INSTANCE;
133 } else {
134returnnull;
135 }
136 }
137138/**139 * Get TypeOps for given type.140 * Exception will be thrown if there is none.141 * @param type Class type142 * @param <T> Type143 * @return TypeOps144 */145publicstatic <T> TypeOps<T> getTypeOps(Class<T> type) {
146 TypeOps<T> typeOps = getTypeOpsOrNull(type);
147if (typeOps != null) {
148return typeOps;
149 } else {
150thrownew IllegalArgumentException(
151 type + " not supported in TypeOps");
152 }
153 }
154155/**156 * Write TypeOps object into a stream157 * @param typeOps type ops instance158 * @param output output stream159 * @param <T> Corresponding type160 */161publicstatic <T> void writeTypeOps(TypeOps<T> typeOps,
162 DataOutput output) throws IOException {
163 WritableUtils.writeEnum((Enum) typeOps, output);
164 }
165166/**167 * Read TypeOps object from the stream168 * @param input input stream169 * @param <O> Concrete TypeOps type170 * @return type ops instance171 */172publicstatic <O extends TypeOps<?>> O readTypeOps(
173 DataInput input) throws IOException {
174return (O) WritableUtils.readEnum(input);
175 }
176 }