001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.core.util.jaxb;
017
018import java.io.Serializable;
019import java.util.AbstractList;
020import java.util.List;
021
022/**
023 * Custom subclass of AbstractList that, whenever the "get" method is called, will pass an
024 * internally-stored list's object to the given listener for conversion into another object matching
025 * the list's type. This allows for the marshalling process to discard generated items after they
026 * have been marshalled.
027 * 
028 * <p>These lists are constructed by passing in another list containing the unconverted items,
029 * as well as a listener that will create items of this list's type upon each invocation of
030 * the "get" method.
031 * 
032 * <p>This is similar to the "streaming" unmarshalling strategy used in the RiceXmlImportList
033 * class, except that this list has been adapted for marshalling instead.
034 * 
035 * @param E The type that the list is expected to return.
036 * @param T The type that the list stores internally and passes to the listener for conversion as needed. 
037 * 
038 * @author Kuali Rice Team (rice.collab@kuali.org)
039 */
040public final class RiceXmlExportList<E,T> extends AbstractList<E> implements Serializable {
041
042    private static final long serialVersionUID = 1L;
043    
044    private final List<? extends T> sourceList;
045    private final RiceXmlListGetterListener<E,T> listGetterListener;
046    
047    /**
048     * Constructs a new export list that will rely on the given listener for converting the provided
049     * list's items into the appropriate type.
050     * 
051     * @param sourceList The list of objects to convert.
052     * @param listGetterListener The listener to use.
053     * @throws IllegalArgumentException if sourceList or listGetterListener are null.
054     */
055    public RiceXmlExportList(List<? extends T> sourceList, RiceXmlListGetterListener<E,T> listGetterListener) {
056        super();
057        if (sourceList == null) {
058            throw new IllegalArgumentException("sourceList cannot be null");
059        } else if (listGetterListener == null) {
060            throw new IllegalArgumentException("listGetterListener cannot be null");
061        }
062        this.sourceList = sourceList;
063        this.listGetterListener = listGetterListener;
064    }
065    
066    /**
067     * Passes the item at the given index of the internal list to the listener, and then returns
068     * the listener's result.
069     * 
070     * @param index The unconverted item's index in the internal list.
071     * @return The item converted by the listener at the given list index.
072     */
073    @Override
074    public E get(int index) {
075        return listGetterListener.gettingNextItem(sourceList.get(index), index);
076    }
077
078    /**
079     * Returns the size of the internal list.
080     * 
081     * @return The size of the internal list.
082     */
083    @Override
084    public int size() {
085        return sourceList.size();
086    }
087
088}