Commit | Line | Data |
---|---|---|
d9b1b4ed AM |
1 | /******************************************************************************* |
2 | * Copyright (c) 2014 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 | * Alexandre Montplaisir - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.tracecompass.tmf.core.filter.model; | |
14 | ||
15 | import java.util.regex.Pattern; | |
16 | ||
2a28075c | 17 | import org.eclipse.tracecompass.common.core.NonNullUtils; |
d9b1b4ed AM |
18 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; |
19 | import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect; | |
20 | ||
21 | /** | |
22 | * Filter node that matches on an event aspect. | |
23 | * | |
24 | * @author Alexandre Montplaisir | |
25 | */ | |
26 | public final class TmfFilterMatchesAspectNode extends TmfFilterMatchesNode { | |
27 | ||
28 | /** Name/ID of this node */ | |
29 | public static final String NODE_NAME = "MATCHES_ASPECT"; //$NON-NLS-1$ | |
30 | ||
31 | private ITmfEventAspect fEventAspect; | |
32 | ||
33 | /** | |
34 | * Constructor | |
35 | * | |
36 | * @param parent | |
37 | * The parent node | |
38 | */ | |
39 | public TmfFilterMatchesAspectNode(ITmfFilterTreeNode parent) { | |
40 | super(parent); | |
41 | } | |
42 | ||
43 | @Override | |
44 | public String getNodeName() { | |
45 | return NODE_NAME; | |
46 | } | |
47 | ||
48 | @Override | |
49 | public boolean matches(ITmfEvent event) { | |
50 | Pattern pattern = getPattern(); | |
51 | boolean isNot = isNot(); | |
52 | ||
53 | if (pattern == null || event == null) { | |
54 | return false ^ isNot; | |
55 | } | |
2a28075c | 56 | String value = NonNullUtils.nullToEmptyString(fEventAspect.resolve(event)); |
d9b1b4ed AM |
57 | return pattern.matcher(value).matches() ^ isNot; |
58 | } | |
59 | ||
60 | /** | |
61 | * @return The event aspect of this filter | |
62 | */ | |
63 | public ITmfEventAspect getEventAspect() { | |
64 | return fEventAspect; | |
65 | } | |
66 | ||
67 | /** | |
68 | * @param aspect | |
69 | * The event aspect to assign to this filter | |
70 | */ | |
71 | public void setEventAspect(ITmfEventAspect aspect) { | |
72 | this.fEventAspect = aspect; | |
73 | } | |
74 | ||
75 | @Override | |
76 | public ITmfFilterTreeNode clone() { | |
77 | TmfFilterMatchesAspectNode clone = (TmfFilterMatchesAspectNode) super.clone(); | |
78 | clone.fEventAspect = fEventAspect; | |
79 | return clone; | |
80 | } | |
81 | ||
82 | @Override | |
83 | public String toString() { | |
84 | return fEventAspect.getName() + (isNot() ? " not" : "") + " matches \"" + getRegex() + "\""; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ | |
85 | } | |
86 | ||
87 | @Override | |
88 | public int hashCode() { | |
89 | final int prime = 31; | |
90 | int result = super.hashCode(); | |
91 | result = prime * result + ((fEventAspect == null) ? 0 : fEventAspect.hashCode()); | |
92 | return result; | |
93 | } | |
94 | ||
95 | @Override | |
96 | public boolean equals(Object obj) { | |
97 | if (this == obj) { | |
98 | return true; | |
99 | } | |
100 | if (!super.equals(obj)) { | |
101 | return false; | |
102 | } | |
103 | if (getClass() != obj.getClass()) { | |
104 | return false; | |
105 | } | |
106 | TmfFilterMatchesAspectNode other = (TmfFilterMatchesAspectNode) obj; | |
107 | if (fEventAspect == null) { | |
108 | if (other.fEventAspect != null) { | |
109 | return false; | |
110 | } | |
111 | } else if (!fEventAspect.equals(other.fEventAspect)) { | |
112 | return false; | |
113 | } | |
114 | return true; | |
115 | } | |
116 | ||
117 | ||
118 | } |