1 package org.apache.lucene.util.encoding;
3 import java.io.IOException;
4 import java.io.OutputStream;
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
14 * http://www.apache.org/licenses/LICENSE-2.0
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.
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}.
29 * @lucene.experimental
31 public final class UniqueValuesIntEncoder extends IntEncoderFilter {
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.
38 private static final long ILLEGAL_VALUE = Integer.MAX_VALUE + 1;
40 private long prev = ILLEGAL_VALUE;
42 /** Constructs a new instance with the given encoder. */
43 public UniqueValuesIntEncoder(IntEncoder encoder) {
48 public void encode(int value) throws IOException {
50 encoder.encode(value);
56 public IntDecoder createMatchingDecoder() {
57 return encoder.createMatchingDecoder();
61 public void reInit(OutputStream out) {
67 public String toString() {
68 return "Unique (" + encoder.toString() + ")";