2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2017 EfficiOS Inc.
5 * Copyright (C) 2017 Philippe Proulx <pproulx@efficios.com>
8 package org
.lttng
.ust
.agent
;
10 import java
.util
.regex
.Pattern
;
13 * Class encapsulating an event name from the session daemon, and its
14 * corresponding {@link Pattern}. This allows referring back to the original
15 * event name, for example when we receive a disable command.
17 * @author Philippe Proulx
18 * @author Alexandre Montplaisir
20 class EventNamePattern
{
22 private final String originalEventName
;
25 * Note that two Patterns coming from the exact same String will not be
26 * equals()! As such, it would be confusing to make the pattern part of this
27 * class's equals/hashCode
29 private final transient Pattern pattern
;
31 public EventNamePattern(String eventName
) {
32 if (eventName
== null) {
33 throw new IllegalArgumentException();
36 originalEventName
= eventName
;
37 pattern
= patternFromEventName(eventName
);
40 public String
getEventName() {
41 return originalEventName
;
44 public Pattern
getPattern() {
49 public int hashCode() {
52 result
= prime
* result
+ originalEventName
.hashCode();
57 public boolean equals(Object obj
) {
64 if (getClass() != obj
.getClass()) {
67 EventNamePattern other
= (EventNamePattern
) obj
;
68 if (!originalEventName
.equals(other
.originalEventName
)) {
74 private static Pattern
patternFromEventName(String eventName
) {
76 * The situation here is that `\*` means a literal `*` in the event
77 * name, and `*` is a wildcard star. We check the event name one
78 * character at a time and create a list of tokens to be converter to
81 StringBuilder bigBuilder
= new StringBuilder("^");
82 StringBuilder smallBuilder
= new StringBuilder();
84 for (int i
= 0; i
< eventName
.length(); i
++) {
85 char c
= eventName
.charAt(i
);
89 /* Add current quoted builder's string if not empty. */
90 if (smallBuilder
.length() > 0) {
91 bigBuilder
.append(Pattern
.quote(smallBuilder
.toString()));
92 smallBuilder
.setLength(0);
95 /* Append the equivalent regex which is `.*`. */
96 bigBuilder
.append(".*");
100 /* We only escape `*` and `\` here. */
101 if (i
< (eventName
.length() - 1)) {
102 char nextChar
= eventName
.charAt(i
+ 1);
104 if (nextChar
== '*' || nextChar
== '\\') {
105 smallBuilder
.append(nextChar
);
107 smallBuilder
.append(c
);
108 smallBuilder
.append(nextChar
);
120 smallBuilder
.append(c
);
123 /* Add current quoted builder's string if not empty. */
124 if (smallBuilder
.length() > 0) {
125 bigBuilder
.append(Pattern
.quote(smallBuilder
.toString()));
128 bigBuilder
.append("$");
130 return Pattern
.compile(bigBuilder
.toString());