Cambridge SMT System
PairWritable.java
Go to the documentation of this file.
1 /*******************************************************************************
2  * Licensed under the Apache License, Version 2.0 (the "License");
3  * you may not use these files except in compliance with the License.
4  * You may obtain a copy of the License at
5  *
6  * http://www.apache.org/licenses/LICENSE-2.0
7  *
8  * Unless required by applicable law or agreed to in writing, software
9  * distributed under the License is distributed on an "AS IS" BASIS,
10  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11  * See the License for the specific language governing permissions and
12  * limitations under the License.
13  *
14  * Copyright 2014 - Juan Pino, Aurelien Waite, William Byrne
15  *******************************************************************************/
16 package uk.ac.cam.eng.extraction.hadoop.datatypes;
17 
18 import java.io.DataInput;
19 import java.io.DataOutput;
20 import java.io.IOException;
21 
22 import org.apache.hadoop.io.Writable;
23 
32 class PairWritable<F extends Writable, S extends Writable> implements
33  Writable {
34 
35  public F first;
36  public S second;
37 
38  public static <F extends Writable, S extends Writable> PairWritable<F, S> createPair(
39  F f, S s) {
40  return new PairWritable<F, S>(f, s);
41  }
42 
43  /*
44  * (non-Javadoc)
45  *
46  * @see java.lang.Object#hashCode()
47  */
48  @Override
49  public int hashCode() {
50  final int prime = 31;
51  int result = 1;
52  result = prime * result + ((first == null) ? 0 : first.hashCode());
53  result = prime * result + ((second == null) ? 0 : second.hashCode());
54  return result;
55  }
56 
57  /*
58  * (non-Javadoc)
59  *
60  * @see java.lang.Object#equals(java.lang.Object)
61  */
62  @Override
63  public boolean equals(Object obj) {
64  if (this == obj)
65  return true;
66  if (obj == null)
67  return false;
68  if (getClass() != obj.getClass())
69  return false;
70  PairWritable<?, ?> other = (PairWritable<?, ?>) obj;
71  if (first == null) {
72  if (other.first != null)
73  return false;
74  } else if (!first.equals(other.first))
75  return false;
76  if (second == null) {
77  if (other.second != null)
78  return false;
79  } else if (!second.equals(other.second))
80  return false;
81  return true;
82  }
83 
84  public PairWritable(F first, S second) {
85 
86  this.first = first;
87  this.second = second;
88  }
89 
90  public PairWritable(Class<F> classF, Class<S> classS) {
91  try {
92  this.first = classF.newInstance();
93  this.second = classS.newInstance();
94  } catch (InstantiationException | IllegalAccessException e) {
95  throw new RuntimeException(e);
96  }
97  }
98 
99  public void set(F f, S s) {
100  first = f;
101  second = s;
102  }
103 
104  public void setFirst(F f) {
105  first = f;
106  }
107 
108  public void setSecond(S s) {
109  second = s;
110  }
111 
112  @Override
113  public void readFields(DataInput in) throws IOException {
114  first.readFields(in);
115  second.readFields(in);
116  }
117 
118  @Override
119  public void write(DataOutput out) throws IOException {
120  first.write(out);
121  second.write(out);
122  }
123 
124  public F getFirst() {
125  return first;
126  }
127 
128  public S getSecond() {
129  return second;
130  }
131 
132  @Override
133  public String toString() {
134  StringBuilder builder = new StringBuilder(first.toString());
135  builder.append("\t").append(second.toString());
136  return builder.toString();
137  }
138 
139 }
std::string toString(const T &x, uint pr=2)
Converts an arbitrary type to string Converts to string integers, floats, doubles Quits execution if ...
double F