Commit | Line | Data |
---|---|---|
8df075b5 FC |
1 | /******************************************************************************* |
2 | * Copyright (c) 2012 Ericsson | |
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 | * Francois Chouinard - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.tmf.core.request; | |
14 | ||
15 | import java.util.Arrays; | |
16 | ||
17 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; | |
18 | import org.eclipse.linuxtools.tmf.core.filter.ITmfFilter; | |
19 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; | |
20 | ||
21 | /** | |
22 | * An event filter based on the event's trace. | |
23 | * | |
24 | * @author Francois Chouinard | |
25 | * @version 1.0 | |
26 | * @since 2.0 | |
27 | */ | |
28 | public final class TmfTraceFilter implements ITmfFilter { | |
29 | ||
30 | // ------------------------------------------------------------------------ | |
31 | // Constants | |
32 | // ------------------------------------------------------------------------ | |
33 | ||
34 | /** | |
35 | * Filter for all events by time range | |
36 | */ | |
37 | public static final TmfTraceFilter ALL_TRACES = new TmfTraceFilter(new ITmfTrace[0]); | |
38 | ||
39 | // ------------------------------------------------------------------------ | |
40 | // Attributes | |
41 | // ------------------------------------------------------------------------ | |
42 | ||
43 | /** The traces of interest */ | |
44 | private final ITmfTrace[] fTraces; | |
45 | ||
46 | // ------------------------------------------------------------------------ | |
47 | // Constructors | |
48 | // ------------------------------------------------------------------------ | |
49 | ||
50 | /** | |
51 | * Default constructor | |
52 | */ | |
53 | public TmfTraceFilter() { | |
54 | this(ALL_TRACES); | |
55 | } | |
56 | ||
57 | /** | |
58 | * Standard constructor | |
59 | * | |
60 | * @param traces the traces of interest | |
61 | */ | |
62 | public TmfTraceFilter(ITmfTrace[] traces) { | |
63 | fTraces = traces; | |
64 | } | |
65 | ||
66 | /** | |
67 | * Copy constructor | |
68 | * | |
69 | * @param other the other filter | |
70 | */ | |
71 | public TmfTraceFilter(TmfTraceFilter other) { | |
72 | fTraces = other.fTraces; | |
73 | } | |
74 | ||
75 | // ------------------------------------------------------------------------ | |
76 | // Getters | |
77 | // ------------------------------------------------------------------------ | |
78 | ||
79 | /** | |
80 | * @return the filter traces | |
81 | */ | |
82 | public ITmfTrace[] getTraces() { | |
83 | return fTraces; | |
84 | } | |
85 | ||
86 | // ------------------------------------------------------------------------ | |
87 | // ITmfFilter | |
88 | // ------------------------------------------------------------------------ | |
89 | ||
90 | /* (non-Javadoc) | |
91 | * @see org.eclipse.linuxtools.tmf.core.filter.ITmfFilter#matches(org.eclipse.linuxtools.tmf.core.event.ITmfEvent) | |
92 | */ | |
93 | @Override | |
94 | public boolean matches(ITmfEvent event) { | |
95 | // The empty set is the universal element | |
96 | if (fTraces.length == 0) { | |
97 | return true; | |
98 | } | |
99 | for (ITmfTrace trace : fTraces) { | |
100 | if (event.getTrace() == trace) { | |
101 | return true; | |
102 | } | |
103 | } | |
104 | return false; | |
105 | } | |
106 | ||
107 | // ------------------------------------------------------------------------ | |
108 | // Object | |
109 | // ------------------------------------------------------------------------ | |
110 | ||
111 | @Override | |
112 | public int hashCode() { | |
113 | final int prime = 31; | |
114 | int result = 1; | |
115 | result = prime * result + Arrays.hashCode(fTraces); | |
116 | return result; | |
117 | } | |
118 | ||
119 | @Override | |
120 | public boolean equals(Object obj) { | |
121 | if (this == obj) { | |
122 | return true; | |
123 | } | |
124 | if (obj == null) { | |
125 | return false; | |
126 | } | |
127 | if (!(obj instanceof TmfTraceFilter)) { | |
128 | return false; | |
129 | } | |
130 | TmfTraceFilter other = (TmfTraceFilter) obj; | |
131 | if (!Arrays.equals(fTraces, other.fTraces)) { | |
132 | return false; | |
133 | } | |
134 | return true; | |
135 | } | |
136 | ||
137 | @Override | |
138 | @SuppressWarnings("nls") | |
139 | public String toString() { | |
140 | return "TmfTraceFilter [fTraces=" + Arrays.toString(fTraces) + "]"; | |
141 | } | |
142 | ||
143 | } |