Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add equals and hashcode to config class #192

Merged
merged 1 commit into from
Jun 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 101 additions & 8 deletions src/main/java/com/worksap/nlp/sudachi/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,28 @@ public Config withFallback(Config other) {
return this;
}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Config config = (Config) o;
return Objects.equals(systemDictionary, config.systemDictionary)
&& Objects.equals(userDictionary, config.userDictionary)
&& Objects.equals(characterDefinition, config.characterDefinition)
&& Objects.equals(editConnectionCost, config.editConnectionCost)
&& Objects.equals(inputText, config.inputText) && Objects.equals(oovProviders, config.oovProviders)
&& Objects.equals(pathRewrite, config.pathRewrite)
&& Objects.equals(allowEmptyMorpheme, config.allowEmptyMorpheme);
}

@Override
public int hashCode() {
return Objects.hash(systemDictionary, userDictionary, characterDefinition, editConnectionCost, inputText,
oovProviders, pathRewrite, allowEmptyMorpheme);
}

@FunctionalInterface
public interface IOFunction<T, R> {
R apply(T arg) throws IOException;
Expand Down Expand Up @@ -633,22 +655,17 @@ public static <T extends Plugin> PluginConf<T> make(Class<T> clz) {
* @throws IllegalArgumentException
* when instantiation fails
*/
@SuppressWarnings("unchecked")
public T instantiate() {
Class<T> clz;
Class<? extends T> clz;
try {
clz = (Class<T>) Class.forName(clazzName);
clz = Class.forName(clazzName).asSubclass(parent);
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException("non-existent plugin class", e);
}
if (!parent.isAssignableFrom(clz)) {
throw new IllegalArgumentException(String.format("plugin %s did not have correct parent, expected %s",
clazzName, parent.getName()));
}

T result;
try {
Constructor<T> constructor = clz.getDeclaredConstructor();
Constructor<? extends T> constructor = clz.getDeclaredConstructor();
result = constructor.newInstance();
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException
| InvocationTargetException e) {
Expand Down Expand Up @@ -704,6 +721,22 @@ public PluginConf<T> addList(String key, String... values) {
return this;
}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
PluginConf<?> that = (PluginConf<?>) o;
return Objects.equals(clazzName, that.clazzName) && Objects.equals(internal, that.internal)
&& Objects.equals(parent, that.parent);
}

@Override
public int hashCode() {
return Objects.hash(clazzName, internal, parent);
}

@Override
public String toString() {
return String.format("Plugin (%s) class: %s", parent.getSimpleName(), clazzName);
Expand Down Expand Up @@ -803,6 +836,21 @@ public String toString() {
Object repr() {
return path;
}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Filesystem<?> that = (Filesystem<?>) o;
return path.equals(that.path);
}

@Override
public int hashCode() {
return Objects.hash(path);
}
}

/**
Expand Down Expand Up @@ -840,6 +888,21 @@ public String toString() {
Object repr() {
return url;
}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Classpath<?> classpath = (Classpath<?>) o;
return Objects.equals(url, classpath.url);
}

@Override
public int hashCode() {
return Objects.hash(url);
}
}

/**
Expand All @@ -864,6 +927,21 @@ public T consume(IOFunction<Resource<T>, T> creator) {
Object repr() {
return object;
}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Ready<?> ready = (Ready<?>) o;
return Objects.equals(object, ready.object);
}

@Override
public int hashCode() {
return Objects.hash(object);
}
}

public static class NotFound<T> extends Resource<T> {
Expand Down Expand Up @@ -899,6 +977,21 @@ private IllegalArgumentException makeException() {
String sb = "Failed to resolve file: " + path.toString() + "\n" + "Tried roots: " + anchor;
return new IllegalArgumentException(sb);
}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
NotFound<?> notFound = (NotFound<?>) o;
return Objects.equals(path, notFound.path) && Objects.equals(anchor, notFound.anchor);
}

@Override
public int hashCode() {
return Objects.hash(path, anchor);
}
}

/**
Expand Down
18 changes: 16 additions & 2 deletions src/main/java/com/worksap/nlp/sudachi/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ public class Settings {
PathAnchor base;

Settings(JsonObject root, PathAnchor base) {
this.root = root;
this.base = base;
this.root = Objects.requireNonNull(root);
this.base = Objects.requireNonNull(base);
}

/**
Expand Down Expand Up @@ -624,4 +624,18 @@ private static JsonObject mergeObject(JsonObject left, JsonObject right) {
return builder.build();
}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Settings settings = (Settings) o;
return root.equals(settings.root) && base.equals(settings.base);
}

@Override
public int hashCode() {
return Objects.hash(root, base);
}
}
16 changes: 12 additions & 4 deletions src/test/java/com/worksap/nlp/sudachi/ConfigTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ package com.worksap.nlp.sudachi
import java.net.URL
import java.nio.file.Path
import java.nio.file.Paths
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFails
import kotlin.test.assertNotNull
import kotlin.test.*

class ConfigTest {

Expand Down Expand Up @@ -116,4 +113,15 @@ class ConfigTest {
PathAnchor.none())
assertFails { cfg.oovProviderPlugins[0].instantiate() }
}

@Test
fun equalsHashCode() {
val c1 = Config.fromClasspath("sudachi.json")
val c2 = Config.fromClasspath("sudachi.json")
val c3 = TestDictionary.user2Cfg()
assertEquals(c1.hashCode(), c2.hashCode())
assertEquals(c1, c2)
assertNotEquals(c1.hashCode(), c3.hashCode())
assertNotEquals(c1, c3)
}
}