All Downloads are FREE. Search and download functionalities are using the official Maven repository.

javax.faces.model.IterableDataModel Maven / Gradle / Ivy

Go to download

This is the master POM file for Oracle's Implementation of the JSF 2.3 Specification.

There is a newer version: 2.3-pfd
Show newest version
/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 1997-2015 Oracle and/or its affiliates. All rights reserved.
 *
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common Development
 * and Distribution License("CDDL") (collectively, the "License").  You
 * may not use this file except in compliance with the License.  You can
 * obtain a copy of the License at
 * https://glassfish.java.net/public/CDDL+GPL_1_1.html
 * or packager/legal/LICENSE.txt.  See the License for the specific
 * language governing permissions and limitations under the License.
 *
 * When distributing the software, include this License Header Notice in each
 * file and include the License file at packager/legal/LICENSE.txt.
 *
 * GPL Classpath Exception:
 * Oracle designates this particular file as subject to the "Classpath"
 * exception as provided by Oracle in the GPL Version 2 section of the License
 * file that accompanied this code.
 *
 * Modifications:
 * If applicable, add the following below the License Header, with the fields
 * enclosed by brackets [] replaced by your own identifying information:
 * "Portions Copyright [year] [name of copyright owner]"
 *
 * Contributor(s):
 * If you wish your version of this file to be governed by only the CDDL or
 * only the GPL Version 2, indicate your decision by adding "[Contributor]
 * elects to include this software in this distribution under the [CDDL or GPL
 * Version 2] license."  If you don't indicate a single choice of license, a
 * recipient has the option to distribute your version of this file under
 * either the CDDL, the GPL Version 2 or to extend the choice of license to
 * its licensees as provided above.  However, if you add GPL Version 2 code
 * and therefore, elected the GPL Version 2 license, then the option applies
 * only if the new code is made subject to such option by the copyright
 * holder.
 */
package javax.faces.model;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.faces.model.DataModel;
import javax.faces.model.DataModelEvent;
import javax.faces.model.DataModelListener;
import javax.faces.model.ListDataModel;

