-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from nfl/better-connection-creation
adding a name registry to allow for reusable connections as long as the underlying GraphQL types are equal
- Loading branch information
Showing
10 changed files
with
379 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
package com.nfl.glitr.registry; | ||
|
||
import com.nfl.glitr.exception.GlitrException; | ||
import graphql.schema.GraphQLType; | ||
|
||
import java.util.Collection; | ||
import java.util.Iterator; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentMap; | ||
|
||
/** | ||
* A map implementation to sync the two kinds of registries currently in {@link com.nfl.glitr.registry.TypeRegistry}. | ||
* Syncs happen dynamically, keeping the nameRegistry appraised of classRegistry additions to ensure unique GraphQLTypes | ||
*/ | ||
public class GlitrTypeMap implements ConcurrentMap { | ||
|
||
private final Map<Class, GraphQLType> classRegistry = new ConcurrentHashMap<>(); | ||
private final Map<String, GraphQLType> nameRegistry = new ConcurrentHashMap<>(); | ||
|
||
|
||
@Override | ||
public Object getOrDefault(Object key, Object defaultValue) { | ||
if (isClass(key)) { | ||
return classRegistry.getOrDefault(key, (GraphQLType) defaultValue); | ||
} else if (isString(key)) { | ||
return nameRegistry.getOrDefault(key, (GraphQLType) defaultValue); | ||
} | ||
throw new GlitrException("Unsupported type passed as key to GlitrTypeMap"); | ||
} | ||
|
||
@Override | ||
public Object putIfAbsent(Object key, Object value) { | ||
if (isClass(key)) { | ||
nameRegistry.putIfAbsent(((Class) key).getSimpleName(), (GraphQLType) value); | ||
return classRegistry.putIfAbsent((Class) key, (GraphQLType) value); | ||
} else if (isString(key)) { | ||
return nameRegistry.putIfAbsent((String) key, (GraphQLType) value); | ||
} | ||
throw new GlitrException("Unsupported type passed as key to GlitrTypeMap"); | ||
} | ||
|
||
@Override | ||
public boolean remove(Object key, Object value) { | ||
if (isClass(key)) { | ||
nameRegistry.remove(((Class) key).getSimpleName(), value); | ||
return classRegistry.remove(key, value); | ||
} else if (isString(key)) { | ||
return nameRegistry.remove(key, value); | ||
} | ||
throw new GlitrException("Unsupported type passed as key to GlitrTypeMap"); | ||
} | ||
|
||
@Override | ||
public boolean replace(Object key, Object oldValue, Object newValue) { | ||
if (isClass(key)) { | ||
nameRegistry.replace(((Class) key).getSimpleName(), (GraphQLType) oldValue, (GraphQLType) newValue); | ||
return classRegistry.replace((Class) key, (GraphQLType) oldValue, (GraphQLType) newValue); | ||
} else if (isString(key)) { | ||
return nameRegistry.replace((String) key, (GraphQLType) oldValue, (GraphQLType) newValue); | ||
} | ||
throw new GlitrException("Unsupported type passed as key to GlitrTypeMap"); | ||
} | ||
|
||
@Override | ||
public Object replace(Object key, Object value) { | ||
if (isClass(key)) { | ||
nameRegistry.replace(((Class) key).getSimpleName(), (GraphQLType) value); | ||
return classRegistry.replace((Class) key, (GraphQLType) value); | ||
} else if (isString(key)) { | ||
return nameRegistry.replace((String) key, (GraphQLType) value); | ||
} | ||
throw new GlitrException("Unsupported type passed as key to GlitrTypeMap"); | ||
} | ||
|
||
@Override | ||
public boolean isEmpty() { | ||
return nameRegistry.isEmpty(); | ||
} | ||
|
||
@Override | ||
public boolean containsKey(Object key) { | ||
if (isClass(key)) { | ||
return classRegistry.containsKey(key); | ||
} else if (isString(key)) { | ||
return nameRegistry.containsKey(key); | ||
} | ||
throw new GlitrException("Unsupported type passed as key to GlitrTypeMap"); | ||
} | ||
|
||
@Override | ||
public boolean containsValue(Object value) { | ||
return nameRegistry.containsValue(value); | ||
} | ||
|
||
@Override | ||
public Object get(Object key) { | ||
if (isClass(key)) { | ||
return classRegistry.get(key); | ||
} else if (isString(key)) { | ||
return nameRegistry.get(key); | ||
} | ||
throw new GlitrException("Unsupported type passed as key to GlitrTypeMap"); | ||
} | ||
|
||
@Override | ||
public Object put(Object key, Object value) { | ||
if (isClass(key)) { | ||
nameRegistry.put(((Class) key).getSimpleName(), (GraphQLType) value); | ||
return classRegistry.put((Class) key, (GraphQLType) value); | ||
} else if (isString(key)) { | ||
return nameRegistry.put((String) key, (GraphQLType) value); | ||
} | ||
throw new GlitrException("Unsupported type passed as key to GlitrTypeMap"); | ||
} | ||
|
||
@Override | ||
public Object remove(Object key) { | ||
if (isClass(key)) { | ||
nameRegistry.remove(((Class) key).getSimpleName()); | ||
return classRegistry.remove(key); | ||
} else if (isString(key)) { | ||
return nameRegistry.remove(key); | ||
} | ||
throw new GlitrException("Unsupported type passed as key to GlitrTypeMap"); | ||
} | ||
|
||
@Override | ||
public void clear() { | ||
classRegistry.clear(); | ||
nameRegistry.clear(); | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return nameRegistry.size(); | ||
} | ||
|
||
@Override | ||
public void putAll(Map m) { | ||
Set set = m.keySet(); | ||
Object next = set.iterator().next(); | ||
if (isClass(next)) { | ||
for (Object o : m.entrySet()) { | ||
Entry pair = (Entry) o; | ||
nameRegistry.put(((Class) pair.getKey()).getSimpleName(), (GraphQLType) pair.getValue()); | ||
classRegistry.put((Class) pair.getKey(), (GraphQLType) pair.getValue()); | ||
} | ||
} else if (isString(next)) { | ||
nameRegistry.putAll(m); | ||
} | ||
throw new GlitrException("Unsupported type passed as key to GlitrTypeMap"); | ||
} | ||
|
||
@Override | ||
public Collection values() { | ||
return nameRegistry.values(); | ||
} | ||
|
||
@Override | ||
public Set keySet() { | ||
throw new GlitrException("Unsupported method, GlitrTypeMap does not support this method to ensure expected return types"); | ||
} | ||
|
||
@Override | ||
public Set<Entry> entrySet() { | ||
throw new GlitrException("Unsupported method, GlitrTypeMap does not support this method to ensure expected return types"); | ||
} | ||
|
||
public Set ClassKeySet() { | ||
return classRegistry.keySet(); | ||
} | ||
|
||
public Set NameKeySet() { | ||
return nameRegistry.keySet(); | ||
} | ||
|
||
public Set<Entry<Class, GraphQLType>> ClassEntrySet() { | ||
return classRegistry.entrySet(); | ||
} | ||
|
||
public Set<Entry<String, GraphQLType>> NameEntrySet() { | ||
return nameRegistry.entrySet(); | ||
} | ||
|
||
private boolean isClass(Object obj) { | ||
return obj instanceof Class; | ||
} | ||
|
||
private boolean isString(Object obj) { | ||
return obj instanceof String; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 31 additions & 29 deletions
60
src/test/groovy/com/nfl/glitr/data/query/additionalTypes/Cyborg.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,34 @@ | ||
package com.nfl.glitr.data.query.additionalTypes; | ||
|
||
public class Cyborg implements Person { | ||
public String getId() { | ||
return id; | ||
} | ||
|
||
public String getCodeName() { | ||
return codeName; | ||
} | ||
|
||
@Override | ||
public String getAge() { | ||
return this.age; | ||
} | ||
|
||
public void setId(String id) { | ||
this.id = id; | ||
} | ||
|
||
public void setCodeName(String codeName) { | ||
this.codeName = codeName; | ||
} | ||
|
||
public void setAge(String age) { | ||
this.age = age; | ||
} | ||
|
||
private String id; | ||
private String codeName; | ||
private String age; | ||
} | ||
|
||
private String id; | ||
private String codeName; | ||
private String age; | ||
|
||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public String getCodeName() { | ||
return codeName; | ||
} | ||
|
||
@Override | ||
public String getAge() { | ||
return this.age; | ||
} | ||
|
||
public void setId(String id) { | ||
this.id = id; | ||
} | ||
|
||
public void setCodeName(String codeName) { | ||
this.codeName = codeName; | ||
} | ||
|
||
public void setAge(String age) { | ||
this.age = age; | ||
} | ||
} |
Oops, something went wrong.