pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / facet / src / java / org / apache / lucene / util / encoding / DGapIntEncoder.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 encodes the gap between the given values,
25  * rather than the values themselves. This encoder usually yields better
26  * encoding performance space-wise (i.e., the final encoded values consume less
27  * space) if the values are 'close' to each other.
28  * <p>
29  * <b>NOTE:</b> this encoder assumes the values are given to
30  * {@link #encode(int)} in an ascending sorted manner, which ensures only
31  * positive values are encoded and thus yields better performance. If you are
32  * not sure whether the values are sorted or not, it is possible to chain this
33  * encoder with {@link SortingIntEncoder} to ensure the values will be
34  * sorted before encoding.
35  * 
36  * @lucene.experimental
37  */
38 public class DGapIntEncoder extends IntEncoderFilter {
39
40   private int prev = 0;
41
42   /** Initializes with the given encoder. */
43   public DGapIntEncoder(IntEncoder encoder) {
44     super(encoder);
45   }
46
47   @Override
48   public void encode(int value) throws IOException {
49     encoder.encode(value - prev);
50     prev = value;
51   }
52
53   @Override
54   public IntDecoder createMatchingDecoder() {
55     return new DGapIntDecoder(encoder.createMatchingDecoder());
56   }
57   
58   @Override
59   public void reInit(OutputStream out) {
60     super.reInit(out);
61     prev = 0;
62   }
63
64   @Override
65   public String toString() {
66     return "DGap (" + encoder.toString() + ")";
67   }
68   
69 }