This project has retired. For details please refer to its Attic page.
WritableUnwrappers xref
View Javadoc

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;
19  
20  import org.apache.hadoop.io.Writable;
21  
22  import com.google.common.collect.Maps;
23  
24  import java.util.Map;
25  
26  import static org.apache.giraph.types.JavaWritablePair.BOOLEAN_BOOLEAN_WRITABLE;
27  import static org.apache.giraph.types.JavaWritablePair.BYTE_BYTE_WRITABLE;
28  import static org.apache.giraph.types.JavaWritablePair.DOUBLE_DOUBLE_WRITABLE;
29  import static org.apache.giraph.types.JavaWritablePair.DOUBLE_FLOAT_WRITABLE;
30  import static org.apache.giraph.types.JavaWritablePair.FLOAT_FLOAT_WRITABLE;
31  import static org.apache.giraph.types.JavaWritablePair.INT_BYTE_WRITABLE;
32  import static org.apache.giraph.types.JavaWritablePair.INT_INT_WRITABLE;
33  import static org.apache.giraph.types.JavaWritablePair.LONG_BYTE_WRITABLE;
34  import static org.apache.giraph.types.JavaWritablePair.LONG_INT_WRITABLE;
35  import static org.apache.giraph.types.JavaWritablePair.LONG_LONG_WRITABLE;
36  import static org.apache.giraph.types.JavaWritablePair.SHORT_BYTE_WRITABLE;
37  
38  /**
39   * Mapping of all the known Writable wrappers.
40   */
41  public class WritableUnwrappers {
42    /** Map of (Writable,Java)-type pair to wrapper for those types */
43    private static final Map<JavaWritablePair, WritableUnwrapper> MAP;
44  
45    static {
46      MAP = Maps.newHashMap();
47      MAP.put(BOOLEAN_BOOLEAN_WRITABLE, new BooleanWritableToBooleanUnwrapper());
48      MAP.put(BYTE_BYTE_WRITABLE, new ByteWritableToByteUnwrapper());
49      MAP.put(DOUBLE_DOUBLE_WRITABLE, new DoubleWritableToDoubleUnwrapper());
50      MAP.put(DOUBLE_FLOAT_WRITABLE, new FloatWritableToDoubleUnwrapper());
51      MAP.put(FLOAT_FLOAT_WRITABLE, new FloatWritableToFloatUnwrapper());
52      MAP.put(INT_BYTE_WRITABLE, new ByteWritableToIntUnwrapper());
53      MAP.put(INT_INT_WRITABLE, new IntWritableToIntUnwrapper());
54      MAP.put(SHORT_BYTE_WRITABLE, new ByteWritableToShortUnwrapper());
55      MAP.put(LONG_BYTE_WRITABLE, new ByteWritableToLongUnwrapper());
56      MAP.put(LONG_INT_WRITABLE, new IntWritableToLongUnwrapper());
57      MAP.put(LONG_LONG_WRITABLE, new LongWritableToLongUnwrapper());
58    }
59  
60    /** Don't construct */
61    private WritableUnwrappers() { }
62  
63    /**
64     * Lookup type converter
65     *
66     * @param writableClass class of Writable
67     * @param javaClass class of Java type
68     * @param <W> Writable type
69     * @param <J> Java type
70     * @return {@link WritableUnwrapper}
71     */
72    public static <W extends Writable, J> WritableUnwrapper<W, J> lookup(
73        Class<W> writableClass, Class<J> javaClass) {
74      return lookup(JavaWritablePair.create(writableClass, javaClass));
75    }
76  
77    /**
78     * Lookup type converter
79     *
80     * @param classes JavaAndWritableClasses
81     * @param <W> Writable type
82     * @param <J> Java type
83     * @return {@link WritableUnwrapper}
84     */
85    public static <W extends Writable, J> WritableUnwrapper<W, J> lookup(
86        JavaWritablePair<W, J> classes) {
87      return MAP.get(classes);
88    }
89  }