org.jdesktop.swingx.ws.yahoo.search
Class PagedResultsArrayList<E>

java.lang.Object
  extended by org.jdesktop.beans.AbstractBean
      extended by org.jdesktop.swingx.ws.yahoo.search.PagedResultsArrayList<E>
All Implemented Interfaces:
Iterable<E>, Collection<E>, List<E>, PagedResultsList<E>, ResultsList<E>

public class PagedResultsArrayList<E>
extends org.jdesktop.beans.AbstractBean
implements List<E>, PagedResultsList<E>

A non visual JavaBean that represents the results of a Yahoo! search. Unlike ResultsList which only contains the results of a single "page" of search results, PagedResultsArrayList allows you to step through all of the results returned from a search.

Sample usage:


      YahooWebSearch yahoo = new YahooWebSearch();
      yahoo.appId = "myappidxxx(get one from yahoo)";
      yahoo.query = "world cup 2006 Italy";
      PagedResultsArrayList results = new
              PagedResultsArrayList();
      results.setYahooSearch(yahoo);
      results.refresh();
      
      System.out.println("NumPages: " + results.getNumPages());
      System.out.println("NumResults: " + results.getTotalResultsAvailable());
      
      int count = 1;
      while (count < 300 && results.hasNextPage()) {
          for (int i=0; i


Constructor Summary
PagedResultsArrayList()
          Creates a new instance of PagedResultsArrayList
 
Method Summary
 boolean add(E e)
          Appends the specified element to the end of this list (optional operation).
 void add(int index, E element)
          Inserts the specified element at the specified position in this list (optional operation).
 boolean addAll(Collection<? extends E> c)
          Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator (optional operation).
 boolean addAll(int index, Collection<? extends E> c)
          Inserts all of the elements in the specified collection into this list at the specified position (optional operation).
 void clear()
          Removes all of the elements from this list (optional operation).
 boolean contains(Object o)
          Returns true if this list contains the specified element.
 boolean containsAll(Collection<?> c)
          Returns true if this list contains all of the elements of the specified collection.
 boolean equals(Object o)
          Indicates whether some other object is "equal to" this one.
 boolean firstPage()
          Reloads this PagedResultsList by searching the YahooSearch component for the first page of results.
 E get(int index)
          Returns the element at the specified position in this list.
 int getCurrentPageIndex()
           
 int getFirstResultPosition()
           
 int getNumPages()
           
 int getResultsPerPage()
           
 int getTotalResultsAvailable()
           
 YahooSearch getYahooSearch()
           
 boolean gotoPage(int index)
          Reloads this PagedResultsList by searching the YahooSearch component for the given page of results.
 int hashCode()
          Returns a hash code value for the object.
 int indexOf(Object o)
          Returns the index in this list of the first occurrence of the specified element, or -1 if this list does not contain this element.
 boolean isEmpty()
          Returns true if this list contains no elements.
 boolean isHasNextPage()
           
 boolean isHasPreviousPage()
           
 Iterator<E> iterator()
          Returns an iterator over the elements in this list in proper sequence.
 int lastIndexOf(Object o)
          Returns the index in this list of the last occurrence of the specified element, or -1 if this list does not contain this element.
 boolean lastPage()
          Reloads this PagedResultsList by searching the YahooSearch component for the last page of results.
 ListIterator<E> listIterator()
          Returns a list iterator of the elements in this list (in proper sequence).
 ListIterator<E> listIterator(int index)
          Returns a list iterator of the elements in this list (in proper sequence), starting at the specified position in this list.
 boolean nextPage()
          Reloads this PagedResultsList by searching the YahooSearch component for the next page of results.
 boolean previousPage()
          Reloads this PagedResultsList by searching the YahooSearch component for the previous page of results.
 void refresh()
          Reloads this PagedResultsList by searching the YahooSearch component for the current page of results.
 E remove(int index)
          Removes the element at the specified position in this list (optional operation).
 boolean remove(Object o)
          Removes the first occurrence in this list of the specified element (optional operation).
 boolean removeAll(Collection<?> c)
          Removes from this list all the elements that are contained in the specified collection (optional operation).
 boolean retainAll(Collection<?> c)
          Retains only the elements in this list that are contained in the specified collection (optional operation).
 E set(int index, E element)
          Replaces the element at the specified position in this list with the specified element (optional operation).
 void setResultsPerPage(int value)
           
 void setYahooSearch(YahooSearch search)
          Sets the YahooSearch component to use for populating this PagedResultsList.
 int size()
          Returns the number of elements in this list.
 List<E> subList(int fromIndex, int toIndex)
          Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.
 Object[] toArray()
          Returns an array containing all of the elements in this list in proper sequence.
<T> T[]
toArray(T[] a)
          Returns an array containing all of the elements in this list in proper sequence; the runtime type of the returned array is that of the specified array.
 
Methods inherited from class org.jdesktop.beans.AbstractBean
addPropertyChangeListener, addPropertyChangeListener, addVetoableChangeListener, addVetoableChangeListener, clone, fireIndexedPropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, fireVetoableChange, getPropertyChangeListeners, getPropertyChangeListeners, getVetoableChangeListeners, getVetoableChangeListeners, hasPropertyChangeListeners, hasVetoableChangeListeners, removePropertyChangeListener, removePropertyChangeListener, removeVetoableChangeListener, removeVetoableChangeListener
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PagedResultsArrayList

public PagedResultsArrayList()
Creates a new instance of PagedResultsArrayList

Method Detail

getNumPages

public int getNumPages()
Specified by:
getNumPages in interface PagedResultsList<E>
Returns:
the number of total pages in this results list. This value can change whenever refresh() is called, or whenever the page index is changed via one of the page modification methods (firstPage, lastPage, etc).

nextPage

public boolean nextPage()
Description copied from interface: PagedResultsList

Reloads this PagedResultsList by searching the YahooSearch component for the next page of results. If hasNextPage returns false, this method does nothing and returns false. If the YahooSearch component is not set, this method returns false.

This method blocks, and should only be called from org.jdesktop.swingx.ws.BackgroundWorker or SwingWorker or another background threading library.

Specified by:
nextPage in interface PagedResultsList<E>
Returns:
true upon a successful loading of the next page.

previousPage

public boolean previousPage()
Description copied from interface: PagedResultsList

Reloads this PagedResultsList by searching the YahooSearch component for the previous page of results. If hasPrevPage returns false, this method does nothing and returns false. If the YahooSearch component is not set, this method returns false.

This method blocks, and should only be called from org.jdesktop.swingx.ws.BackgroundWorker or SwingWorker or another background threading library.

Specified by:
previousPage in interface PagedResultsList<E>
Returns:
true upon a successful loading of the previous page.

firstPage

public boolean firstPage()
Description copied from interface: PagedResultsList

Reloads this PagedResultsList by searching the YahooSearch component for the first page of results. If the YahooSearch component is not set, this method returns false.

This method blocks, and should only be called from org.jdesktop.swingx.ws.BackgroundWorker or SwingWorker or another background threading library.

Specified by:
firstPage in interface PagedResultsList<E>
Returns:
true upon a successful loading of the first page.

lastPage

public boolean lastPage()
Description copied from interface: PagedResultsList

Reloads this PagedResultsList by searching the YahooSearch component for the last page of results. If the YahooSearch component is not set, this method returns false.

This method blocks, and should only be called from org.jdesktop.swingx.ws.BackgroundWorker or SwingWorker or another background threading library.

Specified by:
lastPage in interface PagedResultsList<E>
Returns:
true upon a successful loading of the last page.

gotoPage

public boolean gotoPage(int index)
Description copied from interface: PagedResultsList

Reloads this PagedResultsList by searching the YahooSearch component for the given page of results. The index is 0 based. If the YahooSearch component is not set, this method returns false.

This method blocks, and should only be called from org.jdesktop.swingx.ws.BackgroundWorker or SwingWorker or another background threading library.

Specified by:
gotoPage in interface PagedResultsList<E>
Parameters:
index - a 0 based index of the page to load. If index > getNumPages(), then an IllegalArgumentException is thrown. Likewise if index < 0.
Returns:
true upon a successful loading of the given page.

isHasPreviousPage

public boolean isHasPreviousPage()
Specified by:
isHasPreviousPage in interface PagedResultsList<E>
Returns:
true if, according to getNumPages(), there is a previous page of data from that currently loaded in PagedResultsList. If the YahooSearch component is not set, this method returns false.

isHasNextPage

public boolean isHasNextPage()
Specified by:
isHasNextPage in interface PagedResultsList<E>
Returns:
true if, according to getNumPages(), there is another page of data after that currently loaded in PagedResultsList. If the YahooSearch component is not set, this method returns false.

refresh

public void refresh()
Description copied from interface: PagedResultsList

Reloads this PagedResultsList by searching the YahooSearch component for the current page of results. If the YahooSearch component is not set, this method returns false.

This method blocks, and should only be called from org.jdesktop.swingx.ws.BackgroundWorker or SwingWorker or another background threading library.

Specified by:
refresh in interface PagedResultsList<E>

setYahooSearch

public final void setYahooSearch(YahooSearch search)
Description copied from interface: PagedResultsList
Sets the YahooSearch component to use for populating this PagedResultsList. Setting this property while this list is populated with results will not clear those results.

Specified by:
setYahooSearch in interface PagedResultsList<E>
Parameters:
search - the YahooSearch component to use for populating this PagedResultsList. This may be null.

getYahooSearch

public final YahooSearch getYahooSearch()
Specified by:
getYahooSearch in interface ResultsList<E>
Returns:
the YahooSearch that was used to produce this ResultsList. May be null.

getTotalResultsAvailable

public final int getTotalResultsAvailable()
Specified by:
getTotalResultsAvailable in interface ResultsList<E>
Returns:
the total results available, not to be confused with the total results in this ResultsList. This method returns the total number of search results associated with the search that produced this ResultsList.

getFirstResultPosition

public final int getFirstResultPosition()
Specified by:
getFirstResultPosition in interface ResultsList<E>
Returns:
the 1 based index into the totalResultsAvailable that this ResultsList is populated with. For example, if there are 100 total results, and this list contains results 30-40, then this method will return 30.

getCurrentPageIndex

public final int getCurrentPageIndex()

setResultsPerPage

public final void setResultsPerPage(int value)

getResultsPerPage

public final int getResultsPerPage()

size

public int size()
Description copied from interface: java.util.List
Returns the number of elements in this list. If this list contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

Specified by:
size in interface Collection<E>
Specified by:
size in interface List<E>
Returns:
the number of elements in this list.

isEmpty

public boolean isEmpty()
Description copied from interface: java.util.List
Returns true if this list contains no elements.

Specified by:
isEmpty in interface Collection<E>
Specified by:
isEmpty in interface List<E>
Returns:
true if this list contains no elements.

contains

public boolean contains(Object o)
Description copied from interface: java.util.List
Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).

Specified by:
contains in interface Collection<E>
Specified by:
contains in interface List<E>
Parameters:
o - element whose presence in this list is to be tested.
Returns:
true if this list contains the specified element.

iterator

public Iterator<E> iterator()
Description copied from interface: java.util.List
Returns an iterator over the elements in this list in proper sequence.

Specified by:
iterator in interface Iterable<E>
Specified by:
iterator in interface Collection<E>
Specified by:
iterator in interface List<E>
Returns:
an iterator over the elements in this list in proper sequence.

toArray

public Object[] toArray()
Description copied from interface: java.util.List
Returns an array containing all of the elements in this list in proper sequence. Obeys the general contract of the Collection.toArray method.

Specified by:
toArray in interface Collection<E>
Specified by:
toArray in interface List<E>
Returns:
an array containing all of the elements in this list in proper sequence.
See Also:
Arrays.asList(Object[])

toArray

public <T> T[] toArray(T[] a)
Description copied from interface: java.util.List
Returns an array containing all of the elements in this list in proper sequence; the runtime type of the returned array is that of the specified array. Obeys the general contract of the Collection.toArray(Object[]) method.

Specified by:
toArray in interface Collection<E>
Specified by:
toArray in interface List<E>
Parameters:
a - the array into which the elements of this list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
Returns:
an array containing the elements of this list.

add

public boolean add(E e)
Description copied from interface: java.util.List
Appends the specified element to the end of this list (optional operation).

Lists that support this operation may place limitations on what elements may be added to this list. In particular, some lists will refuse to add null elements, and others will impose restrictions on the type of elements that may be added. List classes should clearly specify in their documentation any restrictions on what elements may be added.

Specified by:
add in interface Collection<E>
Specified by:
add in interface List<E>
Parameters:
e - element to be appended to this list.
Returns:
true (as per the general contract of the Collection.add method).

remove

public boolean remove(Object o)
Description copied from interface: java.util.List
Removes the first occurrence in this list of the specified element (optional operation). If this list does not contain the element, it is unchanged. More formally, removes the element with the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))) (if such an element exists).

