This repository has been archived by the owner on Jan 9, 2019. It is now read-only.
forked from konatakun/powerbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Nameable.java
96 lines (86 loc) · 2.14 KB
/
Nameable.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
94
95
96
package org.powerbot.script;
import java.util.regex.Pattern;
/**
* Nameable
* An entity which has a name.
*/
public interface Nameable {
/**
* The name of the entity.
*
* @return the entity's name
*/
String name();
/**
* Query
* A base for queries that make use of {@link Nameable} entities.
*
* @param <T> the type of query to return for chaining
*/
interface Query<T> {
/**
* Selects the entities which have a name that matches any of the specified names into the query cache.
*
* @param names the valid names
* @return {@code this} for the purpose of method chaining
*/
T name(String... names);
/**
* Selects the entities which have a name that matches one of the specified action patterns into the query cache.
*
* @param names the valid patterns to check RegEx against
* @return {@code this} for the purpose of method chaining
*/
T name(Pattern... names);
/**
* Selects the entities which have a name that matches any of the specified nameables names into the query cache.
*
* @param names the valid nameables to check against
* @return {@code this} for the purpose of method chaining
*/
T name(Nameable... names);
}
/**
* Matcher
*/
class Matcher implements Filter<Nameable> {
private final String[] str;
private final Pattern[] regex;
public Matcher(final String... names) {
str = names;
regex = null;
}
public Matcher(final Nameable... names) {
regex = null;
this.str = new String[names.length];
for (int i = 0; i < names.length; i++) {
this.str[i] = names[i].name();
}
}
public Matcher(final Pattern... names) {
regex = names;
str = null;
}
@Override
public boolean accept(final Nameable nameable) {
final String n = nameable.name();
if (n == null || regex == null && str == null) {
return false;
}
if (regex != null) {
for (final Pattern pattern : regex) {
if (pattern != null && pattern.matcher(n).matches()) {
return true;
}
}
} else {
for (final String string : str) {
if (string != null && string.equalsIgnoreCase(n)) {
return true;
}
}
}
return false;
}
}
}