|
| 1 | +package com.nfl.glitr.registry; |
| 2 | + |
| 3 | +import com.nfl.glitr.exception.GlitrException; |
| 4 | +import graphql.schema.GraphQLType; |
| 5 | + |
| 6 | +import java.util.Collection; |
| 7 | +import java.util.Iterator; |
| 8 | +import java.util.Map; |
| 9 | +import java.util.Set; |
| 10 | +import java.util.concurrent.ConcurrentHashMap; |
| 11 | +import java.util.concurrent.ConcurrentMap; |
| 12 | + |
| 13 | +/** |
| 14 | + * A map implementation to sync the two kinds of registries currently in {@link com.nfl.glitr.registry.TypeRegistry}. |
| 15 | + * Syncs happen dynamically, keeping the nameRegistry appraised of classRegistry additions to ensure unique GraphQLTypes |
| 16 | + */ |
| 17 | +public class GlitrTypeMap implements ConcurrentMap { |
| 18 | + |
| 19 | + private final Map<Class, GraphQLType> classRegistry = new ConcurrentHashMap<>(); |
| 20 | + private final Map<String, GraphQLType> nameRegistry = new ConcurrentHashMap<>(); |
| 21 | + |
| 22 | + |
| 23 | + @Override |
| 24 | + public Object getOrDefault(Object key, Object defaultValue) { |
| 25 | + if (isClass(key)) { |
| 26 | + return classRegistry.getOrDefault(key, (GraphQLType) defaultValue); |
| 27 | + } else if (isString(key)) { |
| 28 | + return nameRegistry.getOrDefault(key, (GraphQLType) defaultValue); |
| 29 | + } |
| 30 | + throw new GlitrException("Unsupported type passed as key to GlitrTypeMap"); |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public Object putIfAbsent(Object key, Object value) { |
| 35 | + if (isClass(key)) { |
| 36 | + nameRegistry.putIfAbsent(((Class) key).getSimpleName(), (GraphQLType) value); |
| 37 | + return classRegistry.putIfAbsent((Class) key, (GraphQLType) value); |
| 38 | + } else if (isString(key)) { |
| 39 | + return nameRegistry.putIfAbsent((String) key, (GraphQLType) value); |
| 40 | + } |
| 41 | + throw new GlitrException("Unsupported type passed as key to GlitrTypeMap"); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public boolean remove(Object key, Object value) { |
| 46 | + if (isClass(key)) { |
| 47 | + nameRegistry.remove(((Class) key).getSimpleName(), value); |
| 48 | + return classRegistry.remove(key, value); |
| 49 | + } else if (isString(key)) { |
| 50 | + return nameRegistry.remove(key, value); |
| 51 | + } |
| 52 | + throw new GlitrException("Unsupported type passed as key to GlitrTypeMap"); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public boolean replace(Object key, Object oldValue, Object newValue) { |
| 57 | + if (isClass(key)) { |
| 58 | + nameRegistry.replace(((Class) key).getSimpleName(), (GraphQLType) oldValue, (GraphQLType) newValue); |
| 59 | + return classRegistry.replace((Class) key, (GraphQLType) oldValue, (GraphQLType) newValue); |
| 60 | + } else if (isString(key)) { |
| 61 | + return nameRegistry.replace((String) key, (GraphQLType) oldValue, (GraphQLType) newValue); |
| 62 | + } |
| 63 | + throw new GlitrException("Unsupported type passed as key to GlitrTypeMap"); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public Object replace(Object key, Object value) { |
| 68 | + if (isClass(key)) { |
| 69 | + nameRegistry.replace(((Class) key).getSimpleName(), (GraphQLType) value); |
| 70 | + return classRegistry.replace((Class) key, (GraphQLType) value); |
| 71 | + } else if (isString(key)) { |
| 72 | + return nameRegistry.replace((String) key, (GraphQLType) value); |
| 73 | + } |
| 74 | + throw new GlitrException("Unsupported type passed as key to GlitrTypeMap"); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public boolean isEmpty() { |
| 79 | + return nameRegistry.isEmpty(); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public boolean containsKey(Object key) { |
| 84 | + if (isClass(key)) { |
| 85 | + return classRegistry.containsKey(key); |
| 86 | + } else if (isString(key)) { |
| 87 | + return nameRegistry.containsKey(key); |
| 88 | + } |
| 89 | + throw new GlitrException("Unsupported type passed as key to GlitrTypeMap"); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public boolean containsValue(Object value) { |
| 94 | + return nameRegistry.containsValue(value); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public Object get(Object key) { |
| 99 | + if (isClass(key)) { |
| 100 | + return classRegistry.get(key); |
| 101 | + } else if (isString(key)) { |
| 102 | + return nameRegistry.get(key); |
| 103 | + } |
| 104 | + throw new GlitrException("Unsupported type passed as key to GlitrTypeMap"); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public Object put(Object key, Object value) { |
| 109 | + if (isClass(key)) { |
| 110 | + nameRegistry.put(((Class) key).getSimpleName(), (GraphQLType) value); |
| 111 | + return classRegistry.put((Class) key, (GraphQLType) value); |
| 112 | + } else if (isString(key)) { |
| 113 | + return nameRegistry.put((String) key, (GraphQLType) value); |
| 114 | + } |
| 115 | + throw new GlitrException("Unsupported type passed as key to GlitrTypeMap"); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public Object remove(Object key) { |
| 120 | + if (isClass(key)) { |
| 121 | + nameRegistry.remove(((Class) key).getSimpleName()); |
| 122 | + return classRegistry.remove(key); |
| 123 | + } else if (isString(key)) { |
| 124 | + return nameRegistry.remove(key); |
| 125 | + } |
| 126 | + throw new GlitrException("Unsupported type passed as key to GlitrTypeMap"); |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public void clear() { |
| 131 | + classRegistry.clear(); |
| 132 | + nameRegistry.clear(); |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public int size() { |
| 137 | + return nameRegistry.size(); |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public void putAll(Map m) { |
| 142 | + Set set = m.keySet(); |
| 143 | + Object next = set.iterator().next(); |
| 144 | + if (isClass(next)) { |
| 145 | + for (Object o : m.entrySet()) { |
| 146 | + Entry pair = (Entry) o; |
| 147 | + nameRegistry.put(((Class) pair.getKey()).getSimpleName(), (GraphQLType) pair.getValue()); |
| 148 | + classRegistry.put((Class) pair.getKey(), (GraphQLType) pair.getValue()); |
| 149 | + } |
| 150 | + } else if (isString(next)) { |
| 151 | + nameRegistry.putAll(m); |
| 152 | + } |
| 153 | + throw new GlitrException("Unsupported type passed as key to GlitrTypeMap"); |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public Collection values() { |
| 158 | + return nameRegistry.values(); |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public Set keySet() { |
| 163 | + throw new GlitrException("Unsupported method, GlitrTypeMap does not support this method to ensure expected return types"); |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public Set<Entry> entrySet() { |
| 168 | + throw new GlitrException("Unsupported method, GlitrTypeMap does not support this method to ensure expected return types"); |
| 169 | + } |
| 170 | + |
| 171 | + public Set ClassKeySet() { |
| 172 | + return classRegistry.keySet(); |
| 173 | + } |
| 174 | + |
| 175 | + public Set NameKeySet() { |
| 176 | + return nameRegistry.keySet(); |
| 177 | + } |
| 178 | + |
| 179 | + public Set<Entry<Class, GraphQLType>> ClassEntrySet() { |
| 180 | + return classRegistry.entrySet(); |
| 181 | + } |
| 182 | + |
| 183 | + public Set<Entry<String, GraphQLType>> NameEntrySet() { |
| 184 | + return nameRegistry.entrySet(); |
| 185 | + } |
| 186 | + |
| 187 | + private boolean isClass(Object obj) { |
| 188 | + return obj instanceof Class; |
| 189 | + } |
| 190 | + |
| 191 | + private boolean isString(Object obj) { |
| 192 | + return obj instanceof String; |
| 193 | + } |
| 194 | +} |
0 commit comments