Skip to content

Commit

Permalink
Adds FactoryCache.copy()
Browse files Browse the repository at this point in the history
  • Loading branch information
jqno committed Dec 4, 2024
1 parent 2a13d5a commit c296bb9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private ConfiguredEqualsVerifier(
public ConfiguredEqualsVerifier copy() {
return new ConfiguredEqualsVerifier(
EnumSet.copyOf(warningsToSuppress),
new FactoryCache().merge(factoryCache),
factoryCache.copy(),
usingGetClass,
fieldnameToGetter
);
Expand Down Expand Up @@ -127,7 +127,7 @@ public <T> SingleTypeEqualsVerifierApi<T> forClass(Class<T> type) {
return new SingleTypeEqualsVerifierApi<>(
type,
EnumSet.copyOf(warningsToSuppress),
factoryCache,
factoryCache.copy(),
objenesis,
usingGetClass,
fieldnameToGetter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ public boolean contains(Class<?> type) {
return cache.containsKey(type.getName());
}

/**
* Returns a new {@code FactoryCache} instance containing the factories from {@code this}.
*
* @return a new instance containing factories from {@code this}
*/
public FactoryCache copy() {
FactoryCache result = new FactoryCache();
addAll(result, this);
return result;
}

/**
* Returns a new {@code FactoryCache} instance containing the factories from {@code this} and
* from the {@code other} cache.
Expand All @@ -75,12 +86,12 @@ public boolean contains(Class<?> type) {
*/
public FactoryCache merge(FactoryCache other) {
FactoryCache result = new FactoryCache();
copy(result, this);
copy(result, other);
addAll(result, this);
addAll(result, other);
return result;
}

private void copy(FactoryCache to, FactoryCache from) {
private void addAll(FactoryCache to, FactoryCache from) {
for (Map.Entry<String, PrefabValueFactory<?>> entry : from.cache.entrySet()) {
to.put(entry.getKey(), entry.getValue());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ public void doesntContain() {
assertFalse(cache.contains(STRING_CLASS));
}

@Test
public void copy() {
cache.put(STRING_CLASS, STRING_FACTORY);
FactoryCache copy = cache.copy();
copy.put(INT_CLASS, INT_FACTORY);
assertTrue(copy.contains(STRING_CLASS));
assertFalse(copy == cache);
assertFalse(cache.contains(INT_CLASS));
}

@Test
public void merge() {
FactoryCache a = new FactoryCache();
Expand Down

0 comments on commit c296bb9

Please sign in to comment.