Cambridge SMT System
AlignmentCountMapWritable.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 
17 package uk.ac.cam.eng.extraction.hadoop.datatypes;
18 
19 import java.io.DataInput;
20 import java.io.DataOutput;
21 import java.io.IOException;
22 import java.util.HashMap;
23 
24 import org.apache.hadoop.io.Writable;
25 import org.apache.hadoop.io.file.tfile.Utils;
26 
27 import uk.ac.cam.eng.extraction.Alignment;
28 
34 public class AlignmentCountMapWritable extends
35  HashMap<Alignment, Integer> implements Writable {
36 
37  private static final long serialVersionUID = 1L;
38 
40 
41  private static final long serialVersionUID = 1L;
42 
43  public Integer put(Alignment key, Integer value) {
44  throw new UnsupportedOperationException();
45  };
46  };
47 
49 
50  }
51 
53  for(Entry<Alignment, Integer> entry : other.entrySet()){
54  put(new Alignment(entry.getKey()), entry.getValue());
55  }
56  }
57 
58  public void increment(AlignmentCountMapWritable newCounts) {
59  for (Entry<Alignment, Integer> alignCount : newCounts
60  .entrySet()) {
61  Alignment key = alignCount.getKey();
62  if (containsKey(key)) {
63  put(key, get(key) + newCounts.get(key));
64  } else {
65  put(key, newCounts.get(key));
66  }
67  }
68  }
69 
70  public void merge(AlignmentCountMapWritable other) {
71  int expectedSize = size() + other.size();
72  putAll(other);
73  if (expectedSize != size()) {
74  throw new RuntimeException("Merging twice the same alignment "
75  + this + other);
76  }
77  }
78 
79  /*
80  * (non-Javadoc)
81  *
82  * @see org.apache.hadoop.io.Writable#write(java.io.DataOutput)
83  */
84  @Override
85  public void write(DataOutput out) throws IOException {
86  Utils.writeVInt(out, size());
87  for (Alignment a : keySet()) {
88  a.write(out);
89  Utils.writeVInt(out, get(a));
90  }
91  }
92 
93  /*
94  * (non-Javadoc)
95  *
96  * @see org.apache.hadoop.io.Writable#readFields(java.io.DataInput)
97  */
98  @Override
99  public void readFields(DataInput in) throws IOException {
100  clear();
101  int size = Utils.readVInt(in);
102  for (int i = 0; i < size; ++i) {
103  Alignment a = new Alignment();
104  a.readFields(in);
105  int alignmentCount = Utils.readVInt(in);
106  put(a, alignmentCount);
107  }
108  }
109 
110 }