pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / facet / src / java / org / apache / lucene / util / encoding / NOnesIntEncoder.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  * A variation of {@link FourFlagsIntEncoder} which translates the data as
25  * follows:
26  * <ul>
27  * <li>Values &ge; 2 are trnalsated to <code>value+1</code> (2 &rArr; 3, 3
28  * &rArr; 4 and so forth).
29  * <li>Any <code>N</code> occurrences of 1 are encoded as a single 2.
30  * <li>Otherwise, each 1 is encoded as 1.
31  * </ul>
32  * <p>
33  * Encoding examples:
34  * <ul>
35  * <li>N = 4: the data 1,1,1,1,1 is translated to: 2, 1
36  * <li>N = 3: the data 1,2,3,4,1,1,1,1,5 is translated to 1,3,4,5,2,1,6
37  * </ul>
38  * <b>NOTE:</b> this encoder does not support values &le; 0 and
39  * {@link Integer#MAX_VALUE}. 0 is not supported because it's not supported by
40  * {@link FourFlagsIntEncoder} and {@link Integer#MAX_VALUE} because this
41  * encoder translates N to N+1, which will cause an overflow and
42  * {@link Integer#MAX_VALUE} will become a negative number, which is not
43  * supported as well.<br>
44  * This does not mean you cannot encode {@link Integer#MAX_VALUE}. If it is not
45  * the first value to encode, and you wrap this encoder with
46  * {@link DGapIntEncoder}, then the value that will be sent to this encoder will
47  * be <code>MAX_VAL - prev</code>.
48  * 
49  * @lucene.experimental
50  */
51 public class NOnesIntEncoder extends FourFlagsIntEncoder {
52
53   /** Number of consecutive '1's to be translated into single target value '2'. */
54   private int n;
55
56   /** Counts the number of consecutive ones seen. */
57   private int onesCounter = 0;
58
59   /**
60    * Constructs an encoder with a given value of N (N: Number of consecutive
61    * '1's to be translated into single target value '2').
62    */
63   public NOnesIntEncoder(int n) {
64     this.n = n;
65   }
66
67   @Override
68   public void close() throws IOException {
69     // We might have ones in our buffer, encode them as neccesary.
70     while (onesCounter-- > 0) {
71       super.encode(1);
72     }
73
74     super.close();
75   }
76
77   @Override
78   public void encode(int value) throws IOException {
79     if (value == 1) {
80       // Increment the number of consecutive ones seen so far
81       if (++onesCounter == n) {
82         super.encode(2);
83         onesCounter = 0;
84       }
85       return;
86     }
87
88     // If it's not one - there might have been ones we had to encode prior to
89     // this value
90     while (onesCounter > 0) {
91       --onesCounter;
92       super.encode(1);
93     }
94
95     // encode value + 1 --> the translation.
96     super.encode(value + 1);
97   }
98
99   @Override
100   public IntDecoder createMatchingDecoder() {
101     return new NOnesIntDecoder(n);
102   }
103
104   @Override
105   public void reInit(OutputStream out) {
106     super.reInit(out);
107     onesCounter = 0;
108   }
109
110   @Override
111   public String toString() {
112     return "NOnes (" + n + ") (" + super.toString() + ")";
113   }
114
115 }