Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * $Id: SelectItemsIterator.java,v 1.16 2007/01/29 07:56:05 rlubke Exp $
   */
  
  /*
   * The contents of this file are subject to the terms
   * of the Common Development and Distribution License
   * (the License). You may not use this file except in
   * compliance with the License.
  * 
  * You can obtain a copy of the License at
  * https://javaserverfaces.dev.java.net/CDDL.html or
  * legal/CDDLv1.0.txt. 
  * See the License for the specific language governing
  * permission and limitations under the License.
  * 
  * When distributing Covered Code, include this CDDL
  * Header Notice in each file and include the License file
  * at legal/CDDLv1.0.txt.    
  * If applicable, add the following below the CDDL Header,
  * with the fields enclosed by brackets [] replaced by
  * your own identifying information:
  * "Portions Copyrighted [year] [name of copyright owner]"
  * 
  * [Name of File] [ver.__] [Date]
  * 
  * Copyright 2005 Sun Microsystems Inc. All Rights Reserved
  */
 
 package javax.faces.component;
 
 
 import java.util.List;
 import java.util.Map;
 

Package private class for iterating over the set of javax.faces.model.SelectItems for a parent UISelectMany or UISelectOne.

 
 
 final class SelectItemsIterator implements Iterator<SelectItem> {
 
 
     // ------------------------------------------------------------ Constructors
 

    

Construct an iterator instance for the specified parent component.

Parameters:
parent The parent UIComponent whose children will be processed
 
     public SelectItemsIterator(UIComponent parent) {
 
          = parent.getChildren().listIterator();
 
     }
 
 
     // ------------------------------------------------------ Instance Variables
 

    

Iterator over the SelectItem elements pointed at by a UISelectItems component, or null.

 
     private Iterator<SelectItemitems = null;


    

Iterator over the children of the parent component.

 
     private ListIterator<UIComponentkids = null;
 
 
     // -------------------------------------------------------- Iterator Methods
 

    

Return true if the iteration has more elements.

 
     public boolean hasNext() {
 
         if ( != null) {
             if (.hasNext()) {
                 return (true);
             } else {
                  = null;
             }
         }
         Object next = findNextValidChild();
        if (next != null) {
            .previous();
            return true;
        }
        return false;
    }


    

Return the next element in the iteration.

Throws:
java.util.NoSuchElementException if there are no more elements
    public SelectItem next() {
        if (!hasNext()) {
            throw new NoSuchElementException();
        }
        if ( != null) {
            return (.next());
        }
        UIComponent kid = (UIComponentfindNextValidChild();
        if (kid instanceof UISelectItem) {
            UISelectItem ui = (UISelectItemkid;
            SelectItem item = (SelectItemui.getValue();
            if (item == null) {
                item = new SelectItem(ui.getItemValue(),
                                      ui.getItemLabel(),
                                      ui.getItemDescription(),
                                      ui.isItemDisabled(), 
                                      ui.isItemEscaped());
            }
            return (item);
        } else if (kid instanceof UISelectItems) {
            UISelectItems ui = (UISelectItemskid;
            Object value = ui.getValue();
            if (value instanceof SelectItem) {
                return ((SelectItem)value);
            } else if (value instanceof SelectItem[]) {
                 = Arrays.asList((SelectItem[]) value).iterator();
                return (next());
            } else if (value instanceof List) {
                 = TypedCollections.dynamicallyCastList((ListvalueSelectItem.class).iterator();
                return (next());
            } else if (value instanceof Map) {
                List<SelectItemlist = new ArrayList<SelectItem>(((Mapvalue).size());
                for (Iterator keys = ((Mapvalue).keySet().iterator();
                    keys.hasNext(); ) {
                    Object key = keys.next();
                    if (key == null) {
                        continue;
                    }
                    Object val = ((Mapvalue).get(key);
                    if (val == null) {
                        continue;
                    }
                    list.add(new SelectItem(valkey.toString(),
                        null));
                }
                 = list.iterator();
                return (next());
            } else {
                throw new IllegalArgumentException();
            }
        } else {
            throw new NoSuchElementException();
        }
    }


    

Throw UnsupportedOperationException.

    public void remove() {
        throw new UnsupportedOperationException();
    }
    private Object findNextValidChild() {
        if (.hasNext()) {
            Object next = .next();
            while (.hasNext() && !(next instanceof UISelectItem || next instanceof UISelectItems)) {
                next = .next();
            }
            if (next instanceof UISelectItem || next instanceof UISelectItems) {
                return next;
            }
        }
        return null;
    }
New to GrepCode? Check out our FAQ X