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.types;
1920import org.apache.hadoop.io.Writable;
2122import com.google.common.collect.Maps;
2324import java.util.Map;
2526importstatic org.apache.giraph.types.JavaWritablePair.BOOLEAN_BOOLEAN_WRITABLE;
27importstatic org.apache.giraph.types.JavaWritablePair.BYTE_BYTE_WRITABLE;
28importstatic org.apache.giraph.types.JavaWritablePair.DOUBLE_DOUBLE_WRITABLE;
29importstatic org.apache.giraph.types.JavaWritablePair.DOUBLE_FLOAT_WRITABLE;
30importstatic org.apache.giraph.types.JavaWritablePair.FLOAT_FLOAT_WRITABLE;
31importstatic org.apache.giraph.types.JavaWritablePair.INT_BYTE_WRITABLE;
32importstatic org.apache.giraph.types.JavaWritablePair.INT_INT_WRITABLE;
33importstatic org.apache.giraph.types.JavaWritablePair.LONG_BYTE_WRITABLE;
34importstatic org.apache.giraph.types.JavaWritablePair.LONG_INT_WRITABLE;
35importstatic org.apache.giraph.types.JavaWritablePair.LONG_LONG_WRITABLE;
36importstatic org.apache.giraph.types.JavaWritablePair.SHORT_BYTE_WRITABLE;
3738/**39 * Mapping of all the known Writable wrappers.40 */41publicclassWritableUnwrappers {
42/** Map of (Writable,Java)-type pair to wrapper for those types */43privatestaticfinal Map<JavaWritablePair, WritableUnwrapper> MAP;
4445static {
46 MAP = Maps.newHashMap();
47 MAP.put(BOOLEAN_BOOLEAN_WRITABLE, newBooleanWritableToBooleanUnwrapper());
48 MAP.put(BYTE_BYTE_WRITABLE, newByteWritableToByteUnwrapper());
49 MAP.put(DOUBLE_DOUBLE_WRITABLE, newDoubleWritableToDoubleUnwrapper());
50 MAP.put(DOUBLE_FLOAT_WRITABLE, newFloatWritableToDoubleUnwrapper());
51 MAP.put(FLOAT_FLOAT_WRITABLE, newFloatWritableToFloatUnwrapper());
52 MAP.put(INT_BYTE_WRITABLE, newByteWritableToIntUnwrapper());
53 MAP.put(INT_INT_WRITABLE, newIntWritableToIntUnwrapper());
54 MAP.put(SHORT_BYTE_WRITABLE, newByteWritableToShortUnwrapper());
55 MAP.put(LONG_BYTE_WRITABLE, newByteWritableToLongUnwrapper());
56 MAP.put(LONG_INT_WRITABLE, newIntWritableToLongUnwrapper());
57 MAP.put(LONG_LONG_WRITABLE, newLongWritableToLongUnwrapper());
58 }
5960/** Don't construct */61privateWritableUnwrappers() { }
6263/**64 * Lookup type converter65 *66 * @param writableClass class of Writable67 * @param javaClass class of Java type68 * @param <W> Writable type69 * @param <J> Java type70 * @return {@link WritableUnwrapper}71 */72publicstatic <W extends Writable, J> WritableUnwrapper<W, J> lookup(
73 Class<W> writableClass, Class<J> javaClass) {
74return lookup(JavaWritablePair.create(writableClass, javaClass));
75 }
7677/**78 * Lookup type converter79 *80 * @param classes JavaAndWritableClasses81 * @param <W> Writable type82 * @param <J> Java type83 * @return {@link WritableUnwrapper}84 */85publicstatic <W extends Writable, J> WritableUnwrapper<W, J> lookup(
86 JavaWritablePair<W, J> classes) {
87return MAP.get(classes);
88 }
89 }