add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / facet / src / java / org / apache / lucene / facet / search / IntArrayAllocator.java
1 package org.apache.lucene.facet.search;
2
3 import java.util.Arrays;
4
5 /**
6  * Licensed to the Apache Software Foundation (ASF) under one or more
7  * contributor license agreements.  See the NOTICE file distributed with
8  * this work for additional information regarding copyright ownership.
9  * The ASF licenses this file to You under the Apache License, Version 2.0
10  * (the "License"); you may not use this file except in compliance with
11  * the License.  You may obtain a copy of the License at
12  *
13  *     http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21
22 /**
23  * An IntArrayAllocator is an object which manages counter array objects
24  * of a certain length. These counter arrays are needed temporarily during
25  * faceted search (see {@link FacetsAccumulator} and can be reused across searches
26  * instead of being allocated afresh on every search.
27  * <P>
28  * An IntArrayAllocator is thread-safe.
29  * 
30  * @lucene.experimental
31  */
32 public final class IntArrayAllocator extends TemporaryObjectAllocator<int[]> {
33
34   // An IntArrayAllocater deals with integer arrays of a fixed length.
35   private int length;
36
37   /**
38    * Construct an allocator for counter arrays of length <CODE>length</CODE>,
39    * keeping around a pool of up to <CODE>maxArrays</CODE> old arrays.
40    * <P>
41    * Note that the pool size only restricts the number of arrays that hang
42    * around when not needed, but <I>not</I> the maximum number of arrays
43    * that are allocated when actually is use: If a number of concurrent
44    * threads ask for an allocation, all of them will get a counter array,
45    * even if their number is greater than maxArrays. If an application wants
46    * to limit the number of concurrent threads making allocations, it needs
47    * to do so on its own - for example by blocking new threads until the
48    * existing ones have finished.
49    * <P>
50    * In particular, when maxArrays=0, this object behaves as a trivial
51    * allocator, always allocating a new array and never reusing an old one. 
52    */
53   public IntArrayAllocator(int length, int maxArrays) {
54     super(maxArrays);
55     this.length = length;
56   }
57
58   @Override
59   public int[] create() {
60     return new int[length];
61   }
62
63   @Override
64   public void clear(int[] array) {
65     Arrays.fill(array, 0);
66   }
67   
68 }