* Implemented hashCode() and equals(Object) for all Repository and Resource implementations

* Some code improvements and minor fixes
This commit is contained in:
hns 2005-03-23 12:23:49 +00:00
parent d3e5eaca9d
commit 14f8f1e8d2
4 changed files with 56 additions and 22 deletions

View file

@ -131,6 +131,12 @@ public class FileRepository extends AbstractRepository {
* resources * resources
*/ */
public synchronized void update() { public synchronized void update() {
if (!dir.exists()) {
repositories = new Repository[0];
resources.clear();
return;
}
if (dir.lastModified() != lastModified) { if (dir.lastModified() != lastModified) {
lastModified = dir.lastModified(); lastModified = dir.lastModified();
@ -159,6 +165,15 @@ public class FileRepository extends AbstractRepository {
} }
} }
public int hashCode() {
return 17 + (37 * dir.hashCode());
}
public boolean equals(Object obj) {
return obj instanceof FileRepository &&
dir.equals(((FileRepository) obj).dir);
}
public String toString() { public String toString() {
return new StringBuffer("FileRepository[").append(name).append("]").toString(); return new StringBuffer("FileRepository[").append(name).append("]").toString();
} }

View file

@ -33,16 +33,12 @@ public class FileResource implements Resource {
protected FileResource(File file, FileRepository repository) { protected FileResource(File file, FileRepository repository) {
this.file = file; this.file = file;
if (repository == null) { this.repository = repository;
name = file.getAbsolutePath(); name = file.getAbsolutePath();
shortName = file.getName(); shortName = file.getName();
} else { // cut off extension from short name
this.repository = repository; if (shortName.lastIndexOf(".") > -1) {
name = repository.getName() + "/" + file.getName(); shortName = shortName.substring(0, shortName.lastIndexOf("."));
shortName = file.getName();
if (shortName.lastIndexOf(".") != -1) {
shortName = shortName.substring(0, shortName.lastIndexOf("."));
}
} }
} }
@ -100,6 +96,14 @@ public class FileResource implements Resource {
return repository; return repository;
} }
public int hashCode() {
return 17 + name.hashCode();
}
public boolean equals(Object obj) {
return obj instanceof FileResource && name.equals(((FileResource)obj).name);
}
public String toString() { public String toString() {
return getName(); return getName();
} }

View file

@ -180,6 +180,19 @@ public final class ZipRepository extends AbstractRepository {
return file.lastModified(); return file.lastModified();
} }
public int hashCode() {
return 17 + (37 * file.hashCode()) + (37 * name.hashCode());
}
public boolean equals(Object obj) {
if (!(obj instanceof ZipRepository)) {
return false;
}
ZipRepository rep = (ZipRepository) obj;
return (file.equals(rep.file) && name.equals(rep.name));
}
public String toString() { public String toString() {
return new StringBuffer("ZipRepository[").append(name).append("]").toString(); return new StringBuffer("ZipRepository[").append(name).append("]").toString();
} }

View file

@ -35,21 +35,15 @@ public final class ZipResource implements Resource {
this.repository = repository; this.repository = repository;
String entryname = zipentry.getName(); String entryname = zipentry.getName();
int lastDot = entryname.lastIndexOf('.');
int lastSlash = entryname.lastIndexOf('/'); int lastSlash = entryname.lastIndexOf('/');
if (lastDot != -1 && lastDot > lastSlash) { shortName = entryname.substring(lastSlash + 1);
shortName = entryname.substring(lastSlash + 1, lastDot); name = new StringBuffer(repository.getName()).append('/')
} else { .append(shortName).toString();
shortName = entryname.substring(lastSlash + 1);
}
StringBuffer buf = new StringBuffer(repository.getName()) // cut off extension from short name
.append('/').append(shortName); if (shortName.lastIndexOf(".") > -1) {
if (lastDot != -1 && lastDot > lastSlash) { shortName = shortName.substring(0, shortName.lastIndexOf("."));
name = buf.append(entryname.substring(lastDot)).toString();
} else {
name = buf.toString();
} }
} }
@ -133,6 +127,14 @@ public final class ZipResource implements Resource {
return repository; return repository;
} }
public int hashCode() {
return 17 + name.hashCode();
}
public boolean equals(Object obj) {
return obj instanceof ZipResource && name.equals(((ZipResource) obj).name);
}
public String toString() { public String toString() {
return getName(); return getName();
} }