pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / facet / src / java / org / apache / lucene / util / encoding / DGapIntDecoder.java
1 package org.apache.lucene.util.encoding;
2
3 import java.io.IOException;
4 import java.io.InputStream;
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 IntDecoder} which wraps another {@link IntDecoder} and reverts the
25  * d-gap that was encoded by {@link DGapIntEncoder}. The wrapped decoder
26  * performs the actual decoding, while this class simply adds the decoded value
27  * to the previous value.
28  * 
29  * @lucene.experimental
30  */
31 public class DGapIntDecoder extends IntDecoder {
32
33   private final IntDecoder decoder;
34
35   private int prev = 0;
36
37   public DGapIntDecoder(IntDecoder decoder) {
38     this.decoder = decoder;
39   }
40
41   @Override
42   public long decode() throws IOException {
43     long decode = decoder.decode();
44     if (decode == EOS) {
45       return EOS;
46     }
47
48     return prev += decode;
49   }
50
51   @Override
52   public void reInit(InputStream in) {
53     decoder.reInit(in);
54     prev = 0;
55   }
56
57   @Override
58   public String toString() {
59     return "DGap (" + decoder.toString() + ")";
60   }
61
62 }