Specified by:
remove in interface Collection<E>
Specified by:
remove in interface List<E>
Parameters:
o - element to be removed from this list, if present.
Returns:
true if this list contained the specified element.

containsAll

public boolean containsAll(Collection<?> c)
Description copied from interface: java.util.List
Returns true if this list contains all of the elements of the specified collection.

Specified by:
containsAll in interface Collection<E>
Specified by:
containsAll in interface List<E>
Parameters:
c - collection to be checked for containment in this list.
Returns:
true if this list contains all of the elements of the specified collection.
See Also:
List.contains(Object)

addAll

public boolean addAll(Collection<? extends E> c)
Description copied from interface: java.util.List
Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator (optional operation). The behavior of this operation is unspecified if the specified collection is modified while the operation is in progress. (Note that this will occur if the specified collection is this list, and it's nonempty.)

Specified by:
addAll in interface Collection<E>
Specified by:
addAll in interface List<E>
Parameters:
c - collection whose elements are to be added to this list.
Returns:
true if this list changed as a result of the call.
See Also:
List.add(Object)

addAll

public boolean addAll(int index,
                      Collection<? extends E> c)
Description copied from interface: java.util.List
Inserts all of the elements in the specified collection into this list at the specified position (optional operation). Shifts the element currently at that position (if any) and any subsequent elements to the right (increases their indices). The new elements will appear in this list in the order that they are returned by the specified collection's iterator. The behavior of this operation is unspecified if the specified collection is modified while the operation is in progress. (Note that this will occur if the specified collection is this list, and it's nonempty.)

