TMF: Make the ITmfEventAspect#resolve nullable
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / filter / model / TmfFilterMatchesNode.java
1 /*******************************************************************************
2 * Copyright (c) 2010, 2013 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 * Patrick Tasse - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.tmf.core.filter.model;
14
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.regex.Pattern;
18 import java.util.regex.PatternSyntaxException;
19
20 /**
21 * Filter node for the regex match
22 *
23 * @version 1.0
24 * @author Patrick Tasse
25 */
26 @SuppressWarnings("javadoc")
27 public abstract class TmfFilterMatchesNode extends TmfFilterTreeNode {
28
29 public static final String NOT_ATTR = "not"; //$NON-NLS-1$
30 public static final String REGEX_ATTR = "regex"; //$NON-NLS-1$
31
32 private boolean fNot = false;
33
34 private String fRegex;
35 private transient Pattern fPattern;
36
37 /**
38 * @param parent
39 * the parent node
40 */
41 public TmfFilterMatchesNode(ITmfFilterTreeNode parent) {
42 super(parent);
43 }
44
45 /**
46 * @return the NOT state
47 */
48 public boolean isNot() {
49 return fNot;
50 }
51
52 /**
53 * @param not
54 * the NOT state
55 */
56 public void setNot(boolean not) {
57 this.fNot = not;
58 }
59
60 /**
61 * @return the regular expression
62 */
63 public String getRegex() {
64 return fRegex;
65 }
66
67 /**
68 * @param regex
69 * the regular expression
70 */
71 public void setRegex(String regex) {
72 this.fRegex = regex;
73 if (regex != null) {
74 try {
75 this.fPattern = Pattern.compile(regex, Pattern.DOTALL);
76 } catch (PatternSyntaxException e) {
77 this.fPattern = null;
78 }
79 }
80 }
81
82 protected Pattern getPattern() {
83 return fPattern;
84 }
85
86 @Override
87 public List<String> getValidChildren() {
88 return new ArrayList<>(0);
89 }
90
91 @Override
92 public ITmfFilterTreeNode clone() {
93 TmfFilterMatchesNode clone = (TmfFilterMatchesNode) super.clone();
94 clone.setRegex(fRegex);
95 return clone;
96 }
97
98 /**
99 * @param pattern
100 * the rough regex pattern
101 * @return the compliant regex
102 */
103 public static String regexFix(String pattern) {
104 String ret = pattern;
105 // if the pattern does not contain one of the expressions .* !^
106 // (at the beginning) $ (at the end), then a .* is added at the
107 // beginning and at the end of the pattern
108 if (!(ret.indexOf(".*") >= 0 || ret.charAt(0) == '^' || ret.charAt(ret.length() - 1) == '$')) { //$NON-NLS-1$
109 ret = ".*" + ret + ".*"; //$NON-NLS-1$ //$NON-NLS-2$
110 }
111 return ret;
112 }
113
114 @Override
115 public int hashCode() {
116 final int prime = 31;
117 int result = super.hashCode();
118 result = prime * result + (fNot ? 1231 : 1237);
119 result = prime * result + ((fRegex == null) ? 0 : fRegex.hashCode());
120 return result;
121 }
122
123 @Override
124 public boolean equals(Object obj) {
125 if (this == obj) {
126 return true;
127 }
128 if (!super.equals(obj)) {
129 return false;
130 }
131 if (getClass() != obj.getClass()) {
132 return false;
133 }
134 TmfFilterMatchesNode other = (TmfFilterMatchesNode) obj;
135 if (fNot != other.fNot) {
136 return false;
137 }
138 if (fRegex == null) {
139 if (other.fRegex != null) {
140 return false;
141 }
142 } else if (!fRegex.equals(other.fRegex)) {
143 return false;
144 }
145 return true;
146 }
147 }
This page took 0.033416 seconds and 5 git commands to generate.