-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathIdModule.java
More file actions
191 lines (172 loc) · 6.31 KB
/
IdModule.java
File metadata and controls
191 lines (172 loc) · 6.31 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
* Copyright (c) 2016, Kevin Phoenix
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package in.twizmwaz.cardinal.module.id;
import com.google.common.collect.Lists;
import in.twizmwaz.cardinal.match.Match;
import in.twizmwaz.cardinal.module.AbstractModule;
import in.twizmwaz.cardinal.module.ModuleEntry;
import lombok.NonNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@ModuleEntry
public class IdModule extends AbstractModule {
private static IdModule idModule;
private final Map<Match, Map<String, Object>> ids = new HashMap<>();
public IdModule() {
IdModule.idModule = this;
}
@Override
public boolean loadMatch(@NonNull Match match) {
ids.put(match, new HashMap<>());
return true;
}
@Override
public void clearMatch(@NonNull Match match) {
if (ids.containsKey(match)) {
ids.remove(match);
}
}
/**
* Gets the id module, used for easy access from outside classes.
* @return the IdModule.
*/
public static IdModule get() {
return idModule;
}
/**
* Adds an object to a match, only if the id is not already present.
* @param match The match to add the object to.
* @param id The id to add the object with, can't be duplicated.
* @param object The object to add.
* @return if the object was successfully added.
*/
public boolean add(Match match, String id, Object object) {
return add(match, id, object, false);
}
/**
* Adds an object to a match, only if the id is not already present or it will be added with a random id if force.
* @param match The match to add the object to.
* @param id The id to add the object with, can't be duplicated.
* @param object The object to add.
* @param force Add the object with a random id if the id is null or already in use.
* @return if the object was successfully added with the provided id.
*/
public boolean add(Match match, String id, Object object, boolean force) {
if (id != null && !ids.get(match).containsKey(id)) {
ids.get(match).put(id, object);
return true;
} else if (force) {
add(match, UUID.randomUUID().toString(), object, true);
}
return false;
}
/**
* Checks if the id could be added to the match
* @param match The match to try the object to.
* @param id The id to check.
* @return if the object isn't null and isn't duplicated.
*/
public boolean canAdd(Match match, String id) {
return id != null && !ids.get(match).containsKey(id);
}
/**
* Get an object with the given id.
* @param match The match the object belongs to.
* @param id The id of the object.
* @param clazz The class of the object you want to retrieve.
* @param <T> The object type.
* @return The object if id is present and it's the correct type, null otherwise.
*/
public <T> T get(Match match, String id, Class<T> clazz) {
return get(match, id, clazz, false);
}
public <T> T get(Match match, String id, Class<T> clazz, boolean caseSensitive) {
if (ids.get(match).containsKey(id)) {
Object obj = ids.get(match).get(id);
if (clazz.isInstance(obj)) {
return (T) obj;
}
}
if (!caseSensitive) {
return null;
}
Map<String, T> map = getMap(match, clazz);
for (String idVal : map.keySet()) {
if (idVal.replaceAll(" ", "").equalsIgnoreCase(id.replaceAll(" ", ""))) {
return map.get(idVal);
}
}
for (String idVal : map.keySet()) {
if (idVal.replaceAll(" ", "").toLowerCase().startsWith(id.replaceAll(" ", "").toLowerCase())) {
return map.get(idVal);
}
}
for (String idVal : map.keySet()) {
if (idVal.replaceAll(" ", "-").equalsIgnoreCase(id.replaceAll(" ", "-"))) {
return map.get(idVal);
}
}
for (String idVal : map.keySet()) {
if (idVal.replaceAll(" ", "-").toLowerCase().startsWith(id.replaceAll(" ", "-").toLowerCase())) {
return map.get(idVal);
}
}
return null;
}
/**
* Get a list of all objects of the given class
* @param match The match the objects belong to.
* @param clazz The class of the objects you want to retrieve.
* @param <T> The object type.
* @return A List with all the objects of that type.
*/
public <T> List<T> getList(Match match, Class<T> clazz) {
List<T> result = Lists.newArrayList();
for (Object obj : ids.get(match).values()) {
if (clazz.isInstance(obj)) {
result.add((T) obj);
}
}
return result;
}
/**
* Get a id object map for all objects of the given class
* @param match The match the objects belong to.
* @param clazz The class of the objects you want to retrieve.
* @param <T> The object type.
* @return A map with all the objects of that type and their id's.
*/
public <T> Map<String, T> getMap(Match match, Class<T> clazz) {
Map<String, T> result = new HashMap<>();
for (Map.Entry<String, Object> entry : ids.get(match).entrySet()) {
if (clazz.isInstance(entry.getValue())) {
result.put(entry.getKey(), (T) entry.getValue());
}
}
return result;
}
}