Specified by:
addAll in interface List<E>
Parameters:
index - index at which to insert first element from the specified collection.
c - elements to be inserted into this list.
Returns:
true if this list changed as a result of the call.

removeAll

public boolean removeAll(Collection<?> c)
Description copied from interface: java.util.List
Removes from this list all the elements that are contained in the specified collection (optional operation).

Specified by:
removeAll in interface Collection<E>
Specified by:
removeAll in interface List<E>
Parameters:
c - collection that defines which elements will be removed from this list.
Returns:
true if this list changed as a result of the call.
See Also:
List.remove(Object), List.contains(Object)

retainAll

public boolean retainAll(Collection<?> c)
Description copied from interface: java.util.List
Retains only the elements in this list that are contained in the specified collection (optional operation). In other words, removes from this list all the elements that are not contained in the specified collection.

Specified by:
retainAll in interface Collection<E>
Specified by:
retainAll in interface List<E>
Parameters:
c - collection that defines which elements this set will retain.
Returns:
true if this list changed as a result of the call.
See Also:
List.remove(Object), List.contains(Object)

clear

public void clear()
Description copied from interface: java.util.List
Removes all of the elements from this list (optional operation). This list will be empty after this call returns (unless it throws an exception).

Specified by:
clear in interface Collection<E>
Specified by:
clear in interface List<E>

