    001/* ===========================================================
    002 * Orson Charts : a 3D chart library for the Java(tm) platform
    003 * ===========================================================
    004 *
    005 * (C)opyright 2013-2022, by David Gilbert.  All rights reserved.
    006 *
    007 * https://github.com/jfree/orson-charts
    008 *
    009 * This program is free software: you can redistribute it and/or modify
    010 * it under the terms of the GNU General Public License as published by
    011 * the Free Software Foundation, either version 3 of the License, or
    012 * (at your option) any later version.
    013 *
    014 * This program is distributed in the hope that it will be useful,
    015 * but WITHOUT ANY WARRANTY; without even the implied warranty of
    016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    017 * GNU General Public License for more details.
    018 *
    019 * You should have received a copy of the GNU General Public License
    020 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    021 *
    022 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    023 * Other names may be trademarks of their respective owners.]
    024 *
    025 * If you do not wish to be bound by the terms of the GPL, an alternative
    026 * commercial license can be purchased.  For details, please see visit the
    027 * Orson Charts home page:
    028 *
    029 * http://www.object-refinery.com/orsoncharts/index.html
    030 *
    031 */
    032
    033package org.jfree.chart3d.interaction;
    034
    035import java.io.Serializable;
    036import java.util.Collection;
    037import java.util.Set;
    038import java.util.TreeSet;
    039import org.jfree.chart3d.data.KeyedValues3D;
    040import org.jfree.chart3d.data.KeyedValues3DItemKey;
    041import org.jfree.chart3d.internal.Args;
    042
    043/**
    044 * An object that tracks selected items from a {@link KeyedValues3D} dataset.
    045 *
    046 * @since 1.3
    047 */
    048public class StandardKeyedValues3DItemSelection
    049        implements KeyedValues3DItemSelection, Serializable {
    050
    051    /** The set of selected items. */
    052    Set<KeyedValues3DItemKey> selectedItems;
    053
    054    /**
    055     * Creates a new item selection instance, initially with no selections.
    056     */
    057    public StandardKeyedValues3DItemSelection() {
    058        this.selectedItems = new TreeSet<>();
    059    }
    060
    061    /**
    062     * Adds an item to the selection, returning {@code true} if the item
    063     * is added and {@code false} if the item already existed in the
    064     * selection.
    065     *
    066     * @param itemKey  the item key ({@code null} not permitted).
    067     *
    068     * @return A boolean.
    069     */
    070    public boolean add(KeyedValues3DItemKey itemKey) {
    071        Args.nullNotPermitted(itemKey, "itemKey");
    072        return this.selectedItems.add(itemKey);
    073    }
    074
    075    /**
    076     * Adds a collection of items to the selection, returning {@code true}
    077     * if the selection is changed, and {@code false} if no changes were
    078     * made.
    079     *
    080     * @param keys  the keys to add ({@code null} not permitted).
    081     *
    082     * @return A boolean.
    083     */
    084    public boolean addAll(Collection<? extends KeyedValues3DItemKey> keys) {
    085        Args.nullNotPermitted(keys, "keys");
    086        return this.selectedItems.addAll(keys);
    087    }
    088
    089    /**
    090     * Removes an item from the selection returning {@code true} if the
    091     * item was removed and {@code false} if it did not exist within the
    092     * selection.
    093     *
    094     * @param itemKey  the item key ({@code null} not permitted).
    095     *
    096     * @return A boolean.
    097     */
    098    public boolean remove(KeyedValues3DItemKey itemKey) {
    099        return this.selectedItems.remove(itemKey);
    100    }
    101
    102    /**
    103     * Returns {@code true} if the specified item is in the selection,
    104     * and {@code false} otherwise.
    105     *
    106     * @param itemKey  the item key ({@code null} not permitted).
    107     *
    108     * @return A boolean.
    109     */
    110    @Override
    111    public boolean isSelected(KeyedValues3DItemKey itemKey) {
    112        return this.selectedItems.contains(itemKey);
    113    }
    114
    115    /**
    116     * Clears the item selection (that is, removes all items contained in the
    117     * selection).
    118     */
    119    public void clear() {
    120        this.selectedItems.clear();
    121    }
    122
    123    @Override
    124    public boolean equals(Object obj) {
    125        if (obj == this) {
    126            return true;
    127        }
    128        if (!(obj instanceof StandardKeyedValues3DItemSelection)) {
    129            return false;
    130        }
    131        StandardKeyedValues3DItemSelection that
    132                = (StandardKeyedValues3DItemSelection) obj;
    133        if (!this.selectedItems.equals(that.selectedItems)) {
    134            return false;
    135        }
    136        return true;
    137    }
    138}