2 * Copyright (C) 2017 - EfficiOS Inc., Philippe Proulx <pproulx@efficios.com>
4 * This library is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License, version 2.1 only,
6 * as published by the Free Software Foundation.
8 * This library is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this library; if not, write to the Free Software Foundation,
15 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 package org
.lttng
.ust
.agent
;
20 import java
.util
.regex
.Pattern
;
23 * Class encapsulating an event name from the session daemon, and its
24 * corresponding {@link Pattern}. This allows referring back to the original
25 * event name, for example when we receive a disable command.
27 * @author Philippe Proulx
28 * @author Alexandre Montplaisir
30 class EventNamePattern
{
32 private final String originalEventName
;
35 * Note that two Patterns coming from the exact same String will not be
36 * equals()! As such, it would be confusing to make the pattern part of this
37 * class's equals/hashCode
39 private final transient Pattern pattern
;
41 public EventNamePattern(String eventName
) {
42 if (eventName
== null) {
43 throw new IllegalArgumentException();
46 originalEventName
= eventName
;
47 pattern
= patternFromEventName(eventName
);
50 public String
getEventName() {
51 return originalEventName
;
54 public Pattern
getPattern() {
59 public int hashCode() {
62 result
= prime
* result
+ originalEventName
.hashCode();
67 public boolean equals(Object obj
) {
74 if (getClass() != obj
.getClass()) {
77 EventNamePattern other
= (EventNamePattern
) obj
;
78 if (!originalEventName
.equals(other
.originalEventName
)) {
84 private static Pattern
patternFromEventName(String eventName
) {
86 * The situation here is that `\*` means a literal `*` in the event
87 * name, and `*` is a wildcard star. We check the event name one
88 * character at a time and create a list of tokens to be converter to
91 StringBuilder bigBuilder
= new StringBuilder("^");
92 StringBuilder smallBuilder
= new StringBuilder();
94 for (int i
= 0; i
< eventName
.length(); i
++) {
95 char c
= eventName
.charAt(i
);
99 /* Add current quoted builder's string if not empty. */
100 if (smallBuilder
.length() > 0) {
101 bigBuilder
.append(Pattern
.quote(smallBuilder
.toString()));
102 smallBuilder
.setLength(0);
105 /* Append the equivalent regex which is `.*`. */
106 bigBuilder
.append(".*");
110 /* We only escape `*` and `\` here. */
111 if (i
< (eventName
.length() - 1)) {
112 char nextChar
= eventName
.charAt(i
+ 1);
114 if (nextChar
== '*' || nextChar
== '\\') {
115 smallBuilder
.append(nextChar
);
117 smallBuilder
.append(c
);
118 smallBuilder
.append(nextChar
);
130 smallBuilder
.append(c
);
133 /* Add current quoted builder's string if not empty. */
134 if (smallBuilder
.length() > 0) {
135 bigBuilder
.append(Pattern
.quote(smallBuilder
.toString()));
138 bigBuilder
.append("$");
140 return Pattern
.compile(bigBuilder
.toString());