001package ball.swing.table;
002/*-
003 * ##########################################################################
004 * Utilities
005 * %%
006 * Copyright (C) 2008 - 2022 Allen D. Ball
007 * %%
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *      http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 * ##########################################################################
020 */
021import java.util.ArrayList;
022import java.util.Collection;
023
024/**
025 * Abstract base class for {@link javax.swing.table.TableModel}
026 * implementations based on an {@link ArrayList}.
027 *
028 * @param       <R>     The type of table row.
029 *
030 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
031 */
032public abstract class ArrayListTableModel<R> extends AbstractTableModelImpl {
033    private static final long serialVersionUID = -7927458440773401575L;
034
035    /** @serial */ private final ArrayList<R> list = new ArrayList<>();
036
037    /**
038     * @see AbstractTableModelImpl#AbstractTableModelImpl(String...)
039     *
040     * @param   iterable        The {@link Iterable} of row values.
041     * @param   names           The column names.
042     */
043    protected ArrayListTableModel(Iterable<? extends R> iterable, String... names) {
044        super(names);
045
046        if (iterable != null) {
047            for (R element : iterable) {
048                list.add(element);
049            }
050        }
051    }
052
053    /**
054     * @see AbstractTableModelImpl#AbstractTableModelImpl(int)
055     *
056     * @param   iterable        The {@link Iterable} of row values.
057     * @param   columns         The number of columns.
058     */
059    protected ArrayListTableModel(Iterable<? extends R> iterable, int columns) {
060        this(iterable, new String[columns]);
061    }
062
063    /**
064     * Method to access the underlying row {@link java.util.List}.
065     *
066     * @return  The underlying row {@link java.util.List}.
067     */
068    public ArrayList<R> list() { return list; }
069
070    @Override
071    public int getRowCount() { return list().size(); }
072
073    /**
074     * Implementation method to retrieve a column value from a row object.
075     *
076     * @param   row             The {@link Object} representing the row.
077     * @param   x               The column index.
078     *
079     * @return  The column value from the row.
080     */
081    protected abstract Object getValueAt(R row, int x);
082
083    @Override
084    public Object getValueAt(int y, int x) {
085        return getColumnClass(x).cast(getValueAt(list().get(y), x));
086    }
087}