Skip to content

Commit cc8e350

Browse files
authored
Merge pull request #375 from lburja/master
Remove Guava dependency
2 parents 00d86ed + 661769e commit cc8e350

File tree

10 files changed

+271
-17
lines changed

10 files changed

+271
-17
lines changed

pom.xml

-5
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,6 @@
114114
<artifactId>commons-lang3</artifactId>
115115
<version>3.9</version>
116116
</dependency>
117-
<dependency>
118-
<groupId>com.google.guava</groupId>
119-
<artifactId>guava</artifactId>
120-
<version>26.0-jre</version>
121-
</dependency>
122117
<dependency>
123118
<groupId>org.slf4j</groupId>
124119
<artifactId>slf4j-api</artifactId>

src/main/kotlin/graphql/kickstart/tools/DictionaryTypeResolver.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package graphql.kickstart.tools
22

3-
import com.google.common.collect.BiMap
43
import graphql.TypeResolutionEnvironment
4+
import graphql.kickstart.tools.util.BiMap
55
import graphql.language.TypeDefinition
66
import graphql.schema.GraphQLInterfaceType
77
import graphql.schema.GraphQLObjectType

src/main/kotlin/graphql/kickstart/tools/GenericType.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package graphql.kickstart.tools
22

33
import com.fasterxml.classmate.ResolvedType
4-
import com.google.common.primitives.Primitives
4+
import graphql.kickstart.tools.util.Primitives
55
import org.apache.commons.lang3.reflect.TypeUtils
66
import java.lang.reflect.ParameterizedType
77
import java.lang.reflect.TypeVariable

src/main/kotlin/graphql/kickstart/tools/ScannedSchemaObjects.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package graphql.kickstart.tools
22

3-
import com.google.common.collect.BiMap
3+
import graphql.kickstart.tools.util.BiMap
44
import graphql.language.FieldDefinition
55
import graphql.language.ObjectTypeDefinition
66
import graphql.language.TypeDefinition

src/main/kotlin/graphql/kickstart/tools/SchemaClassScanner.kt

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package graphql.kickstart.tools
22

