I just sent a couple of classes using a standard list iterator/handler
pattern from the core J2EE design patterns book to Uma.k on this list
that answer this with one solution.  I will send it along to you too. 
I think that when you see the code the answer to your question will
become obvious.  ListHandler allows you to access members of the list
in a very sophisticated way.  This is a very handy and simple design
pattern.

Jack

package com.crackwillow.list;

import java.util.Collection;
import java.util.List;

import com.crackwillow.exception.ChainedException;

public interface ListIterator {
  public void       setList(List list) throws ChainedException;
  public Collection getList();
  public int        getSize() throws ChainedException;
  public void       setIndex(int index) throws ChainedException;
  public int        getCurrentIndex() throws ChainedException;
  public Object     getCurrentElement() throws ChainedException;
  public List       getPreviousElements(int count) throws ChainedException;
  public List       getPreviousElements() throws ChainedException;
  public List       getNextElements(int count) throws ChainedException;
  public List       getNextElements() throws ChainedException;
  public void       resetIndex() throws ChainedException;
} /// ;-)

package com.crackwillow.list;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.crackwillow.exception.ChainedException;

public class ListHandler
    implements com.crackwillow.list.ListIterator {
  static Log log = LogFactory.getLog(ListHandler.class);

  private List         list;
  private ListIterator listIterator;

  public ListHandler() {
  }

  public void setList(List list)
      throws ChainedException {
    this.list = list;
    if(list != null) {
      listIterator =  list.listIterator();
    } else {
      throw new ChainedException("ListHandler: setList(List) failed:
list empty");
    }
  }

  public Collection getList() {
    return list;
  }

  public int getSize()
      throws ChainedException{
    int size = 0;
    if (list != null) {
      size = list.size();
    } else {
      throw new ChainedException("ListHandler: getList() failed"); //No Data
    }
    return size;
  }

  public void setIndex(int index)
      throws ChainedException {
    index -= getCurrentIndex();
    int size = list.size();
    if(index > size) {
      throw new ChainedException("ListHandler: setCurrentIndex()
failed -- the current index chosen was greater than size)");
    }
    getNextElements(index);
    return;
  }

  public int getCurrentIndex()
      throws ChainedException {
    // Will not advance iterator
    if (list != null) {
      return listIterator.nextIndex();
    } else {
      throw new ChainedException();
    }
  }

  public Object getCurrentElement()
      throws ChainedException {
    Object obj = null;
    // Will not advance iterator
    if (list != null) {
      int currIndex = listIterator.nextIndex();
      obj = list.get(currIndex);
    } else {
      throw new ChainedException();
    }
    return obj;
  }

  public List getPreviousElements(int count)
      throws ChainedException {
    int        i      = 0;
    Object     object = null;
    LinkedList list   = new LinkedList();
    if (listIterator != null) {
      while(listIterator.hasPrevious() && (i < count)){
        object = listIterator.previous();
        list.add(object);
        i++;
      }
    } else {
      throw new ChainedException("ListHandler:
getPreviousElements(int) failed -- listIterator null."); // No data
    }
    return list;
  }

  public List getPreviousElements()
      throws ChainedException {
    Object object = null;
    LinkedList list = new LinkedList();
    if(listIterator != null) {
      while(listIterator.hasPrevious()) {
        object = listIterator.previous();
        list.add(object);
      }
    } else {
      throw new ChainedException("ListHandler: getNextElements(int)
failed -- listIterator null");
    }
    return list;
  }

  public List getNextElements(int count)
      throws ChainedException {
    int        i      = 0;
    Object     object = null;
    LinkedList list   = new LinkedList();
    if(listIterator != null) {
      while(listIterator.hasNext() && (i < count)) {
        object = listIterator.next();
        list.add(object);
        i++;
      }
    } else {
      throw new ChainedException("ListHandler: getNextElements(int)
failed -- listIterator null.");
    }
    return list;
  }

  public List getNextElements()
      throws ChainedException {
    Object object = null;
    LinkedList list = new LinkedList();
    if(listIterator != null) {
      while(listIterator.hasNext()) {
        object = listIterator.next();
        list.add(object);
      }
    } else {
      throw new ChainedException("ListHandler: getNextElements(int)
failed -- listIterator null");
    }
    return list;
  }

  public List getNext()
      throws ChainedException {
    Object object = null;
    LinkedList list = new LinkedList();

    if(listIterator != null) {
      if(listIterator.hasNext()) {
        object = listIterator.next();
        list.add(object);
      }
    } else {
      throw new ChainedException("ListHandler: getNextElements(int)
failed -- listIterator null");
    }

    return list;
  }

  public void resetIndex()
      throws ChainedException{
    if(listIterator != null){
      listIterator = list.listIterator();
    } else {
      throw new ChainedException("ListHandler: resetIndex() failed --
listIterator null.");
    }
  }
} ///;-)

Jack


On Thu, 23 Dec 2004 13:32:07 -0500, Robert Taylor <[EMAIL PROTECTED]> wrote:
> Vinicius Caldeira Carvalho wrote:
> 
> > Hi there!
> > I have a collection of beans, and this collection is set to the
> > request. I'd like to retreive only the first object of the collection
> > (later on I'll iterate over it completely) How can I do this? My
> > Collection is a List.
> > I tried <c:set var="bean" value="${collection.get(0)}">
> > and also <c:set var="bean" value="{$collection.iterator.next}">
> >
> > none of 'em seems to work. Any ideas?
> >
> > Thanks all
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> >
> >
> >
> You can try the following although it may not work:
> 
> <c:forEach var="bean" items="collection" begin="0" end="0">
> ...
> </c:forEach>
> 
> /robert
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 


-- 
------------------------------

"You can lead a horse to water but you cannot make it float on its back."

~Dakota Jack~

"You can't wake a person who is pretending to be asleep."

~Native Proverb~

"Each man is good in His sight. It is not necessary for eagles to be crows."

~Hunkesni (Sitting Bull), Hunkpapa Sioux~

-----------------------------------------------

"This message may contain confidential and/or privileged information.
If you are not the addressee or authorized to receive this for the
addressee, you must not use, copy, disclose, or take any action based
on this message or any information herein. If you have received this
message in error, please advise the sender immediately by reply e-mail
and delete this message. Thank you for your cooperation."

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to