Coverage Report - org.apache.giraph.utils.ArrayWritable
 
Classes in this File Line Coverage Branch Coverage Complexity
ArrayWritable
0%
0/23
0%
0/4
1.286
 
 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.utils;
 19  
 
 20  
 import java.io.DataInput;
 21  
 import java.io.DataOutput;
 22  
 import java.io.IOException;
 23  
 import java.lang.reflect.Array;
 24  
 
 25  
 import org.apache.hadoop.io.Writable;
 26  
 import org.apache.hadoop.io.WritableFactories;
 27  
 
 28  
 import com.google.common.base.Preconditions;
 29  
 
 30  
 /**
 31  
  * A Writable for arrays containing instances of a class. The elements of this
 32  
  * writable must all be instances of the same class.
 33  
  *
 34  
  * @param <T> element type
 35  
  */
 36  
 public class ArrayWritable<T extends Writable> implements Writable {
 37  
   /** Element type class */
 38  
   private Class<T> valueClass;
 39  
   /** Array */
 40  
   private T[] values;
 41  
 
 42  
   /** Constructor */
 43  0
   public ArrayWritable() {
 44  0
   }
 45  
 
 46  
   /**
 47  
    * Constructor
 48  
    * @param valueClass Element type class
 49  
    * @param values Array of elements
 50  
    */
 51  0
   public ArrayWritable(Class<T> valueClass, T[] values) {
 52  0
     Preconditions.checkNotNull(valueClass,
 53  
         "valueClass cannot be null in ArrayWritable");
 54  0
     this.valueClass = valueClass;
 55  0
     this.values = values;
 56  0
   }
 57  
 
 58  
   /**
 59  
    * Get element type class
 60  
    * @return element type class
 61  
    */
 62  
   public Class<T> getValueClass() {
 63  0
     return valueClass;
 64  
   }
 65  
 
 66  
   /**
 67  
    * Set array
 68  
    * @param values array
 69  
    */
 70  0
   public void set(T[] values) { this.values = values; }
 71  
 
 72  
   /**
 73  
    * Ger array
 74  
    * @return array
 75  
    */
 76  0
   public T[] get() { return values; }
 77  
 
 78  
   @Override
 79  
   public void readFields(DataInput in) throws IOException {
 80  0
     valueClass = WritableUtils.readClass(in);
 81  0
     values = (T[]) Array.newInstance(valueClass, in.readInt());
 82  
 
 83  0
     for (int i = 0; i < values.length; i++) {
 84  0
       T value = (T) WritableFactories.newInstance(valueClass);
 85  0
       value.readFields(in);                       // read a value
 86  0
       values[i] = value;                          // store it in values
 87  
     }
 88  0
   }
 89  
 
 90  
   @Override
 91  
   public void write(DataOutput out) throws IOException {
 92  0
     Preconditions.checkNotNull(valueClass,
 93  
         "valueClass cannot be null in ArrayWritable");
 94  0
     WritableUtils.writeClass(valueClass, out);
 95  0
     out.writeInt(values.length);                 // write values
 96  0
     for (int i = 0; i < values.length; i++) {
 97  0
       values[i].write(out);
 98  
     }
 99  0
   }
 100  
 }