src/java.desktop/share/classes/java/beans/PropertyChangeEvent.java
author darcy
Tue, 24 Sep 2019 18:25:54 -0700
changeset 58309 c6f8b2c3dc66
parent 47216 71c04702a3d5
permissions -rw-r--r--
8231334: Suppress warnings on non-serializable instance fields in client libs serializable classes Reviewed-by: prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
11089
e23e7a5faad5 7087429: Constructor of java.beans.PropertyChangeEvent should declare thrown NPE for null source
malenkov
parents: 5506
diff changeset
     2
 * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4960
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4960
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4960
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4960
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4960
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.beans;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
11089
e23e7a5faad5 7087429: Constructor of java.beans.PropertyChangeEvent should declare thrown NPE for null source
malenkov
parents: 5506
diff changeset
    28
import java.util.EventObject;
e23e7a5faad5 7087429: Constructor of java.beans.PropertyChangeEvent should declare thrown NPE for null source
malenkov
parents: 5506
diff changeset
    29
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * A "PropertyChange" event gets delivered whenever a bean changes a "bound"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * or "constrained" property.  A PropertyChangeEvent object is sent as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * argument to the PropertyChangeListener and VetoableChangeListener methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * Normally PropertyChangeEvents are accompanied by the name and the old
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * and new value of the changed property.  If the new value is a primitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * type (such as int or boolean) it must be wrapped as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * corresponding java.lang.* Object type (such as Integer or Boolean).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * Null values may be provided for the old and the new values if their
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * true values are not known.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * An event source may send a null object as the name to indicate that an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * arbitrary set of if its properties have changed.  In this case the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * old and new values should also be null.
25130
adfaa02ea516 8044855: Add missing @since tag under java.beans.*
henryjen
parents: 11089
diff changeset
    46
 *
adfaa02ea516 8044855: Add missing @since tag under java.beans.*
henryjen
parents: 11089
diff changeset
    47
 * @since 1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
11089
e23e7a5faad5 7087429: Constructor of java.beans.PropertyChangeEvent should declare thrown NPE for null source
malenkov
parents: 5506
diff changeset
    49
