001/*
002 * This file is part of McIDAS-V
003 *
004 * Copyright 2007-2023
005 * Space Science and Engineering Center (SSEC)
006 * University of Wisconsin - Madison
007 * 1225 W. Dayton Street, Madison, WI 53706, USA
008 * https://www.ssec.wisc.edu/mcidas
009 * 
010 * All Rights Reserved
011 * 
012 * McIDAS-V is built on Unidata's IDV and SSEC's VisAD libraries, and
013 * some McIDAS-V source code is based on IDV and VisAD source code.  
014 * 
015 * McIDAS-V is free software; you can redistribute it and/or modify
016 * it under the terms of the GNU Lesser Public License as published by
017 * the Free Software Foundation; either version 3 of the License, or
018 * (at your option) any later version.
019 * 
020 * McIDAS-V is distributed in the hope that it will be useful,
021 * but WITHOUT ANY WARRANTY; without even the implied warranty of
022 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
023 * GNU Lesser Public License for more details.
024 * 
025 * You should have received a copy of the GNU Lesser Public License
026 * along with this program.  If not, see http://www.gnu.org/licenses.
027 */
028
029package edu.wisc.ssec.mcidasv.probes;
030
031import static java.util.Objects.requireNonNull;
032
033import java.util.EventObject;
034
035/**
036 * This class captures a change to a probe and stores both the previous and
037 * current (as of the event's creation) changed values.
038 */
039@SuppressWarnings("serial")
040public class ProbeEvent<T> extends EventObject {
041
042    /**
043     * Previous value of the probe.
044     */
045    private final T oldValue;
046
047    /**
048     * Current value of the probe.
049     */
050    private final T newValue;
051
052    /**
053     * Generated when a {@link ReadoutProbe} changes either its position, 
054     * color, or visibility. Currently stores either position, color, or 
055     * visibility both before and after the change.
056     * 
057     * @param source Probe that generated this event.
058     * @param oldValue Old value of the probe.
059     * @param newValue New value of the probe.
060     * 
061     * @throws NullPointerException if any parameters are {@code null}.
062     */
063    public ProbeEvent(final ReadoutProbe source, final T oldValue, final T newValue) {
064        super(source);
065        requireNonNull(source, "Events cannot originate from a null source object");
066        requireNonNull(oldValue, "Old value cannot be null");
067        requireNonNull(newValue, "New value cannot be null");
068        this.oldValue = oldValue;
069        this.newValue = newValue;
070    }
071
072    /**
073     * Returns the probe that generated this event.
074     *
075     * @return Probe from which this event originated.
076     */
077    public ReadoutProbe getProbe() {
078        return (ReadoutProbe)getSource();
079    }
080
081    /**
082     * Returns the value of the probe before this event was generated.
083     * 
084     * @return Previous probe value.
085     */
086    public T getOldValue() {
087        return oldValue;
088    }
089
090    /**
091     * Returns the current (as of this event) value of the probe.
092     * 
093     * @return Current probe value.
094     */
095    public T getNewValue() {
096        return newValue;
097    }
098
099    /**
100     * Returns a brief summary of this event. Please note that this format is
101     * subject to change.
102     * 
103     * @return String that looks like {@code [ProbeEvent@HASHCODE: source=..., 
104     * oldValue=..., newValue=...]}.
105     */
106    @Override public String toString() {
107        return String.format("[ProbeEvent@%x: source=%s, oldValue=%s, newValue=%s]", hashCode(), source, oldValue, newValue);
108    }
109}