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.object;
1920import org.apache.giraph.function.Consumer;
21import org.apache.giraph.function.primitive.Int2ObjFunction;
22import org.apache.giraph.types.ops.PrimitiveIdTypeOps;
23import org.apache.giraph.types.ops.collections.Basic2ObjectMap;
24import org.apache.giraph.types.ops.collections.BasicSet;
2526import com.google.common.base.Preconditions;
2728import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2930/**31 * Holds reusable objects of multiple sizes.32 * Example usecase, is when we need a hashmap - that we will insert and iterate33 * on, both clear() and iterate method depend on size. And if we want to reuse34 * objects, we want to have multiple objects of different sizes, that we will35 * reuse.36 *37 * Instead of creating object for each distinct size, it creates objects with38 * first larger power of 2.39 *40 * @param <T> Type of reusable object41 */42publicclass MultiSizedReusable<T> implements Int2ObjFunction<T> {
43privatefinal Int2ObjFunction<T> createSized;
44privatefinal Consumer<T> init;
45 @SuppressWarnings("unchecked")
46 @SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED")
47privatefinaltransient T[] holder = (T[]) new Object[Integer.SIZE];
4849// No-arg constructor Kryo can call to initialize holder50MultiSizedReusable() {
51this(null, null);
52 }
5354publicMultiSizedReusable(Int2ObjFunction<T> createSized, Consumer<T> init) {
55this.createSized = createSized;
56this.init = init;
57 }
5859 @Override
60public T apply(int size) {
61 Preconditions.checkArgument(size >= 0);
62int shiftBits = (Integer.SIZE -
63 Integer.numberOfLeadingZeros(Math.max(0, size - 1))) / 2;
64 T result = holder[shiftBits];
65if (result == null) {
66if (shiftBits >= 15) {
67 result = createSized.apply(Integer.MAX_VALUE);
68 } else {
69 result = createSized.apply(1 << (shiftBits * 2 + 1));
70 }
71 holder[shiftBits] = result;
72 }
73if (init != null) {
74 init.apply(result);
75 }
76return result;
77 }
7879publicstatic <I> MultiSizedReusable<BasicSet<I>> createForBasicSet(
80final PrimitiveIdTypeOps<I> idTypeOps) {
81returnnew MultiSizedReusable<>(
82new Int2ObjFunction<BasicSet<I>>() {
83 @Override
84public BasicSet<I> apply(int value) {
85return idTypeOps.createOpenHashSet(value);
86 }
87 },
88new Consumer<BasicSet<I>>() {
89 @Override
90publicvoid apply(BasicSet<I> t) {
91 t.clear();
92 }
93 });
94 }
9596publicstatic <K, V>
97 MultiSizedReusable<Basic2ObjectMap<K, V>> createForBasic2ObjectMap(
98final PrimitiveIdTypeOps<K> idTypeOps) {
99returnnew MultiSizedReusable<>(
100new Int2ObjFunction<Basic2ObjectMap<K, V>>() {
101 @Override
102public Basic2ObjectMap<K, V> apply(int value) {
103return idTypeOps.create2ObjectOpenHashMap(value, null);
104 }
105 },
106new Consumer<Basic2ObjectMap<K, V>>() {
107 @Override
108publicvoid apply(Basic2ObjectMap<K, V> t) {
109 t.clear();
110 }
111 });
112 }
113 }