This project has retired. For details please refer to its Attic page.
JavaWritablePair 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 com.google.common.base.MoreObjects;
21  import org.apache.hadoop.io.BooleanWritable;
22  import org.apache.hadoop.io.ByteWritable;
23  import org.apache.hadoop.io.DoubleWritable;
24  import org.apache.hadoop.io.FloatWritable;
25  import org.apache.hadoop.io.IntWritable;
26  import org.apache.hadoop.io.LongWritable;
27  import org.apache.hadoop.io.Writable;
28  
29  import com.google.common.base.Objects;
30  
31  /**
32   * Holder for java and writable class pair.
33   *
34   * @param <W> writable class type
35   * @param <J> java class type
36   */
37  public class JavaWritablePair<W extends Writable, J> {
38    /** Boolean,BooleanWritable */
39    public static final JavaWritablePair<BooleanWritable, Boolean>
40    BOOLEAN_BOOLEAN_WRITABLE = create(BooleanWritable.class, Boolean.class);
41    /** Byte,ByteWritable */
42    public static final JavaWritablePair<ByteWritable, Byte>
43    BYTE_BYTE_WRITABLE = create(ByteWritable.class, Byte.class);
44    /** Byte,IntWritable */
45    public static final JavaWritablePair<IntWritable, Byte>
46    BYTE_INT_WRITABLE = create(IntWritable.class, Byte.class);
47    /** Byte,LongWritable */
48    public static final JavaWritablePair<LongWritable, Byte>
49    BYTE_LONG_WRITABLE = create(LongWritable.class, Byte.class);
50    /** Double,FloatWritable */
51    public static final JavaWritablePair<FloatWritable, Double>
52    DOUBLE_FLOAT_WRITABLE = create(FloatWritable.class, Double.class);
53    /** Double,DoubleWritable */
54    public static final JavaWritablePair<DoubleWritable, Double>
55    DOUBLE_DOUBLE_WRITABLE = create(DoubleWritable.class, Double.class);
56    /** Float,FloatWritable */
57    public static final JavaWritablePair<FloatWritable, Float>
58    FLOAT_FLOAT_WRITABLE = create(FloatWritable.class, Float.class);
59    /** Float,DoubleWritable */
60    public static final JavaWritablePair<DoubleWritable, Float>
61    FLOAT_DOUBLE_WRITABLE = create(DoubleWritable.class, Float.class);
62    /** Integer,ByteWritable */
63    public static final JavaWritablePair<ByteWritable, Integer>
64    INT_BYTE_WRITABLE = create(ByteWritable.class, Integer.class);
65    /** Integer,IntWritable */
66    public static final JavaWritablePair<IntWritable, Integer>
67    INT_INT_WRITABLE = create(IntWritable.class, Integer.class);
68    /** Integer,LongWritable */
69    public static final JavaWritablePair<LongWritable, Integer>
70    INT_LONG_WRITABLE = create(LongWritable.class, Integer.class);
71    /** Long,ByteWritable */
72    public static final JavaWritablePair<ByteWritable, Long>
73    LONG_BYTE_WRITABLE = create(ByteWritable.class, Long.class);
74    /** Long,IntWritable */
75    public static final JavaWritablePair<IntWritable, Long>
76    LONG_INT_WRITABLE = create(IntWritable.class, Long.class);
77    /** Long,LongWritable */
78    public static final JavaWritablePair<LongWritable, Long>
79    LONG_LONG_WRITABLE = create(LongWritable.class, Long.class);
80    /** Short,ByteWritable */
81    public static final JavaWritablePair<ByteWritable, Short>
82    SHORT_BYTE_WRITABLE = create(ByteWritable.class, Short.class);
83    /** Short,IntWritable */
84    public static final JavaWritablePair<IntWritable, Short>
85    SHORT_INT_WRITABLE = create(IntWritable.class, Short.class);
86    /** Short,LongWritable */
87    public static final JavaWritablePair<LongWritable, Short>
88    SHORT_LONG_WRITABLE = create(LongWritable.class, Short.class);
89  
90    /** java class */
91    private final Class<J> javaClass;
92    /** writable class */
93    private final Class<W> writableClass;
94  
95    /**
96     * Constructor
97     *
98     * @param javaClass java class
99     * @param writableClass writable class
100    */
101   private JavaWritablePair(Class<W> writableClass, Class<J> javaClass) {
102     this.javaClass = javaClass;
103     this.writableClass = writableClass;
104   }
105 
106   /**
107    * Create holder for classes
108    *
109    * @param writableClass writable class
110    * @param javaClass java class
111    * @param <W> writable class type
112    * @param <J> java class type
113    * @return JavaAndWritableClasses
114    */
115   public static <W extends Writable, J> JavaWritablePair<W, J> create(
116       Class<W> writableClass, Class<J> javaClass) {
117     return new JavaWritablePair<W, J>(writableClass, javaClass);
118   }
119 
120   @Override
121   public boolean equals(Object obj) {
122     if (this == obj) {
123       return true;
124     }
125     if (obj instanceof JavaWritablePair) {
126       JavaWritablePair other = (JavaWritablePair) obj;
127       return Objects.equal(javaClass, other.javaClass) &&
128           Objects.equal(writableClass, other.writableClass);
129     }
130     return false;
131   }
132 
133   @Override
134   public int hashCode() {
135     return Objects.hashCode(javaClass, writableClass);
136   }
137 
138   @Override
139   public String toString() {
140     return MoreObjects.toStringHelper(this)
141         .add("javaClass", javaClass.getSimpleName())
142         .add("writableClass", writableClass.getSimpleName())
143         .toString();
144   }
145 
146   public Class<J> getJavaClass() {
147     return javaClass;
148   }
149 
150   public Class<W> getWritableClass() {
151     return writableClass;
152   }
153 }