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.BYTE_INT_WRITABLE;
29importstatic org.apache.giraph.types.JavaWritablePair.BYTE_LONG_WRITABLE;
30importstatic org.apache.giraph.types.JavaWritablePair.DOUBLE_DOUBLE_WRITABLE;
31importstatic org.apache.giraph.types.JavaWritablePair.FLOAT_DOUBLE_WRITABLE;
32importstatic org.apache.giraph.types.JavaWritablePair.FLOAT_FLOAT_WRITABLE;
33importstatic org.apache.giraph.types.JavaWritablePair.INT_INT_WRITABLE;
34importstatic org.apache.giraph.types.JavaWritablePair.INT_LONG_WRITABLE;
35importstatic org.apache.giraph.types.JavaWritablePair.LONG_LONG_WRITABLE;
36importstatic org.apache.giraph.types.JavaWritablePair.SHORT_INT_WRITABLE;
37importstatic org.apache.giraph.types.JavaWritablePair.SHORT_LONG_WRITABLE;
3839/**40 * Mapping of all the known Writable wrappers.41 */42publicclassWritableWrappers {
43/** Map of (Writable,Java)-type pair to wrapper for those types */44privatestaticfinal Map<JavaWritablePair, WritableWrapper> MAP;
4546static {
47 MAP = Maps.newHashMap();
48 MAP.put(BOOLEAN_BOOLEAN_WRITABLE, newBooleanToBooleanWritableWrapper());
49 MAP.put(BYTE_BYTE_WRITABLE, newByteToByteWritableWrapper());
50 MAP.put(BYTE_INT_WRITABLE, newByteToIntWritableWrapper());
51 MAP.put(BYTE_LONG_WRITABLE, newByteToLongWritableWrapper());
52 MAP.put(DOUBLE_DOUBLE_WRITABLE, newDoubleToDoubleWritableWrapper());
53 MAP.put(FLOAT_DOUBLE_WRITABLE, newFloatToDoubleWritableWrapper());
54 MAP.put(FLOAT_FLOAT_WRITABLE, newFloatToFloatWritableWrapper());
55 MAP.put(INT_INT_WRITABLE, newIntToIntWritableWrapper());
56 MAP.put(INT_LONG_WRITABLE, newIntToLongWritableWrapper());
57 MAP.put(LONG_LONG_WRITABLE, newLongToLongWritableWrapper());
58 MAP.put(SHORT_INT_WRITABLE, newShortToIntWritableWrapper());
59 MAP.put(SHORT_LONG_WRITABLE, newShortToLongWritableWrapper());
60 }
6162/** Don't construct */63privateWritableWrappers() { }
6465/**66 * Lookup type converter67 *68 * @param writableClass class of Writable69 * @param javaClass class of Java type70 * @param <W> Writable type71 * @param <J> Java type72 * @return {@link WritableWrapper}73 */74publicstatic <W extends Writable, J> WritableWrapper<W, J> lookup(
75 Class<W> writableClass, Class<J> javaClass) {
76return lookup(JavaWritablePair.create(writableClass, javaClass));
77 }
7879/**80 * Lookup type converter81 *82 * @param classes JavaAndWritableClasses83 * @param <W> Writable type84 * @param <J> Java type85 * @return {@link WritableWrapper}86 */87publicstatic <W extends Writable, J> WritableWrapper<W, J> lookup(
88 JavaWritablePair<W, J> classes) {
89return MAP.get(classes);
90 }
91 }