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 | ||
17 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; | |
18 | ||
19 | /** | |
20 | * Filter node that matches on an event field (using a field ID). | |
21 | * | |
22 | * @author Alexandre Montplaisir | |
23 | */ | |
24 | public final class TmfFilterMatchesFieldNode extends TmfFilterMatchesNode { | |
25 | ||
26 | /** Name/ID of this node */ | |
27 | public static final String NODE_NAME = "MATCHES"; //$NON-NLS-1$ | |
28 | /** The string attribute of this node type */ | |
29 | public static final String FIELD_ATTR = "field"; //$NON-NLS-1$ | |
30 | ||
31 | private String fField; | |
32 | ||
33 | /** | |
34 | * Constructor | |
35 | * | |
36 | * @param parent | |
37 | * The parent node | |
38 | */ | |
39 | public TmfFilterMatchesFieldNode(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) { | |
54 | return false ^ isNot; | |
55 | } | |
56 | ||
57 | Object value = getFieldValue(event, fField); | |
58 | if (value == null) { | |
59 | return false ^ isNot; | |
60 | } | |
61 | String valueString = value.toString(); | |
62 | ||
63 | return pattern.matcher(valueString).matches() ^ isNot; | |
64 | } | |
65 | ||
66 | /** | |
67 | * @return the field name | |
68 | */ | |
69 | public String getField() { | |
70 | return fField; | |
71 | } | |
72 | ||
73 | /** | |
74 | * @param field | |
75 | * the field name | |
76 | */ | |
77 | public void setField(String field) { | |
78 | this.fField = field; | |
79 | } | |
80 | ||
81 | @Override | |
82 | public ITmfFilterTreeNode clone() { | |
83 | TmfFilterMatchesFieldNode clone = (TmfFilterMatchesFieldNode) super.clone(); | |
84 | clone.fField = fField; | |
85 | return clone; | |
86 | } | |
87 | ||
88 | @Override | |
89 | public String toString() { | |
90 | return fField + (isNot() ? " not" : "") + " matches \"" + getRegex() + "\""; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ | |
91 | } | |
92 | ||
93 | @Override | |
94 | public int hashCode() { | |
95 | final int prime = 31; | |
96 | int result = super.hashCode(); | |
97 | result = prime * result + ((fField == null) ? 0 : fField.hashCode()); | |
98 | return result; | |
99 | } | |
100 | ||
101 | @Override | |
102 | public boolean equals(Object obj) { | |
103 | if (this == obj) { | |
104 | return true; | |
105 | } | |
106 | if (!super.equals(obj)) { | |
107 | return false; | |
108 | } | |
109 | if (getClass() != obj.getClass()) { | |
110 | return false; | |
111 | } | |
112 | TmfFilterMatchesFieldNode other = (TmfFilterMatchesFieldNode) obj; | |
113 | if (fField == null) { | |
114 | if (other.fField != null) { | |
115 | return false; | |
116 | } | |
117 | } else if (!fField.equals(other.fField)) { | |
118 | return false; | |
119 | } | |
120 | return true; | |
121 | } | |
122 | ||
123 | } |