You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Stephan Schuster <st...@wilken.de> on 2008/06/11 18:34:24 UTC
[vfs] Why doesn't AbstractFileObject override equals?
hi together,
on the first glimpse i don't understand why AbstractFileObject doesn't
override equals() and hashCode(). depending on the concrete
implementation this may result in Object's identity comparison when
comparing two file objects which is in most cases definitely not desired
(see further below). in my opinion a simple solution would be:
@Override
public boolean equals(final Object other) {
if (this == other)
return true;
if (other == null)
return false;
if (!(other instanceof FileObject))
return false;
try {
return getURL().equals(((FileObject) other).getURL());
} catch (FileSystemException e) {
return false;
}
}
@Override
public int hashCode() {
try {
return getURL().hashCode();
} catch (FileSystemException e) {
return super.hashCode();
}
}
another issue i stumbled upon today: in AbstractFileObject.getParent()
there's an if-clause like:
if (this == fs.getRoot()) { ... }
i'm pretty sure that an identity comparison makes no sense here and this
should rather be changed to
if (this.equals(fs.getRoot())) { ... }
however, this will only work if equals() is overridden.
what do you think?
regards,
stephan
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org
Re: [vfs] Why doesn't AbstractFileObject override equals?
Posted by Mario Ivankovits <ma...@ops.co.at>.
Hi!
I'd go that way:
> 1. in AbstractFileObject.getParent():
>
> replace
>
> if (this == fs.getRoot()) { ... };
>
> with
>
> FileObject root = fs.getRoot();
> if (root instanceof DecoratedFileObject) {
> root = ((DecoratedFileObject) root).getDecoratedFileObject();
> }
> if (this == root) { ... }
But instead of the instanceof thing simply use
FileObject root = FileObjectUtils.getAbstractFileObject(root);
Which does all the unpacking stuff.
Ciao,
Mario
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org
Re: [vfs] Why doesn't AbstractFileObject override equals?
Posted by Stephan Schuster <st...@wilken.de>.
hi mario,
thank you for your answer.
> The current VFS implementation (if you do not use anything else then the
> only-working SoftRefFilesCache) ensures that two resolveFile will return
> the same object if you ask for the same filename, thus, in terms of VFS
> there is nothing bad with the object comparison.
that is what i was hoping as i saw the code for the first time. however
i always got NPEs somewhere further up in stack. to me it seemed that
the original cause was always the AbstractFileObject.getParent() method
or more precisely the comparison
if (this == fs.getRoot()) { ... };
which never evaluated to true, even if it should! so i tracked down the
problem a bit further. the reason is the following: under certain
circumstances my custom filesystemmanager uses CacheStrategy.ON_CALL. on
resolving a file via this filesystem i therefore get an instance of a
DecoratedFileObject, in fact an instance of OnCallRefreshFileObject. now
when calling getParent() on a DecoratedFileObject the call is forwarded
to the actual implementation of the file object, lets say
LocalFileObject. then, in the above comparison the "this" pointer is an
instance of LocalFileObject. however, "fs.getRoot()" which internally
calls "resolveFile(rootName)" uses the same filesystem and therefore
returns an instance of OnCallRefreshFileObject. comparing both,
LocalFileObject and OnCallRefreshFileObject, via identity will never
work. that's the problem!
there a several solutions:
1. in AbstractFileObject.getParent():
replace
if (this == fs.getRoot()) { ... };
with
FileObject root = fs.getRoot();
if (root instanceof DecoratedFileObject) {
root = ((DecoratedFileObject) root).getDecoratedFileObject();
}
if (this == root) { ... }
2. use equals for the above comparison and overwrite equals in
AbstractFileObject in such way that the identity comparison respects
decorated file objects.
3. use the file object's path for determinig equality (as in my last
posting)
what do think is the best way?
regards,
stephan
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org
Re: [vfs] Why doesn't AbstractFileObject override equals?
Posted by Mario Ivankovits <ma...@ops.co.at>.
Hi!
> on the first glimpse i don't understand why AbstractFileObject doesn't
> override equals() and hashCode().
The current VFS implementation (if you do not use anything else then the
only-working SoftRefFilesCache) ensures that two resolveFile will return
the same object if you ask for the same filename, thus, in terms of VFS
there is nothing bad with the object comparison.
Did you catch any problem?
Ciao,
Mario
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org