This project has retired. For details please refer to its Attic page.
TypeOpsUtils xref
View Javadoc

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  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   * Utility functions for getting TypeOps instances from class types.
36   */
37  @SuppressWarnings({ "unchecked", "rawtypes" })
38  public class TypeOpsUtils {
39    /** No instances */
40    private TypeOpsUtils() { }
41  
42    /**
43     * Get PrimitiveIdTypeOps for given type, or null if there is none.
44     * @param type Class type
45     * @param <T> Type
46     * @return PrimitiveIdTypeOps
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     * Get PrimitiveIdTypeOps for given type.
61     * Exception will be thrown if there is none.
62     * @param type Class type
63     * @param <T> Type
64     * @return PrimitiveIdTypeOps
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     * Get PrimitiveTypeOps for given type, or null if there is none.
79     * @param type Class type
80     * @param <T> Type
81     * @return PrimitiveTypeOps
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    * Get PrimitiveTypeOps for given type.
103    * Exception will be thrown if there is none.
104    * @param type Class type
105    * @param <T> Type
106    * @return PrimitiveTypeOps
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    * Get TypeOps for given type, or null if there is none.
121    * @param type Class type
122    * @param <T> Type
123    * @return TypeOps
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    * Get TypeOps for given type.
140    * Exception will be thrown if there is none.
141    * @param type Class type
142    * @param <T> Type
143    * @return TypeOps
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    * Write TypeOps object into a stream
157    * @param typeOps type ops instance
158    * @param output output stream
159    * @param <T> Corresponding type
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    * Read TypeOps object from the stream
168    * @param input input stream
169    * @param <O> Concrete TypeOps type
170    * @return type ops instance
171    */
172   public static <O extends TypeOps<?>> O readTypeOps(
173       DataInput input) throws IOException {
174     return  (O) WritableUtils.readEnum(input);
175   }
176 }