1 package org.apache.lucene.util.encoding;
3 import java.io.IOException;
4 import java.io.OutputStream;
5 import java.util.Arrays;
8 * Licensed to the Apache Software Foundation (ASF) under one or more
9 * contributor license agreements. See the NOTICE file distributed with
10 * this work for additional information regarding copyright ownership.
11 * The ASF licenses this file to You under the Apache License, Version 2.0
12 * (the "License"); you may not use this file except in compliance with
13 * the License. You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
25 * An {@link IntEncoderFilter} which sorts the values to encode in ascending
26 * order before encoding them. Encoding therefore happens upon calling
27 * {@link #close()}. Since this encoder is usually chained with another encoder
28 * that relies on sorted values, it does not offer a default constructor.
30 * @lucene.experimental
32 public class SortingIntEncoder extends IntEncoderFilter {
34 private float grow = 2.0f;
35 private int index = 0;
36 private int[] set = new int[1024];
38 /** Initializes with the given encoder. */
39 public SortingIntEncoder(IntEncoder encoder) {
44 public void close() throws IOException {
49 Arrays.sort(set, 0, index);
50 for (int i = 0; i < index; i++) {
51 encoder.encode(set[i]);
60 public void encode(int value) throws IOException {
61 if (index == set.length) {
62 int[] newSet = new int[(int) (set.length * grow)];
63 System.arraycopy(set, 0, newSet, 0, set.length);
70 public IntDecoder createMatchingDecoder() {
71 return encoder.createMatchingDecoder();
75 public void reInit(OutputStream out) {
81 public String toString() {
82 return "Sorting (" + encoder.toString() + ")";