Skip to content

Commit

Permalink
Make cauldron contents more clearly hashable
Browse files Browse the repository at this point in the history
In theory, any interface implementation with the same two values will hash to the same location. Means I do not need to use a pair for map lookups, cauldron contents is sufficient
  • Loading branch information
KnightMiner committed Aug 29, 2020
1 parent a13e130 commit 199a0ac
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
Expand Up @@ -31,4 +31,9 @@ public ResourceLocation getTextureName() {
public int getTintColor() {
return -1;
}

@Override
public <T> boolean matches(CauldronContentType<T> type, T value) {
return type == CauldronContentTypes.EMPTY;
}
}
Expand Up @@ -37,4 +37,34 @@ public interface ICauldronContents {
* @return Tint color
*/
int getTintColor();


/* Mapping */

/**
* Checks if these cauldron contents match the given arguments. Use this for {@link #equals(Object)} implementations
* @param type Content type
* @param value Value of the content
* @param <T> Content class
* @return True if they match
*/
<T> boolean matches(CauldronContentType<T> type, T value);

/**
* For consistency, hash code should be {@code 31 * type.hashCode() + value.hashCode()}
* @return Hash code for the given type and value
*/
@Override
int hashCode();

/**
* Checks if the contents contain the given value. Unlike {@link #matches(CauldronContentType, Object)}, supports overrides.
* @param type Content type
* @param value Value of the content
* @param <T> Content class
* @return True if get would return this value
*/
default <T> boolean contains(CauldronContentType<T> type, T value) {
return get(type).map(value::equals).orElse(false);
}
}
Expand Up @@ -48,16 +48,21 @@ public int getTintColor() {
return type.getColor(value);
}

@Override
public <T> boolean matches(CauldronContentType<T> type, T value) {
return this.type == type && this.value == value;
}

@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof CauldronContents)) {
if (!(other instanceof ICauldronContents)) {
return false;
}
CauldronContents<?> contents = (CauldronContents<?>)other;
return this.type == contents.type && this.value.equals(contents.value);
ICauldronContents contents = (ICauldronContents)other;
return contents.matches(type, value);
}

@Override
Expand Down

0 comments on commit 199a0ac

Please sign in to comment.