Cambridge SMT System
IntWritableCache.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.IOException;
20 
21 import org.apache.hadoop.io.IntWritable;
22 
29 public class IntWritableCache {
30  private static final int low = -128;
31  private static final int high = 127;
32  private static final IntWritable cache[];
33 
34  private static class ImmutableIntWritable extends IntWritable {
35 
36  boolean set = false;
37 
38  ImmutableIntWritable(int i) {
39  super(i);
40  }
41 
42  @Override
43  public void set(int value) {
44  if (set) {
45  throw new UnsupportedOperationException();
46  }
47  set = true;
48  super.set(value);
49  }
50 
51  @Override
52  public void readFields(DataInput in) throws IOException {
53  throw new UnsupportedOperationException();
54  }
55  }
56 
57  static {
58  cache = new IntWritable[(high - low) + 1];
59  int j = low;
60  for (int k = 0; k < cache.length; k++)
61  cache[k] = new ImmutableIntWritable(j++);
62  }
63 
64  private IntWritableCache() {
65  }
66 
67  public static IntWritable createIntWritable(int i) {
68  if (i >= low && i <= high)
69  return cache[i + (-low)];
70  return new IntWritable(i);
71  }
72 }