1
//////////////////////////////////////////////////////////////////////
3
// JCSP ("CSP for Java") Libraries //
4
// Copyright (C) 1996-2008 Peter Welch and Paul Austin. //
5
// 2001-2004 Quickstone Technologies Limited. //
7
// This library is free software; you can redistribute it and/or //
8
// modify it under the terms of the GNU Lesser General Public //
9
// License as published by the Free Software Foundation; either //
10
// version 2.1 of the License, or (at your option) any later //
13
// This library is distributed in the hope that it will be //
14
// useful, but WITHOUT ANY WARRANTY; without even the implied //
15
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
16
// PURPOSE. See the GNU Lesser General Public License for more //
19
// You should have received a copy of the GNU Lesser General //
20
// Public License along with this library; if not, write to the //
21
// Free Software Foundation, Inc., 59 Temple Place, Suite 330, //
22
// Boston, MA 02111-1307, USA. //
24
// Author contact: P.H.Welch@kent.ac.uk //
27
//////////////////////////////////////////////////////////////////////
29
package org.jcsp.util.filter;
32
* Storage scheme for a set of filters that is dynamically sized and supports insert and remove
33
* operations to keep the filters in a contiguous block.
35
* @author Quickstone Technologies Limited
40
* The array of filters. The installed filters are in a block at the start of the array.
42
private Filter[] filters;
45
* Number of filters currently installed.
47
private int count = 0;
50
* Constructs a new <code>FilterHolder</code> with an intial capacity of 2.
58
* Constructs a new <Code>FilterHolder</code> with the given initial capacity.
60
* @param initialSize the initial size for the array.
62
FilterHolder(int initialSize)
64
filters = new Filter[initialSize];
68
* Adds a filter to the end of the array, possibly enlarging it if it is full.
70
* @param filter the filter to add.
72
public void addFilter(Filter filter)
75
filters[count] = filter;
80
* Adds a filter at the given index. If the index is past the end of the array, the filter is placed
81
* at the end of the array. If the index is in use, filter is inserted, shifting the existing ones.
82
* If necessary, the array may be enlarged.
84
* @param filter the filter to add.
85
* @param index the position to add the filter.
87
public void addFilter(Filter filter, int index)
95
//shift all elements from specifed index and above along one
96
System.arraycopy(filters, index, filters, index + 1, count - index);
97
filters[index] = filter;
103
* Removes a filter from the set. The first filter, <code>f</code>, satisfying the condition
104
* <code>f.equals (filter)</code> is removed and the remaining filters shifted to close the gap.
106
* @param filter the filter to remove.
108
public void removeFilter(Filter filter)
111
throw new IllegalArgumentException("filter parameter cannot be null");
112
for (int i = 0; i < count; i++)
113
if (filters[i].equals(filter))
118
throw new IllegalArgumentException("supplied filter not installed.");
122
* Removes a filter at a given index. The remaining filters are shifted to close the gap.
124
* @param index the array index to remove the filter.
126
public void removeFilter(int index)
128
if (index > (count - 1) || index < 0)
129
throw new IndexOutOfBoundsException("Invalid filter index.");
130
filters[index] = null;
131
//if filter not the last item in the array
132
//then need to shift all elements after the
134
if (index < filters.length - 1)
136
System.arraycopy(filters, index + 1, filters, index, count - index - 1);
147
* Returns a filter at the given array index.
149
public Filter getFilter(int index)
151
return filters[index];
155
* Returns the number of filters current installed.
157
public int getFilterCount()
163
* Enlarges the size of the array to make room for more filters. Currently the array is doubled
166
private void makeSpace()
168
//if array of filters is full - double size
169
if (count == filters.length)
171
Filter[] filters = new Filter[count * 2];
172
System.arraycopy(this.filters, 0, filters, 0, this.filters.length);
173
this.filters = filters;
178
* Shrinks the array to save space if it is 75% empty.
180
private void compact()
182
int newSize = count + 1;
183
if (count < (filters.length / 4) && newSize < filters.length)
185
//create a new array which
186
Filter[] filters = new Filter[newSize];
187
System.arraycopy(this.filters, 0, filters, 0, count);
188
this.filters = filters;