Tim,
I'm using your suggestion, and thanks for providing a complete example! I
needed that. :)
(It's here if anyone needs to refer to it:
http://www.mail-archive.com/commons-user%40jakarta.apache.org/msg11963.html )
As written, however, it doesn't rearrange the values when you re-visit an
item. In your example, after '1' gets added for the second time, the list
should be 2,3,4,1 and instead it is still 1,2,3,4. (And that means '1' will
be evicted too soon.)
To fix that, I tried to simply remove any item before I add it, so it will
always go to the end of the list as a new item:
public static void add(Buffer recentVisited, String page) {
recentVisited.remove( page );
try {
recentVisited.add( page );
} catch( IllegalArgumentException iae ) {
// do nothing, buffer will complain if predicate fails.
}
System.out.println( recentVisited );
}
And was surprised to get :
run:
[java] [Page 1]
[java] [Page 1, Page 2]
[java] [Page 1, Page 2, Page 3]
[java] [Page 1, Page 2, Page 3, Page 4]
[java] [Page 2, Page 3, Page 4, Page 1] <--- it's working!
[java] [Page 3, Page 4, Page 1, Page 2]
[java] java.lang.ArrayIndexOutOfBoundsException: -1
...
[java] Caused by: java.lang.ArrayIndexOutOfBoundsException: -1
[java] at
org.apache.commons.collections.buffer.BoundedFifoBuffer$1.remove(BoundedFifo
Buffer.java:347)
[java] at
java.util.AbstractCollection.remove(AbstractCollection.java:255)
[java] at
org.apache.commons.collections.collection.AbstractCollectionDecorator.remove
(AbstractCollectionDecorator.java:103)
[java] at RecentlyVisited.add(RecentlyVisited.java:53)
[java] at RecentlyVisited.main(RecentlyVisited.java:39)
It's coming from the third instance of
add( recentVisited, "Page 1" );
in the example code.
I put together a simple example that demonstrates the problem:
http://wiki.wendysmoak.com/cgi-bin/wiki.pl?CircularFifoBuffer
Bug? Surely I'm not doing anything wrong by calling remove(...) on a
Collection? (Inefficient though it may be, first I just want to see it
work.)
Meanwhile, is there another way I can get the buffer to be in the correct
LRU order? The LRUMap did work, but it's ugly having to put the empty
String into the Map, when I don't need key/value pairs.
Thanks,
--
Wendy Smoak
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]