/**
 * 

IterableDataModel is an * implementation of {@link DataModel} that wraps an Iterable.

* *

* This can be used to encapsulate nearly every collection type, including * {@link Collection} derived types such as {@link List} and {@link Set}. * As such this specific DataModel can be used instead of more specific * DataModels like {@link ListDataModel} and {@link CollectionDataModel}. * */ public class IterableDataModel extends DataModel { /** * The current row index. */ private int index = -1; /** * The iterable that this data model is primarily wrapping. */ private Iterable iterable; /** * A list that's optionally used to hold and cache data collected * from the iterable that this data model is wrapping. */ private List list; /** *

Construct a new {@link IterableDataModel} with no specified * wrapped data.

*/ public IterableDataModel() { this(null); } /** *

Construct a new {@link IterableDataModel} wrapping the specified * iterable.

* * @param iterable Iterable to be wrapped. */ public IterableDataModel(Iterable iterable) { setWrappedData(iterable); } /** *

Return a flag indicating whether there is rowData * available at the current rowIndex. If no * wrappedData is available, return false.

* * @throws javax.faces.FacesException if an error occurs getting the row availability */ @Override public boolean isRowAvailable() { return list != null && index >= 0 && index < list.size(); } /** *

Return the number of rows of data objects represented by this * {@link DataModel}. If the number of rows is unknown, or no * wrappedData is available, return -1.

* * @throws javax.faces.FacesException if an error occurs getting the row count */ @Override public int getRowCount() { if (list == null) { return -1; } return list.size(); } /** *

Return an object representing the data for the currenty selected * row index. If no wrappedData is available, return * null.

* * @throws javax.faces.FacesException if an error occurs getting the row data * @throws IllegalArgumentException if now row data is available * at the currently specified row index */ @Override public E getRowData() { if (list == null) { return null; } if (!isRowAvailable()) { throw new IllegalStateException(); } return list.get(index); } /** *

Return the zero-relative index of the currently selected row. If * we are not currently positioned on a row, or no wrappedData * is available, return -1.

* * @throws javax.faces.FacesException if an error occurs getting the row index */ @Override public int getRowIndex() { return index; } /** *

Set the zero-relative index of the currently selected row, or -1 * to indicate that we are not positioned on a row. It is * possible to set the row index at a value for which the underlying data * collection does not contain any row data. Therefore, callers may * use the isRowAvailable() method to detect whether row data * will be available for use by the getRowData() method.

* *

If there is no wrappedData available when this method * is called, the specified rowIndex is stored (and may be * retrieved by a subsequent call to getRowData()), but no * event is sent. Otherwise, if the currently selected row index is * changed by this call, a {@link DataModelEvent} will be sent to the * rowSelected() method of all registered * {@link DataModelListener}s.

* * @param rowIndex The new zero-relative index (must be non-negative) * * @throws javax.faces.FacesException if an error occurs setting the row index * @throws IllegalArgumentException if rowIndex * is less than -1 */ @Override public void setRowIndex(int rowIndex) { if (rowIndex < -1) { throw new IllegalArgumentException(); } int oldRowIndex = index; index = rowIndex; if (list == null) { return; } notifyListeners(oldRowIndex, rowIndex); } /** *

Return the object representing the data wrapped by this * {@link DataModel}, if any.

*/ @Override public Object getWrappedData() { return iterable; } /** *

Set the object representing the data collection wrapped by this * {@link DataModel}. If the specified data is * null, detach this {@link DataModel} from any previously * wrapped data collection instead.

* *

If data is non-null, the currently selected * row index must be set to zero, and a {@link DataModelEvent} must be sent * to the rowSelected() method of all registered * {@link DataModelListener}s indicating that this row is now selected.

* * @param data Data collection to be wrapped, or null to * detach from any previous data collection * * @throws ClassCastException if data is not of the * appropriate type for this {@link DataModel} implementation */ @SuppressWarnings("unchecked") @Override public void setWrappedData(Object data) { if (data == null) { iterable = null; list = null; setRowIndex(-1); } else { iterable = (Iterable) data; list = iterableToList(iterable); setRowIndex(0); } } /** *

Return an object representing the data for the currently selected * row index. If either no wrappedData is available or if * there is no rowDataavailable at the current rowIndex, * return null.

* * @return data for the currently selected row index, or null if there's no data */ private E getRowDataOrNull() { if (isRowAvailable()) { return getRowData(); } return null; } /** * Notifies all DataModelListeners * * @param oldRowIndex the previous index * @param rowIndex The current zero-relative index (must be non-negative) */ private void notifyListeners(int oldRowIndex, int rowIndex) { DataModelListener[] dataModelListeners = getDataModelListeners(); if (oldRowIndex != rowIndex && dataModelListeners != null) { DataModelEvent dataModelEvent = new DataModelEvent(this, rowIndex, getRowDataOrNull()); for (DataModelListener dataModelListener : dataModelListeners) { if (dataModelListener != null) { dataModelListener.rowSelected(dataModelEvent); } } } } /** * Converts an iterable into a list. *

* This method makes NO guarantee to whether changes to the source iterable are * reflected in the returned list or not. For instance if the given iterable * already is a list, it's returned directly. * * @param The generic iterable element type. * @param iterable The iterable to be converted. * @return The list representation of the given iterable, possibly the same instance as that iterable. */ private static List iterableToList(Iterable iterable) { List list = null; if (iterable instanceof List) { list = (List) iterable; } else if (iterable instanceof Collection) { list = new ArrayList<>((Collection) iterable); } else { list = new ArrayList<>(); Iterator iterator = iterable.iterator(); while (iterator.hasNext()) { list.add(iterator.next()); } } return list; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy