1 /*******************************************************************************
2 * Copyright (c) 2016 Polytechnique
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Loïc Prieur-Drevon - Initial API and implementation
11 ******************************************************************************/
13 package org
.eclipse
.tracecompass
.segmentstore
.core
;
15 import java
.util
.HashSet
;
18 import org
.eclipse
.jdt
.annotation
.NonNull
;
19 import org
.eclipse
.jdt
.annotation
.Nullable
;
20 import org
.eclipse
.tracecompass
.internal
.segmentstore
.core
.arraylist
.ArrayListStore
;
21 import org
.eclipse
.tracecompass
.internal
.segmentstore
.core
.arraylist
.LazyArrayListStore
;
22 import org
.eclipse
.tracecompass
.internal
.segmentstore
.core
.treemap
.TreeMapStore
;
25 * Factory to create Segment Stores.
27 * Since segment stores are meant to be accessed using the {@link ISegmentStore}
28 * interface, you can use this factory to instantiate new ones.
30 * @author Loïc Prieur-Drevon
32 * The type of segment held in this store
35 public final class SegmentStoreFactory
<E
> {
37 * Flags to determine the type of SegmentStore to use.
39 public enum SegmentStoreType
{
41 * Segment Store should be as fast as possible to build and read.
42 * Performance of individual operations may be slower, but overall the
43 * speed should be faster.
47 * Segment Store based should have predictable performance. This does
48 * not mean it's faster, it should not give a surprise random slow down
49 * though. If it slows down, it will be on the {@link ISegment} that
54 * Segment Store should contain no duplicate segments
59 private SegmentStoreFactory() {
64 * New SegmentStore factory method
67 * Flags used to determine the type of Segment Store that will be
70 * @return a new {@link ISegmentStore}
72 public static <E
extends ISegment
> ISegmentStore
<E
> createSegmentStore(@Nullable SegmentStoreType
... segmentTypes
) {
73 Set
<@NonNull SegmentStoreType
> segments
= getListOfFlags(segmentTypes
);
74 if (segments
.contains(SegmentStoreType
.Distinct
)) {
75 return createTreeMapStore();
77 if (segments
.contains(SegmentStoreType
.Stable
)) {
78 return createArrayListStore();
80 // default option is the fastest
81 return createLazyArrayListStore();
86 * New SegmentStore factory method to create store from an array of Objects
89 * Flags used to determine the type of Segment Store that will be
92 * the {@link Object} array we want the returned segment store to
93 * contain, {@link Object} are only inserted if they extend
95 * @return an {@link ISegmentStore} containing the {@link ISegment}s from
98 public static <E
extends ISegment
> ISegmentStore
<E
> createSegmentStore(Object
[] array
, SegmentStoreType
... segmentTypes
) {
99 Set
<@NonNull SegmentStoreType
> segments
= getListOfFlags(segmentTypes
);
100 if (segments
.contains(SegmentStoreType
.Distinct
)) {
101 ISegmentStore
<E
> store
= createTreeMapStore();
102 for (Object elem
: array
) {
103 if (elem
instanceof ISegment
) {
104 store
.add((E
) elem
); // warning from type, it should be fine
109 if (segments
.contains(SegmentStoreType
.Stable
)) {
110 return new ArrayListStore
<>(array
);
112 // default option is the fastest
113 return new LazyArrayListStore
<>(array
);
116 private static Set
<@NonNull SegmentStoreType
> getListOfFlags(SegmentStoreType
... segmentTypes
) {
117 Set
<@NonNull SegmentStoreType
> segments
= new HashSet
<>();
118 for(@Nullable SegmentStoreType segmentType
: segmentTypes
) {
119 if(segmentType
!= null) {
120 segments
.add(segmentType
);
127 * New {@link TreeMapStore} factory method
129 * @return the new Segment Store
131 private static <E
extends ISegment
> ISegmentStore
<E
> createTreeMapStore() {
132 return new TreeMapStore
<>();
136 * New {@link ArrayListStore} factory method
138 * @return the new Segment Store
140 private static <E
extends ISegment
> ISegmentStore
<E
> createArrayListStore() {
141 return new ArrayListStore
<>();
145 * New {@link LazyArrayListStore} factory method
147 * @return the new Segment Store
149 private static <E
extends ISegment
> ISegmentStore
<E
> createLazyArrayListStore() {
150 return new LazyArrayListStore
<>();