| 1 | package ezvcard.util; |
| 2 | |
| 3 | import java.lang.reflect.Field; |
| 4 | import java.lang.reflect.Modifier; |
| 5 | import java.util.ArrayList; |
| 6 | import java.util.Collection; |
| 7 | import java.util.Collections; |
| 8 | |
| 9 | /* |
| 10 | Copyright (c) 2012-2023, Michael Angstadt |
| 11 | All rights reserved. |
| 12 | |
| 13 | Redistribution and use in source and binary forms, with or without |
| 14 | modification, are permitted provided that the following conditions are met: |
| 15 | |
| 16 | 1. Redistributions of source code must retain the above copyright notice, this |
| 17 | list of conditions and the following disclaimer. |
| 18 | 2. Redistributions in binary form must reproduce the above copyright notice, |
| 19 | this list of conditions and the following disclaimer in the documentation |
| 20 | and/or other materials provided with the distribution. |
| 21 | |
| 22 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 23 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 24 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 25 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 26 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 27 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 28 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 29 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 30 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | */ |
| 33 | |
| 34 | /** |
| 35 | * <p> |
| 36 | * Manages objects that are like enums in that they are constant, but unlike |
| 37 | * enums in that new instances can be created during runtime. This class ensures |
| 38 | * that all instances of a class are unique, so they can be safely compared |
| 39 | * using "==" (provided their constructors are private). |
| 40 | * </p> |
| 41 | * <p> |
| 42 | * This class awkwardly mimics the "case class" feature in Scala. |
| 43 | * </p> |
| 44 | * <p> |
| 45 | * <b>Example:</b> |
| 46 | * </p> |
| 47 | * <pre class="brush:java"> |
| 48 | * public class Color { |
| 49 | * public static final CaseClasses<Color, String> VALUES = new ColorCaseClasses(); |
| 50 | * public static final Color RED = new Color("red"); |
| 51 | * public static final Color GREEN = new Color("green"); |
| 52 | * public static final Color BLUE = new Color("blue"); |
| 53 | * |
| 54 | * private final String name; |
| 55 | * |
| 56 | * // Constructor should be PRIVATE in order to prevent users from |
| 57 | * // instantiating their own objects and circumventing the CaseClasses |
| 58 | * // object. |
| 59 | * private Color(String name) { |
| 60 | * this.name = name; |
| 61 | * } |
| 62 | * |
| 63 | * public String getName() { |
| 64 | * return name; |
| 65 | * } |
| 66 | * |
| 67 | * // CaseClasses implementation is an inner class because the Color |
| 68 | * // constructor is private. |
| 69 | * private static class ColorCaseClasses extends CaseClasses<Color, String> { |
| 70 | * public ColorCaseClasses() { |
| 71 | * super(Color.class); |
| 72 | * } |
| 73 | * |
| 74 | * @Override |
| 75 | * protected Color create(String value) { |
| 76 | * return new Color(value); |
| 77 | * } |
| 78 | * |
| 79 | * @Override |
| 80 | * protected boolean matches(Color object, String value) { |
| 81 | * return object.getName().equalsIgnoreCase(value); |
| 82 | * } |
| 83 | * } |
| 84 | * } |
| 85 | * |
| 86 | * public class Test { |
| 87 | * @Test |
| 88 | * public void test() { |
| 89 | * assertTrue(Color.RED == Color.VALUES.find("Red")); |
| 90 | * assertTrue(Color.RED == Color.VALUES.get("Red")); |
| 91 | * |
| 92 | * assertNull(Color.VALUES.find("purple")); |
| 93 | * Color purple = Color.VALUES.get("purple"); |
| 94 | * assertEquals("purple", purple.getName()); |
| 95 | * assertTrue(purple == Color.VALUES.get("Purple")); |
| 96 | * } |
| 97 | * } |
| 98 | * </pre> |
| 99 | * @author Michael Angstadt |
| 100 | * |
| 101 | * @param <T> the case class |
| 102 | * @param <V> the value that the class holds (e.g. String) |
| 103 | */ |
| 104 | public abstract class CaseClasses<T, V> { |
| 105 | protected final Class<T> clazz; |
| 106 | private volatile Collection<T> preDefined = null; |
| 107 | private Collection<T> runtimeDefined = null; |
| 108 | |
| 109 | /** |
| 110 | * Creates a new case class collection. |
| 111 | * @param clazz the case class |
| 112 | */ |
| 113 | public CaseClasses(Class<T> clazz) { |
| 114 | this.clazz = clazz; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Creates a new instance of the case class. |
| 119 | * @param value the value to give the instance |
| 120 | * @return the new instance |
| 121 | */ |
| 122 | protected abstract T create(V value); |
| 123 | |
| 124 | /** |
| 125 | * Determines if a case object is "equal to" the given value. |
| 126 | * @param object the case object |
| 127 | * @param value the value |
| 128 | * @return true if it matches, false if not |
| 129 | */ |
| 130 | protected abstract boolean matches(T object, V value); |
| 131 | |
| 132 | /** |
| 133 | * Searches for a case object by value, only looking at the case class' |
| 134 | * static constants (does not search runtime-defined constants). |
| 135 | * @param value the value |
| 136 | * @return the object or null if one wasn't found |
| 137 | */ |
| 138 | public T find(V value) { |
| 139 | checkInit(); |
| 140 | |
| 141 | for (T obj : preDefined) { |
| 142 | if (matches(obj, value)) { |
| 143 | return obj; |
| 144 | } |
| 145 | } |
| 146 | return null; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Searches for a case object by value, creating a new object if one cannot |
| 151 | * be found. |
| 152 | * @param value the value |
| 153 | * @return the object |
| 154 | */ |
| 155 | public T get(V value) { |
| 156 | T found = find(value); |
| 157 | if (found != null) { |
| 158 | return found; |
| 159 | } |
| 160 | |
| 161 | synchronized (runtimeDefined) { |
| 162 | for (T obj : runtimeDefined) { |
| 163 | if (matches(obj, value)) { |
| 164 | return obj; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | T created = create(value); |
| 169 | runtimeDefined.add(created); |
| 170 | return created; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Gets all the static constants of the case class (does not include |
| 176 | * runtime-defined constants). |
| 177 | * @return all static constants |
| 178 | */ |
| 179 | public Collection<T> all() { |
| 180 | checkInit(); |
| 181 | return preDefined; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Checks to see if this class's fields were initialized yet, and |
| 186 | * initializes them if they haven't been initialized. This method is |
| 187 | * thread-safe. |
| 188 | */ |
| 189 | private void checkInit() { |
| 190 | if (preDefined == null) { |
| 191 | synchronized (this) { |
| 192 | //"double check idiom" (Bloch p.283) |
| 193 | if (preDefined == null) { |
| 194 | init(); |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Initializes this class's fields. |
| 202 | */ |
| 203 | private void init() { |
| 204 | Collection<T> preDefined = new ArrayList<>(); |
| 205 | for (Field field : clazz.getFields()) { |
| 206 | if (!isPreDefinedField(field)) { |
| 207 | continue; |
| 208 | } |
| 209 | |
| 210 | try { |
| 211 | Object obj = field.get(null); |
| 212 | if (obj != null) { |
| 213 | T c = clazz.cast(obj); |
| 214 | preDefined.add(c); |
| 215 | } |
| 216 | } catch (Exception e) { |
| 217 | //reflection error |
| 218 | //should never be thrown because we check for "public static" and the correct type |
| 219 | throw new RuntimeException(e); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | runtimeDefined = new ArrayList<>(0); |
| 224 | this.preDefined = Collections.unmodifiableCollection(preDefined); |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Determines if a field should be treated as a predefined case object. |
| 229 | * @param field the field |
| 230 | * @return true if it's a predefined case object, false if not |
| 231 | */ |
| 232 | private boolean isPreDefinedField(Field field) { |
| 233 | int modifiers = field.getModifiers(); |
| 234 | |
| 235 | //@formatter:off |
| 236 | return |
| 237 | Modifier.isStatic(modifiers) && |
| 238 | Modifier.isPublic(modifiers) && |
| 239 | field.getDeclaringClass() == clazz && |
| 240 | field.getType() == clazz; |
| 241 | //@formatter:on |
| 242 | } |
| 243 | } |