public class PropertyChangeEvent extends EventObject {
2482
77898184e343 4769844: classes in java.beans that are serializable but don't define serialVersionUID
malenkov
parents: 2
diff changeset
    50
    private static final long serialVersionUID = 7042693688939648123L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /**
11089
e23e7a5faad5 7087429: Constructor of java.beans.PropertyChangeEvent should declare thrown NPE for null source
malenkov
parents: 5506
diff changeset
    53
     * Constructs a new {@code PropertyChangeEvent}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     *
11089
e23e7a5faad5 7087429: Constructor of java.beans.PropertyChangeEvent should declare thrown NPE for null source
malenkov
parents: 5506
diff changeset
    55
     * @param source        the bean that fired the event
e23e7a5faad5 7087429: Constructor of java.beans.PropertyChangeEvent should declare thrown NPE for null source
malenkov
parents: 5506
diff changeset
    56
     * @param propertyName  the programmatic name of the property that was changed
e23e7a5faad5 7087429: Constructor of java.beans.PropertyChangeEvent should declare thrown NPE for null source
malenkov
parents: 5506
diff changeset
    57
     * @param oldValue      the old value of the property
e23e7a5faad5 7087429: Constructor of java.beans.PropertyChangeEvent should declare thrown NPE for null source
malenkov
parents: 5506
diff changeset
    58
     * @param newValue      the new value of the property
e23e7a5faad5 7087429: Constructor of java.beans.PropertyChangeEvent should declare thrown NPE for null source
malenkov
parents: 5506
diff changeset
    59
     *
e23e7a5faad5 7087429: Constructor of java.beans.PropertyChangeEvent should declare thrown NPE for null source
malenkov
parents: 5506
diff changeset
    60
     * @throws IllegalArgumentException if {@code source} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public PropertyChangeEvent(Object source, String propertyName,
11089
e23e7a5faad5 7087429: Constructor of java.beans.PropertyChangeEvent should declare thrown NPE for null source
malenkov
parents: 5506
diff changeset
    63
                               Object oldValue, Object newValue) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        super(source);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        this.propertyName = propertyName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        this.newValue = newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        this.oldValue = oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * Gets the programmatic name of the property that was changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * @return  The programmatic name of the property that was changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     *          May be null if multiple properties have changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public String getPropertyName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        return propertyName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * Gets the new value for the property, expressed as an Object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @return  The new value for the property, expressed as an Object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *          May be null if multiple properties have changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public Object getNewValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        return newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Gets the old value for the property, expressed as an Object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @return  The old value for the property, expressed as an Object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *          May be null if multiple properties have changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public Object getOldValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        return oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Sets the propagationId object for the event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @param propagationId  The propagationId object for the event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public void setPropagationId(Object propagationId) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        this.propagationId = propagationId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * The "propagationId" field is reserved for future use.  In Beans 1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * the sole requirement is that if a listener catches a PropertyChangeEvent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * and then fires a PropertyChangeEvent of its own, then it should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * make sure that it propagates the propagationId field from its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * incoming event to its outgoing event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @return the propagationId object associated with a bound/constrained
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *          property update.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public Object getPropagationId() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        return propagationId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * name of the property that changed.  May be null, if not known.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    private String propertyName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * New value for property.  May be null if not known.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
58309
c6f8b2c3dc66 8231334: Suppress warnings on non-serializable instance fields in client libs serializable classes
darcy
parents: 47216
diff changeset
   133
    @SuppressWarnings("serial") // Not statically typed as Serializable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private Object newValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Previous value for property.  May be null if not known.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
58309
c6f8b2c3dc66 8231334: Suppress warnings on non-serializable instance fields in client libs serializable classes
darcy
parents: 47216
diff changeset
   140
    @SuppressWarnings("serial") // Not statically typed as Serializable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    private Object oldValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Propagation ID.  May be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @see #getPropagationId
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     */
58309
c6f8b2c3dc66 8231334: Suppress warnings on non-serializable instance fields in client libs serializable classes
darcy
parents: 47216
diff changeset
   148
    @SuppressWarnings("serial") // Not statically typed as Serializable
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    private Object propagationId;
4960
99ac74ca2f2f 4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents: 2482
diff changeset
   150
99ac74ca2f2f 4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents: 2482
diff changeset
   151
    /**
99ac74ca2f2f 4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents: 2482
diff changeset
   152
     * Returns a string representation of the object.
99ac74ca2f2f 4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents: 2482
diff changeset
   153
     *
99ac74ca2f2f 4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents: 2482
diff changeset
   154
     * @return a string representation of the object
99ac74ca2f2f 4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents: 2482
diff changeset
   155
     *
99ac74ca2f2f 4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents: 2482
diff changeset
   156
     * @since 1.7
99ac74ca2f2f 4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents: 2482
diff changeset
   157
     */
99ac74ca2f2f 4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents: 2482
diff changeset
   158
    public String toString() {
99ac74ca2f2f 4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents: 2482
diff changeset
   159
        StringBuilder sb = new StringBuilder(getClass().getName());
99ac74ca2f2f 4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents: 2482
diff changeset
   160
        sb.append("[propertyName=").append(getPropertyName());
99ac74ca2f2f 4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents: 2482
diff changeset
   161
        appendTo(sb);
99ac74ca2f2f 4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents: 2482
diff changeset
   162
        sb.append("; oldValue=").append(getOldValue());
99ac74ca2f2f 4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents: 2482
diff changeset
   163
        sb.append("; newValue=").append(getNewValue());
99ac74ca2f2f 4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents: 2482
diff changeset
   164
        sb.append("; propagationId=").append(getPropagationId());
99ac74ca2f2f 4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents: 2482
diff changeset
   165
        sb.append("; source=").append(getSource());
99ac74ca2f2f 4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents: 2482
diff changeset
   166
        return sb.append("]").toString();
99ac74ca2f2f 4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents: 2482
diff changeset
   167
    }
99ac74ca2f2f 4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents: 2482
diff changeset
   168
99ac74ca2f2f 4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents: 2482
diff changeset
   169
    void appendTo(StringBuilder sb) {
99ac74ca2f2f 4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents: 2482
diff changeset
   170
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
}