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.collections; 19 20 import org.apache.giraph.types.ops.PrimitiveIdTypeOps; 21 import org.apache.giraph.types.ops.TypeOpsUtils; 22 import org.apache.giraph.types.ops.collections.Basic2ObjectMap.BasicObject2ObjectOpenHashMap; 23 import org.apache.hadoop.io.Writable; 24 25 /** 26 * Utility functions for constructing basic collections 27 */ 28 public class BasicCollectionsUtils { 29 /** No instances */ 30 private BasicCollectionsUtils() { } 31 32 /** 33 * Construct OpenHashMap with primitive keys. 34 * 35 * @param <I> Vertex id type 36 * @param <V> Value type 37 * @param idClass Class type 38 * @return map 39 */ 40 public static <I extends Writable, V> 41 Basic2ObjectMap<I, V> create2ObjectMap(Class<I> idClass) { 42 return create2ObjectMap(idClass, null, null); 43 } 44 45 /** 46 * Construct OpenHashMap with primitive keys. 47 * 48 * If keyWriter/valueWriter are not provided, 49 * readFields/write will throw an Exception, if called. 50 * 51 * @param <I> Vertex id type 52 * @param <V> Value type 53 * @param idClass Class type 54 * @param keyWriter writer for keys 55 * @param valueWriter writer for values 56 * @return map 57 */ 58 public static <I extends Writable, V> 59 Basic2ObjectMap<I, V> create2ObjectMap( 60 Class<I> idClass, 61 WritableWriter<I> keyWriter, 62 WritableWriter<V> valueWriter 63 ) { 64 PrimitiveIdTypeOps<I> idTypeOps = TypeOpsUtils.getPrimitiveIdTypeOpsOrNull( 65 idClass 66 ); 67 if (idTypeOps != null) { 68 return idTypeOps.create2ObjectOpenHashMap(valueWriter); 69 } else { 70 return new BasicObject2ObjectOpenHashMap<>(keyWriter, valueWriter); 71 } 72 } 73 }