jdk/src/share/classes/java/awt/font/TransformAttribute.java
author prr
Thu, 26 Sep 2013 15:06:39 -0700
changeset 20091 75850d176243
parent 5506 202f599c92aa
child 21278 ef8a3a2a72f2
permissions -rw-r--r--
7092764: java.awt.font.TransformAttribute.equals(null) throws NPE Reviewed-by: jgodinez, jchen
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1998, 2005, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * (C) Copyright Taligent, Inc. 1996 - 1997, All Rights Reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * (C) Copyright IBM Corp. 1996 - 1998, All Rights Reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * The original version of this source code and documentation is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * copyrighted and owned by Taligent, Inc., a wholly-owned subsidiary
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * of IBM. These materials are provided under terms of a License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * Agreement between Taligent and Sun. This technology is protected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * by multiple US and International patents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * This notice and attribution to Taligent may not be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Taligent is a registered trademark of Taligent, Inc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
package java.awt.font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.awt.geom.AffineTransform;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.io.Serializable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.io.ObjectStreamException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * The <code>TransformAttribute</code> class provides an immutable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * wrapper for a transform so that it is safe to use as an attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
public final class TransformAttribute implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * The <code>AffineTransform</code> for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * <code>TransformAttribute</code>, or <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * if <code>AffineTransform</code> is the identity transform.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private AffineTransform transform;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * Wraps the specified transform.  The transform is cloned and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * reference to the clone is kept.  The original transform is unchanged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * If null is passed as the argument, this constructor behaves as though
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * it were the identity transform.  (Note that it is preferable to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * {@link #IDENTITY} in this case.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * @param transform the specified {@link AffineTransform} to be wrapped,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * or null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public TransformAttribute(AffineTransform transform) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        if (transform != null && !transform.isIdentity()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            this.transform = new AffineTransform(transform);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Returns a copy of the wrapped transform.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @return a <code>AffineTransform</code> that is a copy of the wrapped
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * transform of this <code>TransformAttribute</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public AffineTransform getTransform() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        AffineTransform at = transform;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        return (at == null) ? new AffineTransform() : new AffineTransform(at);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Returns <code>true</code> if the wrapped transform is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * an identity transform.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @return <code>true</code> if the wrapped transform is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * an identity transform; <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public boolean isIdentity() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        return transform == null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * A <code>TransformAttribute</code> representing the identity transform.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public static final TransformAttribute IDENTITY = new TransformAttribute(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private void writeObject(java.io.ObjectOutputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
      throws java.lang.ClassNotFoundException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
             java.io.IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        // sigh -- 1.3 expects transform is never null, so we need to always write one out
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        if (this.transform == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            this.transform = new AffineTransform();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    private Object readResolve() throws ObjectStreamException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if (transform == null || transform.isIdentity()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            return IDENTITY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    // Added for serial backwards compatability (4348425)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    static final long serialVersionUID = 3356247357827709530L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        return transform == null ? 0 : transform.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * Returns <code>true</code> if rhs is a <code>TransformAttribute</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * whose transform is equal to this <code>TransformAttribute</code>'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * transform.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @param rhs the object to compare to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @return <code>true</code> if the argument is a <code>TransformAttribute</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * whose transform is equal to this <code>TransformAttribute</code>'s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * transform.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public boolean equals(Object rhs) {
20091
75850d176243 7092764: java.awt.font.TransformAttribute.equals(null) throws NPE
prr
parents: 5506
diff changeset
   144
        if (rhs != null) {
75850d176243 7092764: java.awt.font.TransformAttribute.equals(null) throws NPE
prr
parents: 5506
diff changeset
   145
            try {
75850d176243 7092764: java.awt.font.TransformAttribute.equals(null) throws NPE
prr
parents: 5506
diff changeset
   146
                TransformAttribute that = (TransformAttribute)rhs;
75850d176243 7092764: java.awt.font.TransformAttribute.equals(null) throws NPE
prr
parents: 5506
diff changeset
   147
                if (transform == null) {
75850d176243 7092764: java.awt.font.TransformAttribute.equals(null) throws NPE
prr
parents: 5506
diff changeset
   148
                    return that.transform == null;
75850d176243 7092764: java.awt.font.TransformAttribute.equals(null) throws NPE
prr
parents: 5506
diff changeset
   149
                }
75850d176243 7092764: java.awt.font.TransformAttribute.equals(null) throws NPE
prr
parents: 5506
diff changeset
   150
                return transform.equals(that.transform);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            }
20091
75850d176243 7092764: java.awt.font.TransformAttribute.equals(null) throws NPE
prr
parents: 5506
diff changeset
   152
            catch (ClassCastException e) {
75850d176243 7092764: java.awt.font.TransformAttribute.equals(null) throws NPE
prr
parents: 5506
diff changeset
   153
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
}