* Implemented hashCode() and equals(Object) for all Repository and Resource implementations
* Some code improvements and minor fixes
This commit is contained in:
parent
d3e5eaca9d
commit
14f8f1e8d2
4 changed files with 56 additions and 22 deletions
|
@ -131,6 +131,12 @@ public class FileRepository extends AbstractRepository {
|
|||
* resources
|
||||
*/
|
||||
public synchronized void update() {
|
||||
if (!dir.exists()) {
|
||||
repositories = new Repository[0];
|
||||
resources.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
if (dir.lastModified() != 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() {
|
||||
return new StringBuffer("FileRepository[").append(name).append("]").toString();
|
||||
}
|
||||
|
|
|
@ -33,18 +33,14 @@ public class FileResource implements Resource {
|
|||
protected FileResource(File file, FileRepository repository) {
|
||||
this.file = file;
|
||||
|
||||
if (repository == null) {
|
||||
this.repository = repository;
|
||||
name = file.getAbsolutePath();
|
||||
shortName = file.getName();
|
||||
} else {
|
||||
this.repository = repository;
|
||||
name = repository.getName() + "/" + file.getName();
|
||||
shortName = file.getName();
|
||||
if (shortName.lastIndexOf(".") != -1) {
|
||||
// cut off extension from short name
|
||||
if (shortName.lastIndexOf(".") > -1) {
|
||||
shortName = shortName.substring(0, shortName.lastIndexOf("."));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
|
@ -100,6 +96,14 @@ public class FileResource implements Resource {
|
|||
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() {
|
||||
return getName();
|
||||
}
|
||||
|
|
|
@ -180,6 +180,19 @@ public final class ZipRepository extends AbstractRepository {
|
|||
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() {
|
||||
return new StringBuffer("ZipRepository[").append(name).append("]").toString();
|
||||
}
|
||||
|
|
|
@ -35,21 +35,15 @@ public final class ZipResource implements Resource {
|
|||
this.repository = repository;
|
||||
|
||||
String entryname = zipentry.getName();
|
||||
int lastDot = entryname.lastIndexOf('.');
|
||||
int lastSlash = entryname.lastIndexOf('/');
|
||||
|
||||
if (lastDot != -1 && lastDot > lastSlash) {
|
||||
shortName = entryname.substring(lastSlash + 1, lastDot);
|
||||
} else {
|
||||
shortName = entryname.substring(lastSlash + 1);
|
||||
}
|
||||
name = new StringBuffer(repository.getName()).append('/')
|
||||
.append(shortName).toString();
|
||||
|
||||
StringBuffer buf = new StringBuffer(repository.getName())
|
||||
.append('/').append(shortName);
|
||||
if (lastDot != -1 && lastDot > lastSlash) {
|
||||
name = buf.append(entryname.substring(lastDot)).toString();
|
||||
} else {
|
||||
name = buf.toString();
|
||||
// cut off extension from short name
|
||||
if (shortName.lastIndexOf(".") > -1) {
|
||||
shortName = shortName.substring(0, shortName.lastIndexOf("."));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -133,6 +127,14 @@ public final class ZipResource implements Resource {
|
|||
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() {
|
||||
return getName();
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue