Coverage Report - org.apache.giraph.types.ops.TypeOpsUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
TypeOpsUtils
0%
0/41
0%
0/26
4.222
 
 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  0
   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  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  
    * 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  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  
    * 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  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  
    * 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  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  
    * 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  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  
    * 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  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  
    * 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  0
     WritableUtils.writeEnum((Enum) typeOps, output);
 164  0
   }
 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  0
     return  (O) WritableUtils.readEnum(input);
 175  
   }
 176  
 }