You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Damien Tacheron <dt...@sqli.com> on 2010/12/28 11:51:14 UTC

[io] It seems not possible to control order in which FileAlterationObserver triggers FileAlterationListener when more than one file alteration was detected

Hi,

i'm not english native, so i'll do my best.

i need to be triggered by FileAlterationListener for file creation or file
update, based a timeline :
- first the first file which was created or updated
- second, the second fil which was created or updated
and so on

but, it seems that trigerring was done in aphabetical order and i dont' find
anything to control this.

an idea ?

Best regards

 *Damien Tacheron*
Chef de projet
------------------------------
SQLI Paris
standard : 01 55 93 26 00
ligne directe : 01 55 93 02 51
<dt...@hotmail.fr>

Re: [io] It seems not possible to control order in which FileAlterationObserver triggers FileAlterationListener when more than one file alteration was detected

Posted by Damien Tacheron <dt...@sqli.com>.
Hi,

finally, i did what you have suggested in your reply : wrapping File in a
class with equals/hashcode contract based on lastModified, and buffering
while Observer trigger events in a sorted collection instancied with a
specialized Comparator

Happy new year !



public class FileWrapper {
 private File wrapped;
 public FileWrapper(File wrapped) {
super();
this.wrapped = wrapped;
}

/* (non-Javadoc)
 * @see java.lang.Object#equals(java.lang.Object)
 */
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
} else {
File file = (File) obj;
return (file.lastModified() == wrapped.lastModified()) &&
(file.getAbsolutePath().equals(wrapped.getAbsolutePath())) ;
}
}

/* (non-Javadoc)
 * @see java.lang.Object#hashCode()
 */
@Override
public int hashCode() {
return (wrapped.getAbsolutePath()+wrapped.lastModified()).hashCode();
}

/**
 * @return La valeur de l'attribut wrapped
 */
public File getWrapped() {
return wrapped;
}
}

public class FileComparator implements Comparator<FileWrapper> {

/* (non-Javadoc)
 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
 */
public int compare(FileWrapper o1, FileWrapper o2) {
 long lastmod1 = o1.getWrapped().lastModified();
long lastmod2 = o2.getWrapped().lastModified();
 String pathName1 = o1.getWrapped().getAbsolutePath();
String pathName2 = o2.getWrapped().getAbsolutePath();
 if ( (lastmod1 == lastmod2) && (pathName1.equals(pathName2)) ) {
return 0;
} else {
/*
 * Tri par date, puis par ordre alpha du pathName
 */
if (lastmod1 > lastmod2) {
return 1;
} else if (lastmod2 > lastmod1) {
return -1;
} else {
//on s'appuie sur l'ordre alpha pour classer les fichiers avec le même
lastModified
return pathName1.compareTo(pathName2);
}
}
}
}





2010/12/31 Niall Pemberton <ni...@gmail.com>

> On Tue, Dec 28, 2010 at 10:51 AM, Damien Tacheron <dt...@sqli.com>
> wrote:
> > Hi,
> >
> > i'm not english native, so i'll do my best.
> >
> > i need to be triggered by FileAlterationListener for file creation or
> file
> > update, based a timeline :
> > - first the first file which was created or updated
> > - second, the second fil which was created or updated
> > and so on
> >
> > but, it seems that trigerring was done in alphabetical order and i dont'
> find
> > anything to control this.
>
> You are correct - there is no way to do this.
>
> FileAlterationObserver works by comparing the previous state of a
> directory structure with the current state and in order to *match*
> files it sorts the contents of each directory on file name to
> determine if a file previously existed or has been added or deleted.
> As a consequence of that is the behaviour you see - i.e. triggering in
> alphabetical order.
>
> There is no straight forward way to do what you want. The file events
> would need to be *buffered* in some way and then sorted into time
> order before firing the listeners.
>
> Niall
>
> > any idea ?
> >
> > Best regards
> >
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>

Re: [io] It seems not possible to control order in which FileAlterationObserver triggers FileAlterationListener when more than one file alteration was detected

Posted by Niall Pemberton <ni...@gmail.com>.
On Tue, Dec 28, 2010 at 10:51 AM, Damien Tacheron <dt...@sqli.com> wrote:
> Hi,
>
> i'm not english native, so i'll do my best.
>
> i need to be triggered by FileAlterationListener for file creation or file
> update, based a timeline :
> - first the first file which was created or updated
> - second, the second fil which was created or updated
> and so on
>
> but, it seems that trigerring was done in alphabetical order and i dont' find
> anything to control this.

You are correct - there is no way to do this.

FileAlterationObserver works by comparing the previous state of a
directory structure with the current state and in order to *match*
files it sorts the contents of each directory on file name to
determine if a file previously existed or has been added or deleted.
As a consequence of that is the behaviour you see - i.e. triggering in
alphabetical order.

There is no straight forward way to do what you want. The file events
would need to be *buffered* in some way and then sorted into time
order before firing the listeners.

Niall

> any idea ?
>
> Best regards
>
>  *Damien Tacheron*
> Chef de projet
> ------------------------------
> SQLI Paris
> standard : 01 55 93 26 00
> ligne directe : 01 55 93 02 51
> <dt...@hotmail.fr>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org