Skip to content

Commit 39e187d

Browse files
akutscheraNylle
authored andcommitted
Use SpecimenType as key for caching map
Refs: #89
1 parent b40cf12 commit 39e187d

File tree

7 files changed

+56
-28
lines changed

7 files changed

+56
-28
lines changed

src/main/java/com/github/nylle/javafixture/Context.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
public class Context {
77
private final Configuration configuration;
8-
private final Map<Integer, Object> cache;
8+
private final Map<SpecimenType<?>, Object> cache;
99

1010
public Context(Configuration configuration) {
1111

@@ -17,7 +17,7 @@ public Context(Configuration configuration) {
1717
this.cache = new ConcurrentHashMap<>();
1818
}
1919

20-
public Context(Configuration configuration, Map<Integer, Object> predefinedInstances) {
20+
public Context(Configuration configuration, Map<SpecimenType<?>, Object> predefinedInstances) {
2121

2222
if (configuration == null) {
2323
throw new IllegalArgumentException("configuration: null");
@@ -32,25 +32,25 @@ public Configuration getConfiguration() {
3232
}
3333

3434
public boolean isCached(SpecimenType<?> type) {
35-
return cache.containsKey(type.hashCode());
35+
return cache.containsKey(type);
3636
}
3737

3838
public <T> T overwrite(SpecimenType<?> type, T instance) {
39-
cache.put(type.hashCode(), instance);
40-
return (T) cache.get(type.hashCode());
39+
cache.put(type, instance);
40+
return (T) cache.get(type);
4141
}
4242

4343
public <T> T cached(SpecimenType<?> type, T instance) {
44-
cache.putIfAbsent(type.hashCode(), instance);
45-
return (T) cache.get(type.hashCode());
44+
cache.putIfAbsent(type, instance);
45+
return (T) cache.get(type);
4646
}
4747

4848
public <T> T cached(SpecimenType<T> type) {
49-
return (T) cache.get(type.hashCode());
49+
return (T) cache.get(type);
5050
}
5151

5252
public <T> T preDefined(SpecimenType<T> type, T instance) {
53-
return cache.containsKey(type.hashCode()) ? (T) cache.get(type.hashCode()) : instance;
53+
return cache.containsKey(type) ? (T) cache.get(type) : instance;
5454
}
5555

5656
}

src/main/java/com/github/nylle/javafixture/SpecimenBuilder.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class SpecimenBuilder<T> implements ISpecimenBuilder<T> {
1515
private final List<Consumer<T>> functions = new LinkedList<>();
1616
private final List<String> ignoredFields = new LinkedList<>();
1717
private final Map<String, Object> customFields = new HashMap<>();
18-
private final Map<Integer, Object> predefinedInstances = new ConcurrentHashMap<>();
18+
private final Map<SpecimenType<?>, Object> predefinedInstances = new ConcurrentHashMap<>();
1919

2020
private final SpecimenType<T> type;
2121
private final Configuration configuration;
@@ -98,7 +98,7 @@ public ISpecimenBuilder<T> with(final String fieldName, Object value) {
9898
*/
9999
@Override
100100
public <U> ISpecimenBuilder<T> with(final Class<U> type, final U value) {
101-
predefinedInstances.putIfAbsent(SpecimenType.fromClass(type).hashCode(), value);
101+
predefinedInstances.putIfAbsent(SpecimenType.fromClass(type), value);
102102
return this;
103103
}
104104

@@ -112,7 +112,7 @@ public <U> ISpecimenBuilder<T> with(final Class<U> type, final U value) {
112112
*/
113113
@Override
114114
public <U> ISpecimenBuilder<T> with(final SpecimenType<U> type, final U value) {
115-
predefinedInstances.putIfAbsent(type.hashCode(), value);
115+
predefinedInstances.putIfAbsent(type, value);
116116
return this;
117117
}
118118

src/main/java/com/github/nylle/javafixture/SpecimenType.java

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import java.time.temporal.Temporal;
1111
import java.time.temporal.TemporalAdjuster;
1212
import java.time.temporal.TemporalAmount;
13-
import java.util.Arrays;
1413
import java.util.Collection;
1514
import java.util.List;
1615
import java.util.Map;
@@ -220,19 +219,11 @@ public boolean equals(final Object o) {
220219
return true;
221220
}
222221

223-
if (o == null || getClass() != o.getClass()) {
224-
return false;
222+
if (o instanceof SpecimenType<?>) {
223+
SpecimenType<?> that = (SpecimenType<?>) o;
224+
return this.type.equals(that.type);
225225
}
226-
227-
final SpecimenType that = (SpecimenType) o;
228-
229-
if (isParameterized() != that.isParameterized()) {
230-
return false;
231-
}
232-
233-
return isParameterized() && that.isParameterized()
234-
? Objects.equals(type, that.type) && Arrays.equals(getGenericTypeArguments(), that.getGenericTypeArguments())
235-
: Objects.equals(type, that.type);
226+
return false;
236227
}
237228

238229
@Override

src/test/java/com/github/nylle/javafixture/SpecimenTypeTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.github.nylle.javafixture.testobjects.TestObjectWithStaticMethods;
1212
import com.github.nylle.javafixture.testobjects.TestWildCardType;
1313
import com.github.nylle.javafixture.testobjects.withconstructor.TestObjectWithAllConstructors;
14+
import org.junit.jupiter.api.Nested;
1415
import org.junit.jupiter.api.Test;
1516

1617
import java.io.File;
@@ -489,4 +490,40 @@ void castToClass_CanHandleWildcardTypesWithoutBounds() {
489490

490491
assertThat(SpecimenType.castToClass(wildcardType)).isEqualTo(Object.class);
491492
}
493+
494+
@Nested
495+
class EqualsTests {
496+
497+
@Test
498+
void objectIsEqualToItself() {
499+
var sut = SpecimenType.fromClass(String.class);
500+
assertThat(sut.equals(sut)).isTrue();
501+
}
502+
503+
@Test
504+
void objectIsNotEqualToNull() {
505+
var sut = SpecimenType.fromClass(String.class);
506+
assertThat(sut.equals(null)).isFalse();
507+
}
508+
509+
@Test
510+
void objectIsNotEqualToOtherClass() {
511+
var sut = SpecimenType.fromClass(String.class);
512+
assertThat(sut.equals(String.class)).isFalse();
513+
}
514+
515+
@Test
516+
void objectIsEqualToSpecimenTypeOfSameClass() {
517+
var sut = SpecimenType.fromClass(String.class);
518+
var other = SpecimenType.fromClass(String.class);
519+
assertThat(sut.equals(other)).isTrue();
520+
}
521+
522+
@Test
523+
void objectIsNotEqualToSpecimenTypeOfOtherClass() {
524+
var sut = new SpecimenType<List<TestObject>>() {};
525+
var other = new SpecimenType<List<List<TestObject>>>() {};
526+
assertThat(sut.equals(other)).isFalse();
527+
}
528+
}
492529
}

src/test/java/com/github/nylle/javafixture/specimen/EnumSpecimenTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ void createEnum() {
5252
void canBePredefined() {
5353
var expected = fixture().create(TestEnum.class);
5454

55-
var context = new Context(Configuration.configure(), Map.of(SpecimenType.fromClass(TestEnum.class).hashCode(), expected));
55+
var context = new Context(Configuration.configure(), Map.of(SpecimenType.fromClass(TestEnum.class), expected));
5656

5757
var sut = new EnumSpecimen<>(SpecimenType.fromClass(TestEnum.class), context);
5858

src/test/java/com/github/nylle/javafixture/specimen/PrimitiveSpecimenTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ void createDouble(boolean positiveOnly, double min, double max) {
143143
void canBePredefined(Class type) {
144144
var expected = fixture().create(type);
145145

146-
var context = new Context(Configuration.configure(), Map.of(SpecimenType.fromClass(type).hashCode(), expected));
146+
var context = new Context(Configuration.configure(), Map.of(SpecimenType.fromClass(type), expected));
147147

148148
var sut = new PrimitiveSpecimen<>(SpecimenType.fromClass(type), context);
149149

src/test/java/com/github/nylle/javafixture/specimen/TimeSpecimenTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ void createWithClock() {
155155
void canBePredefined(Class type) {
156156
var expected = fixture().create(type);
157157

158-
var context = new Context(Configuration.configure(), Map.of(SpecimenType.fromClass(type).hashCode(), expected));
158+
var context = new Context(Configuration.configure(), Map.of(SpecimenType.fromClass(type), expected));
159159

160160
var sut = new TimeSpecimen<>(SpecimenType.fromClass(type), context);
161161

0 commit comments

Comments
 (0)