segment store: introduce a Segment Store Factory and centralize segment stores
[deliverable/tracecompass.git] / statesystem / org.eclipse.tracecompass.segmentstore.core / src / org / eclipse / tracecompass / segmentstore / core / SegmentStoreFactory.java
1 /*******************************************************************************
2 * Copyright (c) 2016 Polytechnique
3 *
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
8 *
9 * Contributors:
10 * Loïc Prieur-Drevon - Initial API and implementation
11 ******************************************************************************/
12
13 package org.eclipse.tracecompass.segmentstore.core;
14
15 import java.util.HashSet;
16 import java.util.Set;
17
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;
23
24 /**
25 * Factory to create Segment Stores.
26 *
27 * Since segment stores are meant to be accessed using the {@link ISegmentStore}
28 * interface, you can use this factory to instantiate new ones.
29 *
30 * @author Loïc Prieur-Drevon
31 * @param <E>
32 * The type of segment held in this store
33 * @since 1.1
34 */
35 public final class SegmentStoreFactory<E> {
36 /**
37 * Flags to determine the type of SegmentStore to use.
38 */
39 public enum SegmentStoreType {
40 /**
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.
44 */
45 Fast,
46 /**
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
50 * slows it down.
51 */
52 Stable,
53 /**
54 * Segment Store should contain no duplicate segments
55 */
56 Distinct
57 }
58
59 private SegmentStoreFactory() {
60 // Do nothing
61 }
62
63 /**
64 * New SegmentStore factory method
65 *
66 * @param segmentTypes
67 * Flags used to determine the type of Segment Store that will be
68 * created
69 *
70 * @return a new {@link ISegmentStore}
71 */
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();
76 }
77 if (segments.contains(SegmentStoreType.Stable)) {
78 return createArrayListStore();
79 }
80 // default option is the fastest
81 return createLazyArrayListStore();
82
83 }
84
85 /**
86 * New SegmentStore factory method to create store from an array of Objects
87 *
88 * @param segmentTypes
89 * Flags used to determine the type of Segment Store that will be
90 * created
91 * @param array
92 * the {@link Object} array we want the returned segment store to
93 * contain, {@link Object} are only inserted if they extend
94 * {@link ISegment}
95 * @return an {@link ISegmentStore} containing the {@link ISegment}s from
96 * array.
97 */
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
105 }
106 }
107 return store;
108 }
109 if (segments.contains(SegmentStoreType.Stable)) {
110 return new ArrayListStore<>(array);
111 }
112 // default option is the fastest
113 return new LazyArrayListStore<>(array);
114 }
115
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);
121 }
122 }
123 return segments;
124 }
125
126 /**
127 * New {@link TreeMapStore} factory method
128 *
129 * @return the new Segment Store
130 */
131 private static <E extends ISegment> ISegmentStore<E> createTreeMapStore() {
132 return new TreeMapStore<>();
133 }
134
135 /**
136 * New {@link ArrayListStore} factory method
137 *
138 * @return the new Segment Store
139 */
140 private static <E extends ISegment> ISegmentStore<E> createArrayListStore() {
141 return new ArrayListStore<>();
142 }
143
144 /**
145 * New {@link LazyArrayListStore} factory method
146 *
147 * @return the new Segment Store
148 */
149 private static <E extends ISegment> ISegmentStore<E> createLazyArrayListStore() {
150 return new LazyArrayListStore<>();
151 }
152
153 }
This page took 0.041694 seconds and 5 git commands to generate.