timing.core: Make ArrayList use the array of segments if possible
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.timing.core / src / org / eclipse / tracecompass / internal / analysis / timing / core / store / ArrayListStore.java
1 /*******************************************************************************
2 * Copyright (c) 2016 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
9
10 package org.eclipse.tracecompass.internal.analysis.timing.core.store;
11
12 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.Comparator;
18 import java.util.Iterator;
19 import java.util.List;
20 import java.util.concurrent.locks.ReadWriteLock;
21 import java.util.concurrent.locks.ReentrantReadWriteLock;
22 import java.util.stream.Collectors;
23
24 import org.eclipse.jdt.annotation.NonNull;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.eclipse.tracecompass.segmentstore.core.ISegment;
27 import org.eclipse.tracecompass.segmentstore.core.ISegmentStore;
28
29 import com.google.common.collect.ImmutableList;
30
31 /**
32 * Implementation of an {@link ISegmentStore} using one in-memory
33 * {@link ArrayList}. This relatively simple implementation holds everything in
34 * memory, and as such cannot contain too much data.
35 *
36 * The ArrayListStore itself is {@link Iterable}, and its iteration order will
37 * be by ascending order of start times. For segments with identical start
38 * times, the secondary comparator will be the end time. If even those are
39 * equal, it will defer to the segments' natural ordering (
40 * {@link ISegment#compareTo}).
41 *
42 * The store's tree maps will not accept duplicate key-value pairs, which means
43 * that if you want several segments with the same start and end times, make
44 * sure their compareTo() differentiates them.
45 *
46 * Removal operations are not supported.
47 *
48 * @param <E>
49 * The type of segment held in this store
50 *
51 * @author Matthew Khouzam
52 */
53 public class ArrayListStore<@NonNull E extends ISegment> implements ISegmentStore<E> {
54
55 private final Comparator<E> COMPARATOR = (o1, o2) -> {
56 int ret = Long.compare(o1.getStart(), o2.getStart());
57 if (ret == 0) {
58 return Long.compare(o1.getEnd(), o2.getEnd());
59 }
60 return ret;
61 };
62
63 private final ReadWriteLock fLock = new ReentrantReadWriteLock(false);
64
65 private final List<E> fStore;
66
67 private @Nullable transient Iterable<E> fLastSnapshot = null;
68
69 /**
70 * Constructor
71 */
72 public ArrayListStore() {
73 fStore = new ArrayList<>();
74 }
75
76 /**
77 * Constructor
78 *
79 * @param array
80 * an array of elements to wrap in the segment store
81 *
82 */
83 public ArrayListStore(Object[] array) {
84 fStore = new ArrayList<>();
85 for (int i = 0; i < array.length; i++) {
86 if (array[i] instanceof ISegment) {
87 fStore.add((E) array[i]);
88 }
89 }
90 fStore.sort(COMPARATOR);
91 }
92 // ------------------------------------------------------------------------
93 // Methods from Collection
94 // ------------------------------------------------------------------------
95
96 @Override
97 public Iterator<E> iterator() {
98 fLock.readLock().lock();
99 try {
100 Iterable<E> lastSnapshot = fLastSnapshot;
101 if (lastSnapshot == null) {
102 lastSnapshot = ImmutableList.copyOf(fStore);
103 fLastSnapshot = lastSnapshot;
104 }
105 return checkNotNull(lastSnapshot.iterator());
106 } finally {
107 fLock.readLock().unlock();
108 }
109 }
110
111 @Override
112 public boolean add(@Nullable E val) {
113 if (val == null) {
114 throw new IllegalArgumentException("Cannot add null value"); //$NON-NLS-1$
115 }
116
117 fLock.writeLock().lock();
118 try {
119 fStore.add(val);
120 // Go backwards to "sift up" like a priority queue
121 for (int i = size() - 1; i > 0 && COMPARATOR.compare(val, fStore.get(i - 1)) < 0; i--) {
122 Collections.swap(fStore, i, i - 1);
123 }
124 return true;
125 } finally {
126 fLock.writeLock().unlock();
127 }
128 }
129
130 @Override
131 public int size() {
132 return fStore.size();
133 }
134
135 @Override
136 public boolean isEmpty() {
137 return fStore.isEmpty();
138 }
139
140 @Override
141 public boolean contains(@Nullable Object o) {
142 fLock.readLock().lock();
143 try {
144 return fStore.contains(o);
145 } finally {
146 fLock.readLock().unlock();
147 }
148 }
149
150 @Override
151 public boolean containsAll(@Nullable Collection<?> c) {
152 fLock.readLock().lock();
153 try {
154 return fStore.containsAll(c);
155 } finally {
156 fLock.readLock().unlock();
157 }
158 }
159
160 @Override
161 public Object[] toArray() {
162 fLock.readLock().lock();
163 try {
164 return fStore.toArray();
165 } finally {
166 fLock.readLock().unlock();
167 }
168 }
169
170 @Override
171 public <T> T[] toArray(T[] a) {
172 fLock.readLock().lock();
173 try {
174 return fStore.toArray(a);
175 } finally {
176 fLock.readLock().unlock();
177 }
178 }
179
180 @Override
181 public boolean remove(@Nullable Object o) {
182 throw new UnsupportedOperationException();
183 }
184
185 @Override
186 public boolean addAll(@Nullable Collection<? extends E> c) {
187 if (c == null) {
188 throw new IllegalArgumentException();
189 }
190
191 fLock.writeLock().lock();
192 try {
193 boolean changed = false;
194 for (E elem : c) {
195 if (this.add(elem)) {
196 changed = true;
197 }
198 }
199 return changed;
200 } finally {
201 fLock.writeLock().unlock();
202 }
203 }
204
205 @Override
206 public boolean removeAll(@Nullable Collection<?> c) {
207 throw new UnsupportedOperationException();
208 }
209
210 @Override
211 public boolean retainAll(@Nullable Collection<?> c) {
212 throw new UnsupportedOperationException();
213 }
214
215 @Override
216 public void clear() {
217 fLock.writeLock().lock();
218 try {
219 fStore.clear();
220 } finally {
221 fLock.writeLock().unlock();
222 }
223 }
224
225 // ------------------------------------------------------------------------
226 // Methods added by ISegmentStore
227 // ------------------------------------------------------------------------
228
229 @Override
230 public Iterable<E> getIntersectingElements(long position) {
231 /*
232 * The intervals intersecting 't' are those whose 1) start time is
233 * *lower* than 't' AND 2) end time is *higher* than 't'.
234 */
235 fLock.readLock().lock();
236 try {
237 return fStore.stream().filter(element -> position >= element.getStart() && position <= element.getEnd()).collect(Collectors.toList());
238 } finally {
239 fLock.readLock().unlock();
240 }
241 }
242
243 @Override
244 public Iterable<E> getIntersectingElements(long start, long end) {
245 fLock.readLock().lock();
246 try {
247 return fStore.stream().filter(element -> !(start > element.getEnd() || end < element.getStart())).collect(Collectors.toList());
248 } finally {
249 fLock.readLock().unlock();
250 }
251 }
252
253 @Override
254 public void dispose() {
255 fLock.writeLock().lock();
256 try {
257 fStore.clear();
258 } finally {
259 fLock.writeLock().unlock();
260 }
261 }
262 }
This page took 0.037729 seconds and 6 git commands to generate.