Cambridge SMT System
FeatureMap.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  *******************************************************************************/
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 import java.util.EnumMap;
22 
23 import org.apache.hadoop.io.Writable;
24 import org.apache.hadoop.io.WritableUtils;
25 
27 
28 public class FeatureMap extends EnumMap<Feature, ProvenanceProbMap> implements Writable {
29 
30  private static final Feature[] enums = Feature.values();
31 
32  public FeatureMap() {
33  super(Feature.class);
34  }
35 
36  public FeatureMap(FeatureMap other){
37  this();
38  for (Entry<Feature, ProvenanceProbMap> entry : other.entrySet()){
39  put(entry.getKey(), new ProvenanceProbMap(entry.getValue()));
40  }
41  }
42 
43  private static final long serialVersionUID = 1L;
44 
45  static final FeatureMap EMPTY = new FeatureMap() {
46 
47  private static final long serialVersionUID = 1L;
48 
49  public ProvenanceProbMap put(Feature key, ProvenanceProbMap value) {
50  throw new UnsupportedOperationException();
51  };
52  };
53 
54 
55  @Override
56  public void readFields(DataInput in) throws IOException {
57  clear();
58  int noOfKeys = WritableUtils.readVInt(in);
59  for(int i=0;i<noOfKeys;++i){
60  int ordinal = WritableUtils.readVInt(in);
61  Feature f = enums[ordinal];
63  map.readFields(in);
64  put(f, map);
65  }
66  }
67 
68  @Override
69  public void write(DataOutput out) throws IOException {
70  WritableUtils.writeVInt(out, size());
71  for(Entry<Feature, ProvenanceProbMap> entry : entrySet()){
72  WritableUtils.writeVInt(out,entry.getKey().ordinal());
73  entry.getValue().write(out);
74  }
75  }
76 
77  void merge(FeatureMap other) {
78  int expectedSize = size() + other.size();
79  putAll(other);
80  if (expectedSize != size()) {
81  throw new RuntimeException("Two features with the same id: " + this
82  + " " + other + " expected size = " + expectedSize);
83  }
84  }
85 
86 }