Coverage Report - org.apache.giraph.object.MultiSizedReusable
 
Classes in this File Line Coverage Branch Coverage Complexity
MultiSizedReusable
0%
0/21
0%
0/8
0
MultiSizedReusable$1
0%
0/2
N/A
0
MultiSizedReusable$2
0%
0/3
N/A
0
MultiSizedReusable$3
0%
0/2
N/A
0
MultiSizedReusable$4
0%
0/3
N/A
0
 
 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.object;
 19  
 
 20  
 import org.apache.giraph.function.Consumer;
 21  
 import org.apache.giraph.function.primitive.Int2ObjFunction;
 22  
 import org.apache.giraph.types.ops.PrimitiveIdTypeOps;
 23  
 import org.apache.giraph.types.ops.collections.Basic2ObjectMap;
 24  
 import org.apache.giraph.types.ops.collections.BasicSet;
 25  
 
 26  
 import com.google.common.base.Preconditions;
 27  
 
 28  
 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 29  
 
 30  
 /**
 31  
  * Holds reusable objects of multiple sizes.
 32  
  * Example usecase, is when we need a hashmap - that we will insert and iterate
 33  
  * on, both clear() and iterate method depend on size. And if we want to reuse
 34  
  * objects, we want to have multiple objects of different sizes, that we will
 35  
  * reuse.
 36  
  *
 37  
  * Instead of creating object for each distinct size, it creates objects with
 38  
  * first larger power of 2.
 39  
  *
 40  
  * @param <T> Type of reusable object
 41  
  */
 42  
 public class MultiSizedReusable<T> implements Int2ObjFunction<T> {
 43  
   private final Int2ObjFunction<T> createSized;
 44  
   private final Consumer<T> init;
 45  0
   @SuppressWarnings("unchecked")
 46  
   @SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED")
 47  
   private final transient T[] holder = (T[]) new Object[Integer.SIZE];
 48  
 
 49  
   // No-arg constructor Kryo can call to initialize holder
 50  
   MultiSizedReusable() {
 51  0
     this(null, null);
 52  0
   }
 53  
 
 54  0
   public MultiSizedReusable(Int2ObjFunction<T> createSized, Consumer<T> init) {
 55  0
     this.createSized = createSized;
 56  0
     this.init = init;
 57  0
   }
 58  
 
 59  
   @Override
 60  
   public T apply(int size) {
 61  0
     Preconditions.checkArgument(size >= 0);
 62  0
     int shiftBits = (Integer.SIZE -
 63  0
         Integer.numberOfLeadingZeros(Math.max(0, size - 1))) / 2;
 64  0
     T result = holder[shiftBits];
 65  0
     if (result == null) {
 66  0
       if (shiftBits >= 15) {
 67  0
         result = createSized.apply(Integer.MAX_VALUE);
 68  
       } else {
 69  0
         result = createSized.apply(1 << (shiftBits * 2 + 1));
 70  
       }
 71  0
       holder[shiftBits] = result;
 72  
     }
 73  0
     if (init != null) {
 74  0
       init.apply(result);
 75  
     }
 76  0
     return result;
 77  
   }
 78  
 
 79  
   public static <I> MultiSizedReusable<BasicSet<I>> createForBasicSet(
 80  
       final PrimitiveIdTypeOps<I> idTypeOps) {
 81  0
     return new MultiSizedReusable<>(
 82  0
         new Int2ObjFunction<BasicSet<I>>() {
 83  
           @Override
 84  
           public BasicSet<I> apply(int value) {
 85  0
             return idTypeOps.createOpenHashSet(value);
 86  
           }
 87  
         },
 88  0
         new Consumer<BasicSet<I>>() {
 89  
           @Override
 90  
           public void apply(BasicSet<I> t) {
 91  0
             t.clear();
 92  0
           }
 93  
         });
 94  
   }
 95  
 
 96  
   public static <K, V>
 97  
   MultiSizedReusable<Basic2ObjectMap<K, V>> createForBasic2ObjectMap(
 98  
       final PrimitiveIdTypeOps<K> idTypeOps) {
 99  0
     return new MultiSizedReusable<>(
 100  0
         new Int2ObjFunction<Basic2ObjectMap<K, V>>() {
 101  
           @Override
 102  
           public Basic2ObjectMap<K, V> apply(int value) {
 103  0
             return idTypeOps.create2ObjectOpenHashMap(value, null);
 104  
           }
 105  
         },
 106  0
         new Consumer<Basic2ObjectMap<K, V>>() {
 107  
           @Override
 108  
           public void apply(Basic2ObjectMap<K, V> t) {
 109  0
             t.clear();
 110  0
           }
 111  
         });
 112  
   }
 113  
 }