~ubuntu-branches/ubuntu/precise/jcsp/precise

« back to all changes in this revision

Viewing changes to src/org/jcsp/util/filter/FilterHolder.java

  • Committer: Bazaar Package Importer
  • Author(s): Miguel Landaeta
  • Date: 2010-06-20 18:12:26 UTC
  • Revision ID: james.westby@ubuntu.com-20100620181226-8yg8d9rjjjiuy7oz
Tags: upstream-1.1-rc4
ImportĀ upstreamĀ versionĀ 1.1-rc4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
    //////////////////////////////////////////////////////////////////////
 
2
    //                                                                  //
 
3
    //  JCSP ("CSP for Java") Libraries                                 //
 
4
    //  Copyright (C) 1996-2008 Peter Welch and Paul Austin.            //
 
5
    //                2001-2004 Quickstone Technologies Limited.        //
 
6
    //                                                                  //
 
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       //
 
11
    //  version.                                                        //
 
12
    //                                                                  //
 
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     //
 
17
    //  details.                                                        //
 
18
    //                                                                  //
 
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.                                     //
 
23
    //                                                                  //
 
24
    //  Author contact: P.H.Welch@kent.ac.uk                             //
 
25
    //                                                                  //
 
26
    //                                                                  //
 
27
    //////////////////////////////////////////////////////////////////////
 
28
 
 
29
package org.jcsp.util.filter;
 
30
 
 
31
/**
 
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.
 
34
 *
 
35
 * @author Quickstone Technologies Limited
 
36
 */
 
37
class FilterHolder
 
38
{
 
39
    /**
 
40
     * The array of filters. The installed filters are in a block at the start of the array.
 
41
     */
 
42
    private Filter[] filters;
 
43
 
 
44
    /**
 
45
     * Number of filters currently installed.
 
46
     */
 
47
    private int count = 0;
 
48
 
 
49
    /**
 
50
     * Constructs a new <code>FilterHolder</code> with an intial capacity of 2.
 
51
     */
 
52
    FilterHolder()
 
53
    {
 
54
        this(2);
 
55
    }
 
56
 
 
57
    /**
 
58
     * Constructs a new <Code>FilterHolder</code> with the given initial capacity.
 
59
     *
 
60
     * @param initialSize the initial size for the array.
 
61
     */
 
62
    FilterHolder(int initialSize)
 
63
    {
 
64
        filters = new Filter[initialSize];
 
65
    }
 
66
 
 
67
    /**
 
68
     * Adds a filter to the end of the array, possibly enlarging it if it is full.
 
69
     *
 
70
     * @param filter the filter to add.
 
71
     */
 
72
    public void addFilter(Filter filter)
 
73
    {
 
74
        makeSpace();
 
75
        filters[count] = filter;
 
76
        count++;
 
77
    }
 
78
 
 
79
    /**
 
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.
 
83
     *
 
84
     * @param filter the filter to add.
 
85
     * @param index the position to add the filter.
 
86
     */
 
87
    public void addFilter(Filter filter, int index)
 
88
    {
 
89
        if (index >= count)
 
90
            //add filter to end
 
91
            addFilter(filter);
 
92
        else
 
93
        {
 
94
            makeSpace();
 
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;
 
98
            count++;
 
99
        }
 
100
    }
 
101
 
 
102
    /**
 
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.
 
105
     *
 
106
     * @param filter the filter to remove.
 
107
     */
 
108
    public void removeFilter(Filter filter)
 
109
    {
 
110
        if (filter == null)
 
111
            throw new IllegalArgumentException("filter parameter cannot be null");
 
112
        for (int i = 0; i < count; i++)
 
113
            if (filters[i].equals(filter))
 
114
            {
 
115
                removeFilter(i);
 
116
                return;
 
117
            }
 
118
        throw new IllegalArgumentException("supplied filter not installed.");
 
119
    }
 
120
 
 
121
    /**
 
122
     * Removes a filter at a given index. The remaining filters are shifted to close the gap.
 
123
     *
 
124
     * @param index the array index to remove the filter.
 
125
     */
 
126
    public void removeFilter(int index)
 
127
    {
 
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
 
133
        //specified filter
 
134
        if (index < filters.length - 1)
 
135
        {
 
136
            System.arraycopy(filters, index + 1, filters, index, count - index - 1);
 
137
            count--;
 
138
        }
 
139
        else
 
140
        {
 
141
            count--;
 
142
            compact();
 
143
        }
 
144
    }
 
145
 
 
146
    /**
 
147
     * Returns a filter at the given array index.
 
148
     */
 
149
    public Filter getFilter(int index)
 
150
    {
 
151
        return filters[index];
 
152
    }
 
153
 
 
154
    /**
 
155
     * Returns the number of filters current installed.
 
156
     */
 
157
    public int getFilterCount()
 
158
    {
 
159
        return count;
 
160
    }
 
161
 
 
162
    /**
 
163
     * Enlarges the size of the array to make room for more filters. Currently the array is doubled
 
164
     * in size.
 
165
     */
 
166
    private void makeSpace()
 
167
    {
 
168
        //if array of filters is full - double size
 
169
        if (count == filters.length)
 
170
        {
 
171
            Filter[] filters = new Filter[count * 2];
 
172
            System.arraycopy(this.filters, 0, filters, 0, this.filters.length);
 
173
            this.filters = filters;
 
174
        }
 
175
    }
 
176
 
 
177
    /**
 
178
     * Shrinks the array to save space if it is 75% empty.
 
179
     */
 
180
    private void compact()
 
181
    {
 
182
        int newSize = count + 1;
 
183
        if (count < (filters.length / 4) && newSize < filters.length)
 
184
        {
 
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;
 
189
        }
 
190
    }
 
191
}