equals

public boolean equals(Object o)
Description copied from class: java.lang.Object
Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).

Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.

Specified by:
equals in interface Collection<E>
Specified by:
equals in interface List<E>
Overrides:
equals in class Object
Parameters:
o - the reference object with which to compare.
Returns:
true if this object is the same as the obj argument; false otherwise.
See Also:
Object.hashCode(), Hashtable

hashCode

public int hashCode()
Description copied from class: java.lang.Object
Returns a hash code value for the object. This method is supported for the benefit of hashtables such as those provided by java.util.Hashtable.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the Object.equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)

Specified by:
hashCode in interface Collection<E>
Specified by:
hashCode in interface List<E>
Overrides:
hashCode in class Object
Returns:
a hash code value for this object.
See Also:
Object.equals(java.lang.Object), Hashtable

get

public E get(int index)
Description copied from interface: java.util.List
Returns the element at the specified position in this list.

Specified by:
get in interface List<E>
Parameters:
index - index of element to return.
Returns:
the element at the specified position in this list.

set

public E set(int index,
             E element)
Description copied from interface: java.util.List
Replaces the element at the specified position in this list with the specified element (optional operation).

Specified by:
set in interface List<E>
Parameters:
index - index of element to replace.
element - element to be stored at the specified position.
Returns:
the element previously at the specified position.

