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;
020
021/**
022 * Custom subclass of AbstractList that, when adding new items, will pass them on to a listener instead of
023 * storing them internally.
024 * 
025 * <p>This is based off of the JAXB "streaming" unmarshalling strategy, which is briefly mentioned here:
026 * 
027 * <p>http://jaxb.java.net/guide/Dealing_with_large_documents.html
028 * 
029 * <p>and is presented in the example code available here:
030 * 
031 * <p>http://jaxb.java.net/2.2.4/
032 * 
033 * @author Kuali Rice Team (rice.collab@kuali.org)
034 */
035public final class RiceXmlImportList<E> extends AbstractList<E> implements Serializable {
036    
037    private static final long serialVersionUID = 1L;
038    
039    /** The listener that this list will pass new items to. */
040    private final RiceXmlListAdditionListener<E> listAdditionListener;
041    
042    /**
043     * Constructs a new streaming list that will pass new items to the given listener instead of storing them.
044     * 
045     * @param listAdditionListener The listener to use.
046     * @throws IllegalArgumentException if listAdditionListener is null.
047     */
048    public RiceXmlImportList(RiceXmlListAdditionListener<E> listAdditionListener) {
049        super();
050        if (listAdditionListener == null) {
051            throw new IllegalArgumentException("listAdditionListener cannot be null");
052        }
053        this.listAdditionListener = listAdditionListener;
054    }
055    
056    /**
057     * Instead of adding the item to the list, simply invoke the appropriate listener.
058     * 
059     * <p>This is based off of the "streaming" unmarshalling strategy used in one of the JAXB sample apps.
060     * 
061     * @return false, since the list never gets altered as a result of invoking this method.
062     */
063    @Override
064    public boolean add(E e) {
065        listAdditionListener.newItemAdded(e);
066        return false;
067    }
068    
069    /**
070     * This method always throws an exception, since the list never contains any items.
071     * 
072     * @throws IndexOutOfBoundsException
073     */
074    @Override
075    public E get(int index) {
076        throw new IndexOutOfBoundsException();
077    }
078
079    /**
080     * This method always returns zero, since items are never actually added to the list.
081     * 
082     * @return zero.
083     */
084    @Override
085    public int size() {
086        return 0;
087    }
088}