forked from konatakun/powerbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Actionable.java
93 lines (85 loc) · 1.99 KB
/
Actionable.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package org.powerbot.script;
import java.util.regex.Pattern;
/**
* Actionable
* An entity which holds an array of actions.
*/
public interface Actionable extends Interactive {
/**
* The current actions for the entity.
*
* @return the current entity actions
*/
String[] actions();
/**
* Query
* A base for queries that make use of {@link Actionable} entities.
*
* @param <T> the type of query to return for chaining
*/
interface Query<T> {
/**
* Selects the entities which have one of the specified actions into the query cache.
*
* @param actions the valid actions
* @return {@code this} for the purpose of method chaining
*/
T action(String... actions);
/**
* Selects the entities which have any action which matches one of the specified action patterns into the query cache.
*
* @param actions the valid patterns to check RegEx against
* @return {@code this} for the purpose of method chaining
*/
T action(Pattern... actions);
}
/**
* Matcher
*/
class Matcher implements Filter<Actionable> {
private final String[] str;
private final Pattern[] regex;
public Matcher(final String... actions) {
str = actions;
regex = null;
}
public Matcher(final Pattern... actions) {
regex = actions;
str = null;
}
@Override
public boolean accept(final Actionable actionable) {
final String[] actions = actionable.actions();
if (actions == null) {
return false;
}
if (regex == null && str == null) {
return false;
}
if (regex != null) {
for (final String action : actions) {
if (action == null) {
continue;
}
for (final Pattern pattern : regex) {
if (pattern.matcher(action).matches()) {
return true;
}
}
}
} else {
for (final String action : actions) {
if (action == null) {
continue;
}
for (final String string : str) {
if (action.equalsIgnoreCase(string)) {
return true;
}
}
}
}
return false;
}
}
}