Skip to content

Commit

Permalink
Implement equals() and hashCode() in YamlStorage and its YamlKey
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Jul 14, 2013
1 parent e560f8e commit 9d83e4a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 10 deletions.
4 changes: 1 addition & 3 deletions src/main/java/net/citizensnpcs/api/util/DataKey.java
Expand Up @@ -107,9 +107,7 @@ public String getString(String key, String value) {
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((path == null) ? 0 : path.hashCode());
return result;
return prime + ((path == null) ? 0 : path.hashCode());
}

public abstract boolean keyExists(String key);
Expand Down
57 changes: 50 additions & 7 deletions src/main/java/net/citizensnpcs/api/util/YamlStorage.java
Expand Up @@ -44,6 +44,25 @@ private void create() {
}
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
YamlStorage other = (YamlStorage) obj;
if (file == null) {
if (other.file != null) {
return false;
}
} else if (!file.equals(other.file)) {
return false;
}
return true;
}

@Override
public File getFile() {
return file;
Expand All @@ -54,6 +73,14 @@ public YamlKey getKey(String root) {
return new YamlKey(root);
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((file == null) ? 0 : file.hashCode());
return result;
}

@Override
public boolean load() {
try {
Expand Down Expand Up @@ -90,17 +117,22 @@ public String toString() {
}

public class YamlKey extends DataKey {

public YamlKey(String root) {
super(root);
}

/* @Override
public void copy(String to) {
ConfigurationSection root = config.getConfigurationSection(current);
if (root == null)
return;
config.createSection(to, root.getValues(true));
}*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!super.equals(obj) || getClass() != obj.getClass()) {
return false;
}
YamlKey other = (YamlKey) obj;
return getOuterType().equals(other.getOuterType());
}

@Override
public boolean getBoolean(String key) {
Expand Down Expand Up @@ -178,6 +210,10 @@ public long getLong(String key, long def) {
return def;
}

private YamlStorage getOuterType() {
return YamlStorage.this;
}

@Override
public Object getRaw(String key) {
return config.get(createRelativeKey(key));
Expand Down Expand Up @@ -218,6 +254,13 @@ public Map<String, Object> getValuesDeep() {
return (Map<String, Object>) (subSection == null ? Collections.emptyMap() : subSection.getValues(true));
}

@Override
public int hashCode() {
final int prime = 31;
int result = prime * super.hashCode() + getOuterType().hashCode();
return result;
}

@Override
public boolean keyExists(String key) {
return config.get(createRelativeKey(key)) != null;
Expand Down

0 comments on commit 9d83e4a

Please sign in to comment.