3-
import com.google.common.collect.BiMap
4-
import com.google.common.collect.HashBiMap
5-
import com.google.common.collect.Maps
3+
import graphql.kickstart.tools.util.BiMap
64
import graphql.language.*
75
import graphql.schema.GraphQLScalarType
86
import graphql.schema.idl.ScalarInfo
@@ -120,7 +118,7 @@ internal class SchemaClassScanner(initialDictionary: BiMap<String, Class<*>>, al
120118
// Input types can also be excluded from the dictionary, since it's only used for interfaces, unions, and enums.
121119
// Union types can also be excluded, as their possible types are resolved recursively later
122120
val dictionary = try {
123-
Maps.unmodifiableBiMap(HashBiMap.create<TypeDefinition<*>, JavaType>().also {
121+
BiMap.unmodifiableBiMap(BiMap.create<TypeDefinition<*>, JavaType>().also {
124122
dictionary.filter {
125123
it.value.javaType != null
126124
&& it.value.typeClass() != java.lang.Object::class.java

src/main/kotlin/graphql/kickstart/tools/SchemaParserBuilder.kt

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package graphql.kickstart.tools
22

33
import com.fasterxml.jackson.databind.ObjectMapper
4-
import com.google.common.collect.BiMap
5-
import com.google.common.collect.HashBiMap
6-
import com.google.common.collect.Maps
74
import graphql.kickstart.tools.relay.RelayConnectionFactory
5+
import graphql.kickstart.tools.util.BiMap
86
import graphql.language.Definition
97
import graphql.language.Document
108
import graphql.parser.Parser
@@ -216,9 +214,9 @@ class InvalidSchemaError(pce: ParseCancellationException, private val recognitio
216214

217215
class SchemaParserDictionary {
218216

219-
private val dictionary: BiMap<String, Class<*>> = HashBiMap.create()
217+
private val dictionary: BiMap<String, Class<*>> = BiMap.create()
220218

221-
fun getDictionary(): BiMap<String, Class<*>> = Maps.unmodifiableBiMap(dictionary)
219+
fun getDictionary(): BiMap<String, Class<*>> = BiMap.unmodifiableBiMap(dictionary)
222220

223221
/**
224222
* Add arbitrary classes to the parser's dictionary, overriding the generated type name.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
package graphql.kickstart.tools.util;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.Objects;
6+
import java.util.Set;
7+
8+
import static java.util.Collections.unmodifiableMap;
9+
10+
@SuppressWarnings({"NullableProblems", "SuspiciousMethodCalls"})
11+
public class BiMap<K, V> implements Map<K, V> {
12+
private final Map<K, V> delegate;
13+
private final Map<V, K> inverse;
14+
15+
public static <K, V> BiMap<K, V> create() {
16+
return new BiMap<>(new HashMap<>(), new HashMap<>());
17+
}
18+
19+
public static <K, V> BiMap<K, V> unmodifiableBiMap(BiMap<K, V> biMap) {
20+
return new BiMap<>(unmodifiableMap(biMap.delegate), unmodifiableMap(biMap.inverse));
21+
}
22+
23+
private BiMap(Map<K, V> delegate, Map<V, K> inverse) {
24+
this.delegate = delegate;
25+
this.inverse = inverse;
26+
}
27+
28+
public BiMap<V, K> inverse() {
29+
return new BiMap<>(inverse, delegate);
30+
}
31+
32+
@Override
33+
public void clear() {
34+
delegate.clear();
35+
inverse.clear();
36+
}
37+
38+
@Override
39+
public boolean containsKey(Object key) {
40+
return delegate.containsKey(key);
41+
}
42+
43+
@Override
44+
public boolean containsValue(Object value) {
45+
return inverse.containsKey(value);
46+
}
47+
48+
@Override
49+
public Set<Entry<K, V>> entrySet() {
50+
return delegate.entrySet();
51+
}
52+
53+
@Override
54+
public V get(Object key) {
55+
return delegate.get(key);
56+
}
57+
58+
@Override
59+
public boolean isEmpty() {
60+
return delegate.isEmpty();
61+
}
62+
63+
@Override
64+
public Set<K> keySet() {
65+
return delegate.keySet();
66+
}
67+
68+
/**
69+
* {@inheritDoc}
70+
*
71+
* @throws IllegalArgumentException if the given value is already bound to a different key in this bimap.
72+
*/
73+
@Override
74+
public V put(K key, V value) {
75+
// if the key already exists, and the value is the same as the old, then nothing to do
76+
boolean containedKey = containsKey(key);
77+
if (containedKey && Objects.equals(value, get(key))) {
78+
return value;
79+
}
80+
81+
// if the value already exists, then it's not ok
82+
if (containsValue(value)) {
83+
throw new IllegalArgumentException("value already present: " + value);
84+
}
85+
86+
// put the value; if the key already exists, it replaces an existing value, and we have to remove it from the inverse as well
87+
V oldValue = delegate.put(key, value);
88+
if (containedKey) {
89+
inverse.remove(oldValue);
90+
}
91+
inverse.put(value, key);
92+
93+
return oldValue;
94+
}
95+
96+
/**
97+
* {@inheritDoc}
98+
*
99+
* @throws IllegalArgumentException if an attempt to {@code put} any entry fails.
100+
*/
101+
@Override
102+
public void putAll(Map<? extends K, ? extends V> m) {
103+
for (Entry<? extends K, ? extends V> entry : m.entrySet()) {
104+
put(entry.getKey(), entry.getValue());
105+
}
106+
}
107+
108+
@Override
109+
public V remove(Object key) {
110+
if (!containsKey(key)) {
111+
return null;
112+
}
113+
114+
inverse.remove(delegate.get(key));
115+
return delegate.remove(key);
116+
}
117+
118+
@Override
119+
public int size() {
120+
return delegate.size();
121+
}
122+
123+
@Override
124+
public Set<V> values() {
125+
return inverse.keySet();
126+
}
127+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package graphql.kickstart.tools.util;
2+
3+
public final class Primitives {
4+
@SuppressWarnings("unchecked")
5+
public static <T> Class<T> wrap(Class<T> type) {
6+
if (boolean.class.equals(type)) {
7+
return (Class<T>) Boolean.class;
8+
} else if (byte.class.equals(type)) {
9+
return (Class<T>) Byte.class;
10+
} else if (char.class.equals(type)) {
11+
return (Class<T>) Character.class;
12+
} else if (double.class.equals(type)) {
13+
return (Class<T>) Double.class;
14+
} else if (float.class.equals(type)) {
15+
return (Class<T>) Float.class;
16+
} else if (int.class.equals(type)) {
17+
return (Class<T>) Integer.class;
18+
} else if (long.class.equals(type)) {
19+
return (Class<T>) Long.class;
20+
} else if (short.class.equals(type)) {
21+
return (Class<T>) Short.class;
22+
} else if (void.class.equals(type)) {
23+
return (Class<T>) Void.class;
24+
} else {
25+
return type;
26+
}
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package graphql.kickstart.tools.util;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
6+
import java.util.Arrays;
7+
import java.util.HashSet;
8+
import java.util.Set;
9+
10+
import static org.hamcrest.CoreMatchers.is;
11+
import static org.junit.Assert.*;
12+
13+
public class BiMapTest {
14+
private BiMap<String, Integer> bimap;
15+
16+
@Before
17+
public void setUp() {
18+
bimap = BiMap.create();
19+
}
20+
21+
@Test(expected = IllegalArgumentException.class)
22+
public void testDoesNotPermitDuplicateValues() {
23+
bimap.put("foo", 1);
24+
bimap.put("bar", 1);
25+
}
26+
27+
@Test
28+
@SuppressWarnings("OverwrittenKey")
29+
public void testPutSameKeySameValue() {
30+
bimap.put("foo", 1);
31+
32+
// should not fail
33+
bimap.put("foo", 1);
34+
}
35+
36+
@Test
37+
@SuppressWarnings({"ConstantConditions"})
38+
public void testPutSameKeyNewValue() {
39+
bimap.put("foo", 1);
40+
bimap.put("bar", 2);
41+
42+
int old = bimap.put("bar", 3);
43+
44+
// old value should have been returned
45+
assertEquals(2, old);
46+
47+
// inverse should have correct keys
48+
Set<Integer> expected = new HashSet<>(Arrays.asList(1, 3));
49+
assertEquals(expected, bimap.values());
50+
assertEquals(expected, bimap.inverse().keySet());
51+
}
52+
53+
@Test
54+
public void testInverse() {
55+
bimap.put("foo", 1);
56+
bimap.put("bar", 2);
57+
bimap.put("baz", 3);
58+
59+
BiMap<Integer, String> inverse = bimap.inverse();
60+
assertTrue(inverse.containsKey(1));
61+
assertTrue(inverse.containsKey(2));
62+
assertTrue(inverse.containsKey(3));
63+
64+
assertThat(inverse.get(1), is("foo"));
65+
assertThat(inverse.get(2), is("bar"));
66+
assertThat(inverse.get(3), is("baz"));
67+
}
68+
69+
@Test
70+
public void testValues() {
71+
bimap.put("foo", 1);
72+
bimap.put("bar", 2);
73+
bimap.put("baz", 3);
74+
75+
Set<Integer> values = bimap.values();
76+
Set<Integer> expected = new HashSet<>(Arrays.asList(1, 2, 3));
77+
assertThat(values, is(expected));
78+
}
79+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package graphql.kickstart.tools.util;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertEquals;
6+
import static org.junit.Assert.assertNull;
7+
8+
public class PrimitivesTest {
9+
@Test
10+
public void testWrapPrimitive() {
11+
Class<?> clazz = Primitives.wrap(int.class);
12+
13+
assertEquals(Integer.class, clazz);
14+
}
15+
16+
@Test
17+
public void testWrapNonPrimitive() {
18+
Class<?> clazz = Primitives.wrap(String.class);
19+
20+
assertEquals(String.class, clazz);
21+
}
22+
23+
@Test
24+
public void testWrapNull() {
25+
Class<?> clazz = Primitives.wrap(null);
26+
27+
assertNull(clazz);
28+
}
29+
}

0 commit comments

Comments
 (0)