add

public void add(int index,
                E element)
Description copied from interface: java.util.List
Inserts the specified element at the specified position in this list (optional operation). Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).

Specified by:
add in interface List<E>
Parameters:
index - index at which the specified element is to be inserted.
element - element to be inserted.

remove

public E remove(int index)
Description copied from interface: java.util.List
Removes the element at the specified position in this list (optional operation). Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the list.

Specified by:
remove in interface List<E>
Parameters:
index - the index of the element to removed.
Returns:
the element previously at the specified position.

indexOf

public int indexOf(Object o)
Description copied from interface: java.util.List
Returns the index in this list of the first occurrence of the specified element, or -1 if this list does not contain this element. More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.

Specified by:
indexOf in interface List<E>
Parameters:
o - element to search for.
Returns:
the index in this list of the first occurrence of the specified element, or -1 if this list does not contain this element.

lastIndexOf

public int lastIndexOf(Object o)
Description copied from interface: java.util.List
Returns the index in this list of the last occurrence of the specified element, or -1 if this list does not contain this element. More formally, returns the highest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.

Specified by:
lastIndexOf in interface List<E>
Parameters:
o - element to search for.
Returns:
the index in this list of the last occurrence of the specified element, or -1 if this list does not contain this element.

listIterator

public ListIterator<E> listIterator()
Description copied from interface: java.util.List
Returns a list iterator of the elements in this list (in proper sequence).

Specified by:
listIterator in interface List<E>
Returns:
a list iterator of the elements in this list (in proper sequence).

listIterator

public ListIterator<E> listIterator(int index)
Description copied from interface: java.util.List
Returns a list iterator of the elements in this list (in proper sequence), starting at the specified position in this list. The specified index indicates the first element that would be returned by an initial call to the next method. An initial call to the previous method would return the element with the specified index minus one.

Specified by:
listIterator in interface List<E>
Parameters:
index - index of first element to be returned from the list iterator (by a call to the next method).
Returns:
a list iterator of the elements in this list (in proper sequence), starting at the specified position in this list.

subList

public List<E> subList(int fromIndex,
                       int toIndex)
Description copied from interface: java.util.List
Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned list is empty.) The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa. The returned list supports all of the optional list operations supported by this list.

This method eliminates the need for explicit range operations (of the sort that commonly exist for arrays). Any operation that expects a list can be used as a range operation by passing a subList view instead of a whole list. For example, the following idiom removes a range of elements from a list:

            list.subList(from, to).clear();
 
Similar idioms may be constructed for indexOf and lastIndexOf, and all of the algorithms in the Collections class can be applied to a subList.

The semantics of the list returned by this method become undefined if the backing list (i.e., this list) is structurally modified in any way other than via the returned list. (Structural modifications are those that change the size of this list, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results.)

Specified by:
subList in interface List<E>
Parameters:
fromIndex - low endpoint (inclusive) of the subList.
toIndex - high endpoint (exclusive) of the subList.
Returns:
a view of the specified range within this list.