add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / facet / src / java / org / apache / lucene / util / encoding / UniqueValuesIntEncoder.java
1 package org.apache.lucene.util.encoding;
2
3 import java.io.IOException;
4 import java.io.OutputStream;
5
6 /**
7  * Licensed to the Apache Software Foundation (ASF) under one or more
8  * contributor license agreements.  See the NOTICE file distributed with
9  * this work for additional information regarding copyright ownership.
10  * The ASF licenses this file to You under the Apache License, Version 2.0
11  * (the "License"); you may not use this file except in compliance with
12  * the License.  You may obtain a copy of the License at
13  *
14  *     http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  */
22
23 /**
24  * An {@link IntEncoderFilter} which ensures only unique values are encoded. The
25  * implementation assumes the values given to {@link #encode(int)} are sorted.
26  * If this is not the case, you can chain this encoder with
27  * {@link SortingIntEncoder}.
28  * 
29  * @lucene.experimental
30  */
31 public final class UniqueValuesIntEncoder extends IntEncoderFilter {
32
33   /**
34    * Denotes an illegal value which we can use to init 'prev' to. Since all
35    * encoded values are integers, this value is init to MAX_INT+1 and is of type
36    * long. Therefore we are guaranteed not to get this value in encode.
37    */
38   private static final long ILLEGAL_VALUE = Integer.MAX_VALUE + 1;
39
40   private long prev = ILLEGAL_VALUE;
41   
42   /** Constructs a new instance with the given encoder. */
43   public UniqueValuesIntEncoder(IntEncoder encoder) {
44     super(encoder);
45   }
46
47   @Override
48   public void encode(int value) throws IOException {
49     if (prev != value) {
50       encoder.encode(value);
51       prev = value;
52     }
53   }
54
55   @Override
56   public IntDecoder createMatchingDecoder() {
57     return encoder.createMatchingDecoder();
58   }
59   
60   @Override
61   public void reInit(OutputStream out) {
62     super.reInit(out);
63     prev = ILLEGAL_VALUE;
64   }
65
66   @Override
67   public String toString() {
68     return "Unique (" + encoder.toString() + ")";
69   }
70   
71 }