|
| 1 | +/* |
| 2 | + * Copyright (c) 2016, Kevin Phoenix |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * |
| 8 | + * 1. Redistributions of source code must retain the above copyright notice, this |
| 9 | + * list of conditions and the following disclaimer. |
| 10 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | + * this list of conditions and the following disclaimer in the documentation |
| 12 | + * and/or other materials provided with the distribution. |
| 13 | + * |
| 14 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 15 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 16 | + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 17 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 18 | + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 19 | + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 20 | + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 21 | + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 23 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | + */ |
| 25 | + |
| 26 | +package in.twizmwaz.cardinal.module.id; |
| 27 | + |
| 28 | +import com.google.common.collect.Lists; |
| 29 | +import in.twizmwaz.cardinal.match.Match; |
| 30 | +import in.twizmwaz.cardinal.module.AbstractModule; |
| 31 | +import in.twizmwaz.cardinal.module.ModuleEntry; |
| 32 | +import lombok.NonNull; |
| 33 | + |
| 34 | +import java.util.HashMap; |
| 35 | +import java.util.List; |
| 36 | +import java.util.Map; |
| 37 | +import java.util.UUID; |
| 38 | + |
| 39 | +@ModuleEntry |
| 40 | +public class IdModule extends AbstractModule { |
| 41 | + |
| 42 | + private static IdModule idModule; |
| 43 | + private final Map<Match, Map<String, Object>> ids = new HashMap<>(); |
| 44 | + |
| 45 | + public IdModule() { |
| 46 | + IdModule.idModule = this; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public boolean loadMatch(@NonNull Match match) { |
| 51 | + ids.put(match, new HashMap<>()); |
| 52 | + return true; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public void clearMatch(@NonNull Match match) { |
| 57 | + if (ids.containsKey(match)) { |
| 58 | + ids.remove(match); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Gets the id module, used for easy access from outside classes. |
| 64 | + * @return the IdModule. |
| 65 | + */ |
| 66 | + public static IdModule get() { |
| 67 | + return idModule; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Adds an object to a match, only if the id is not already present. |
| 72 | + * @param match The match to add the object to. |
| 73 | + * @param id The id to add the object with, can't be duplicated. |
| 74 | + * @param object The object to add. |
| 75 | + * @return if the object was successfully added. |
| 76 | + */ |
| 77 | + public boolean add(Match match, String id, Object object) { |
| 78 | + return add(match, id, object, false); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * 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. |
| 83 | + * @param match The match to add the object to. |
| 84 | + * @param id The id to add the object with, can't be duplicated. |
| 85 | + * @param object The object to add. |
| 86 | + * @param force Add the object with a random id if the id is null or already in use. |
| 87 | + * @return if the object was successfully added with the provided id. |
| 88 | + */ |
| 89 | + public boolean add(Match match, String id, Object object, boolean force) { |
| 90 | + if (id != null && !ids.get(match).containsKey(id)) { |
| 91 | + ids.get(match).put(id, object); |
| 92 | + return true; |
| 93 | + } else if (force) { |
| 94 | + add(match, UUID.randomUUID().toString(), object, true); |
| 95 | + } |
| 96 | + return false; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Get an object with the given id. |
| 101 | + * @param match The match the object belongs to. |
| 102 | + * @param id The id of the object. |
| 103 | + * @param clazz The class of the object you want to retrieve. |
| 104 | + * @param <T> The object type. |
| 105 | + * @return The object if id is present and it's the correct type, null otherwise. |
| 106 | + */ |
| 107 | + public <T> T get(Match match, String id, Class<T> clazz) { |
| 108 | + return get(match, id, clazz, false); |
| 109 | + } |
| 110 | + |
| 111 | + public <T> T get(Match match, String id, Class<T> clazz, boolean caseSensitive) { |
| 112 | + if (ids.get(match).containsKey(id)) { |
| 113 | + Object obj = ids.get(match).get(id); |
| 114 | + if (clazz.isInstance(obj)) { |
| 115 | + return (T) obj; |
| 116 | + } |
| 117 | + } |
| 118 | + if (!caseSensitive) { |
| 119 | + return null; |
| 120 | + } |
| 121 | + Map<String, T> map = getMap(match, clazz); |
| 122 | + |
| 123 | + for (String idVal : map.keySet()) { |
| 124 | + if (idVal.replaceAll(" ", "").equalsIgnoreCase(id.replaceAll(" ", ""))) { |
| 125 | + return map.get(idVal); |
| 126 | + } |
| 127 | + } |
| 128 | + for (String idVal : map.keySet()) { |
| 129 | + if (idVal.replaceAll(" ", "").toLowerCase().startsWith(id.replaceAll(" ", "").toLowerCase())) { |
| 130 | + return map.get(idVal); |
| 131 | + } |
| 132 | + } |
| 133 | + for (String idVal : map.keySet()) { |
| 134 | + if (idVal.replaceAll(" ", "-").equalsIgnoreCase(id.replaceAll(" ", "-"))) { |
| 135 | + return map.get(idVal); |
| 136 | + } |
| 137 | + } |
| 138 | + for (String idVal : map.keySet()) { |
| 139 | + if (idVal.replaceAll(" ", "-").toLowerCase().startsWith(id.replaceAll(" ", "-").toLowerCase())) { |
| 140 | + return map.get(idVal); |
| 141 | + } |
| 142 | + } |
| 143 | + return null; |
| 144 | + } |
| 145 | + |
| 146 | + |
| 147 | + /** |
| 148 | + * Get a list of all objects of the given class |
| 149 | + * @param match The match the objects belong to. |
| 150 | + * @param clazz The class of the objects you want to retrieve. |
| 151 | + * @param <T> The object type. |
| 152 | + * @return A List with all the objects of that type. |
| 153 | + */ |
| 154 | + public <T> List<T> getList(Match match, Class<T> clazz) { |
| 155 | + List<T> result = Lists.newArrayList(); |
| 156 | + for (Object obj : ids.get(match).values()) { |
| 157 | + if (clazz.isInstance(obj)) { |
| 158 | + result.add((T) obj); |
| 159 | + } |
| 160 | + } |
| 161 | + return result; |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Get a id object map for all objects of the given class |
| 166 | + * @param match The match the objects belong to. |
| 167 | + * @param clazz The class of the objects you want to retrieve. |
| 168 | + * @param <T> The object type. |
| 169 | + * @return A map with all the objects of that type and their id's. |
| 170 | + */ |
| 171 | + public <T> Map<String, T> getMap(Match match, Class<T> clazz) { |
| 172 | + Map<String, T> result = new HashMap<>(); |
| 173 | + for (Map.Entry<String, Object> entry : ids.get(match).entrySet()) { |
| 174 | + if (clazz.isInstance(entry.getValue())) { |
| 175 | + result.put(entry.getKey(), (T) entry.getValue()); |
| 176 | + } |
| 177 | + } |
| 178 | + return result; |
| 179 | + } |
| 180 | + |
| 181 | +} |
0 commit comments