9537fb403ab5879197e8f55448a2770846b580a3
1 /*******************************************************************************
2 * Copyright (c) 2016 Ericsson
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 *******************************************************************************/
10 package org
.eclipse
.tracecompass
.internal
.analysis
.timing
.core
.store
;
12 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
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
;
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
;
29 import com
.google
.common
.collect
.ImmutableList
;
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.
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}).
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.
46 * Removal operations are not supported.
49 * The type of segment held in this store
51 * @author Matthew Khouzam
53 public class ArrayListStore
<@NonNull E
extends ISegment
> implements ISegmentStore
<E
> {
55 private final Comparator
<E
> COMPARATOR
= (o1
, o2
) -> {
56 int ret
= Long
.compare(o1
.getStart(), o2
.getStart());
58 return Long
.compare(o1
.getEnd(), o2
.getEnd());
63 private final ReadWriteLock fLock
= new ReentrantReadWriteLock(false);
65 private final List
<E
> fStore
;
67 private @Nullable transient Iterable
<E
> fLastSnapshot
= null;
72 public ArrayListStore() {
73 fStore
= new ArrayList
<>();
80 * an array of elements to wrap in the segment store
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
]);
90 fStore
.sort(COMPARATOR
);
92 // ------------------------------------------------------------------------
93 // Methods from Collection
94 // ------------------------------------------------------------------------
97 public Iterator
<E
> iterator() {
98 fLock
.readLock().lock();
100 Iterable
<E
> lastSnapshot
= fLastSnapshot
;
101 if (lastSnapshot
== null) {
102 lastSnapshot
= ImmutableList
.copyOf(fStore
);
103 fLastSnapshot
= lastSnapshot
;
105 return checkNotNull(lastSnapshot
.iterator());
107 fLock
.readLock().unlock();
112 public boolean add(@Nullable E val
) {
114 throw new IllegalArgumentException("Cannot add null value"); //$NON-NLS-1$
117 fLock
.writeLock().lock();
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);
124 fLastSnapshot
= null;
127 fLock
.writeLock().unlock();
133 return fStore
.size();
137 public boolean isEmpty() {
138 fLock
.readLock().lock();
140 return fStore
.isEmpty();
142 fLock
.readLock().unlock();
147 public boolean contains(@Nullable Object o
) {
148 fLock
.readLock().lock();
150 return fStore
.contains(o
);
152 fLock
.readLock().unlock();
157 public boolean containsAll(@Nullable Collection
<?
> c
) {
158 fLock
.readLock().lock();
160 return fStore
.containsAll(c
);
162 fLock
.readLock().unlock();
167 public Object
[] toArray() {
168 fLock
.readLock().lock();
170 return fStore
.toArray();
172 fLock
.readLock().unlock();
177 public <T
> T
[] toArray(T
[] a
) {
178 fLock
.readLock().lock();
180 return fStore
.toArray(a
);
182 fLock
.readLock().unlock();
187 public boolean remove(@Nullable Object o
) {
188 throw new UnsupportedOperationException();
192 public boolean addAll(@Nullable Collection
<?
extends E
> c
) {
194 throw new IllegalArgumentException();
197 fLock
.writeLock().lock();
199 boolean changed
= false;
201 if (this.add(elem
)) {
207 fLock
.writeLock().unlock();
212 public boolean removeAll(@Nullable Collection
<?
> c
) {
213 throw new UnsupportedOperationException();
217 public boolean retainAll(@Nullable Collection
<?
> c
) {
218 throw new UnsupportedOperationException();
222 public void clear() {
223 fLock
.writeLock().lock();
227 fLock
.writeLock().unlock();
231 // ------------------------------------------------------------------------
232 // Methods added by ISegmentStore
233 // ------------------------------------------------------------------------
236 public Iterable
<E
> getIntersectingElements(long position
) {
238 * The intervals intersecting 't' are those whose 1) start time is
239 * *lower* than 't' AND 2) end time is *higher* than 't'.
241 fLock
.readLock().lock();
243 return fStore
.stream().filter(element
-> position
>= element
.getStart() && position
<= element
.getEnd()).collect(Collectors
.toList());
245 fLock
.readLock().unlock();
250 public Iterable
<E
> getIntersectingElements(long start
, long end
) {
251 fLock
.readLock().lock();
253 return fStore
.stream().filter(element
-> !(start
> element
.getEnd() || end
< element
.getStart())).collect(Collectors
.toList());
255 fLock
.readLock().unlock();
260 public void dispose() {
261 fLock
.writeLock().lock();
265 fLock
.writeLock().unlock();
This page took 0.03887 seconds and 4 git commands to generate.