Author: alexsmirnov
Date: 2008-03-07 21:12:08 -0500 (Fri, 07 Mar 2008)
New Revision: 6627
Modified:
trunk/framework/impl/src/main/java/org/ajax4jsf/component/AjaxViewRoot.java
trunk/framework/impl/src/main/java/org/ajax4jsf/event/EventsQueue.java
trunk/framework/impl/src/test/java/org/ajax4jsf/event/EventsQueueTest.java
Log:
Implement EventsQueue as Java Collection
Modified: trunk/framework/impl/src/main/java/org/ajax4jsf/component/AjaxViewRoot.java
===================================================================
--- trunk/framework/impl/src/main/java/org/ajax4jsf/component/AjaxViewRoot.java 2008-03-08
01:05:20 UTC (rev 6626)
+++ trunk/framework/impl/src/main/java/org/ajax4jsf/component/AjaxViewRoot.java 2008-03-08
02:12:08 UTC (rev 6627)
@@ -132,9 +132,9 @@
// in future, with JSF 1.2 it must be done by invokeOnComponent
// method.
// In 1.1 , events to RENDER_RESPONSE phase not used.
- getAjaxEventsQueue(getFacesContext()).add(event);
+ getAjaxEventsQueue(getFacesContext()).offer(event);
} else {
- getEventsQueue(getFacesContext(), event.getPhaseId()).add(event);
+ getEventsQueue(getFacesContext(), event.getPhaseId()).offer(event);
}
}
Modified: trunk/framework/impl/src/main/java/org/ajax4jsf/event/EventsQueue.java
===================================================================
--- trunk/framework/impl/src/main/java/org/ajax4jsf/event/EventsQueue.java 2008-03-08
01:05:20 UTC (rev 6626)
+++ trunk/framework/impl/src/main/java/org/ajax4jsf/event/EventsQueue.java 2008-03-08
02:12:08 UTC (rev 6627)
@@ -3,6 +3,8 @@
*/
package org.ajax4jsf.event;
+import java.util.AbstractQueue;
+import java.util.Iterator;
import java.util.NoSuchElementException;
import javax.faces.event.FacesEvent;
@@ -12,7 +14,7 @@
* @author asmirnov
*
*/
-public class EventsQueue {
+public class EventsQueue extends AbstractQueue<FacesEvent>{
private QueueElement first;
@@ -25,24 +27,34 @@
* @return faces event form top of queue
* @throws NoSuchElementException , if queue is empty.
*/
- public FacesEvent remove() throws NoSuchElementException {
- if(isEmpty()){
- throw new NoSuchElementException("Events queue is empty");
+ public FacesEvent poll() {
+ FacesEvent element = null;
+ if (!isEmpty()) {
+ element = first.getElement();
+ first = first.getPrevious();
+ if (null == first) {
+ last = null;
+ size = 0;
+ } else {
+ size--;
+ }
}
- FacesEvent element = first.getElement();
- first = first.getPrevious();
- if(null == first){
- last = null;
+ return element;
+ }
+
+ public FacesEvent peek() {
+ FacesEvent element = null;
+ if (!isEmpty()) {
+ element = first.getElement();
}
- size--;
return element;
}
-
/**
* Add event to queue.
* @param element
+ * @return TODO
*/
- public void add(FacesEvent element) {
+ public boolean offer(FacesEvent element) {
QueueElement queueElement = new QueueElement(element);
if(isEmpty()){
first = last = queueElement;
@@ -51,6 +63,7 @@
last = queueElement;
}
size++;
+ return true;
}
public void clear() {
@@ -100,4 +113,31 @@
}
}
+
+ @Override
+ public Iterator<FacesEvent> iterator() {
+
+ return new Iterator<FacesEvent>(){
+
+ private QueueElement current = first;
+
+ public boolean hasNext() {
+ return null != current;
+ }
+
+ public FacesEvent next() {
+ if(null == current){
+ throw new NoSuchElementException();
+ }
+ FacesEvent event = current.getElement();
+ current = current.getPrevious();
+ return event;
+ }
+
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+
+ };
+ }
}
Modified: trunk/framework/impl/src/test/java/org/ajax4jsf/event/EventsQueueTest.java
===================================================================
--- trunk/framework/impl/src/test/java/org/ajax4jsf/event/EventsQueueTest.java 2008-03-08
01:05:20 UTC (rev 6626)
+++ trunk/framework/impl/src/test/java/org/ajax4jsf/event/EventsQueueTest.java 2008-03-08
02:12:08 UTC (rev 6627)
@@ -45,10 +45,10 @@
public void testRemove() {
ActionEvent event = new ActionEvent(new UICommand());
EventsQueue queue = new EventsQueue();
- queue.add(event);
+ queue.offer(event);
assertFalse(queue.isEmpty());
ActionEvent event2 = new ActionEvent(new UICommand());
- queue.add(event2);
+ queue.offer(event2);
assertSame(event, queue.remove());
assertSame(event2, queue.remove());
assertTrue(queue.isEmpty());
@@ -61,15 +61,15 @@
}
/**
- * Test method for {@link
org.ajax4jsf.event.EventsQueue#add(javax.faces.event.FacesEvent)}.
+ * Test method for {@link
org.ajax4jsf.event.EventsQueue#offer(javax.faces.event.FacesEvent)}.
*/
public void testAdd() {
ActionEvent event = new ActionEvent(new UICommand());
EventsQueue queue = new EventsQueue();
- queue.add(event);
+ queue.offer(event);
assertFalse(queue.isEmpty());
ActionEvent event2 = new ActionEvent(new UICommand());
- queue.add(event2);
+ queue.offer(event2);
}
/**
@@ -78,10 +78,10 @@
public void testClear() {
ActionEvent event = new ActionEvent(new UICommand());
EventsQueue queue = new EventsQueue();
- queue.add(event);
+ queue.offer(event);
assertFalse(queue.isEmpty());
ActionEvent event2 = new ActionEvent(new UICommand());
- queue.add(event2);
+ queue.offer(event2);
queue.clear();
assertTrue(queue.isEmpty());
}