File: ThreadSafeIndexer.java

package info (click to toggle)
beagle 5.1-200518%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 9,480 kB
  • sloc: java: 14,430; sh: 55; makefile: 11
file content (201 lines) | stat: -rw-r--r-- 6,487 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
 * Copyright (C) 2014-2016 Brian L. Browning
 *
 * This file is part of Beagle
 *
 * Beagle is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Beagle is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package beagleutil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>Class {@code ThreadSafeIndexer} indexes objects.
 * </p>
 * Instances of class {@code ThreadSafeIndexer} are thread-safe.
 *
 * @param <T> the type parameter.
 *
 * @author Brian L. Browning {@code <browning@uw.edu>}
 */
public final class ThreadSafeIndexer<T> {

    /**
     * The default initial capacity, which is 500.
     */
    public static final int DEFAULT_INIT_CAPACITY = 500;

    private final List<T> list ;
    private final Map<T, Integer> map;

    /**
     * Creates a new {@code ThreadSafeIndexer} instance with the default
     * initial capacity.
     *
     * @see #DEFAULT_INIT_CAPACITY
     */
    public ThreadSafeIndexer() {
        this(DEFAULT_INIT_CAPACITY);
    }

    /**
     * Creates a new {@code ThreadSafeIndexer}instance with the specified
     * initial capacity.
     * @param initCapacity the initial capacity
     * @throws IllegalArgumentException if {@code initCapacity < 1}
     */
    public ThreadSafeIndexer(int initCapacity) {
        if (initCapacity < 1) {
            throw new IllegalArgumentException(String.valueOf(initCapacity));
        }
        this.list = new ArrayList<>(initCapacity);
        this.map = new HashMap<>(initCapacity);
    }

    /**
     * Returns the index of the specified object.  If the object
     * is not yet indexed, the object will be indexed. Indices
     * are assigned in consecutive order beginning with 0.
     * @param object the object whose index will be retrieved
     * @return the index of the specified object
     * @throws NullPointerException if {@code object==null}
     */
    public synchronized int getIndex(T object) {
        if (object==null) {
            throw new NullPointerException();
        }
        if (map.keySet().contains(object)) {
            return map.get(object);
        }
        else {
            int idIndex = list.size();
            list.add(object);
            map.put(object, idIndex);
            return idIndex;
        }
    }

    /**
     * Returns an array of object indices corresponding to the specified
     * object array.  If an object is not yet indexed, the object will be
     * indexed.  Object indices are assigned in increasing order starting with 0.
     * @param objects an array of objects
     * @return an array of object identifier indices
     * @throws IllegalArgumentException if there is a {@code j} satisfying
     * {@code (0 <= j && j < objects.length) && objects[j].isEmpty()}
     * @throws NullPointerException if {@code objects == null}
     * @throws NullPointerException if there is a {@code j} satisfying
     * {@code (0 <= j && j < objects.length) && (objects[j] == null)}
     */
    public synchronized int[] getIndices(T[] objects) {
        int[] indices = new int[objects.length];
        for (int j=0; j<indices.length; ++j) {
            T object = objects[j];
            if (object==null) {
                throw new NullPointerException();
            }
            if (map.keySet().contains(object)) {
                indices[j] = map.get(object);
            }
            else {
                int idIndex = list.size();
                list.add(object);
                map.put(object, idIndex);
                indices[j] = idIndex;
            }
        }
        return indices;
    }

    /**
     * Returns the index of the specified object, or returns
     * {@code -1} if the specified object is not indexed.
     *
     * @param object an object
     * @return the index of the specified object, or
     * {@code -1} if the specified object is not indexed
     *
     * @throws NullPointerException if {@code object == null}.
     */
    public synchronized int getIndexIfIndexed(T object) {
        if (object==null) {
            throw new NullPointerException();
        }
        if (map.keySet().contains(object)) {
            return map.get(object);
        }
        else {
            return -1;
        }
    }

    /**
     * Returns the number of indexed objects.
     * @return the number of indexed objects
     */
    public synchronized int size() {
        return list.size();
    }

    /**
     * Returns the object with the specified index.
     * @param index an object index
     * @return the object with the specified index
     * @throws IndexOutOfBoundsException if
     * {@code  index < 0 || index >= this.size()}
     */
    public synchronized T item(int index) {
        return list.get(index);
    }

    /**
     * Returns a list of objects with the specified indices.
     * @param indices an array of object indices
     * @return a list of objects with the specified indices
     * @throws IndexOutOfBoundsException if there exists a {@code j} satisfying
     * {@code (0 <= j && j < indices.length)
     * && (indices[j] < 0 || indices[j] >= this.size())}
     */
    public synchronized List<T> items(int[] indices) {
        List<T> items = new ArrayList<>(indices.length);
        for (int index : indices) {
            items.add(list.get(index));
        }
        return items;
    }

    /**
     * Returns an listed of all indexed objects. The returned list will
     * have size {@code this.size()}, and it will satisfy
     * {@code this.items().get(k).equals(this.item(k))==true}
     * for {@code  0 <= k && k < this.size()}
     *
     * @return an array of objects
     */
    public synchronized List<T> items() {
        return new ArrayList<>(list);
    }

    /**
     * Returns {@code this.items().toString()}.
     * @return a string representation of {@code this}
     */
    @Override
    public synchronized String toString() {
        return this.items().toString();
    }
}