1/*
2 * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26/*
27 * (C) Copyright Taligent, Inc. 1996 - 1997, All Rights Reserved
28 * (C) Copyright IBM Corp. 1996 - 1998, All Rights Reserved
29 *
30 * The original version of this source code and documentation is
31 * copyrighted and owned by Taligent, Inc., a wholly-owned subsidiary
32 * of IBM. These materials are provided under terms of a License
33 * Agreement between Taligent and Sun. This technology is protected
34 * by multiple US and International patents.
35 *
36 * This notice and attribution to Taligent may not be removed.
37 * Taligent is a registered trademark of Taligent, Inc.
38 *
39 */
40
41package java.awt.font;
42
43import java.awt.geom.AffineTransform;
44import java.io.Serializable;
45import java.io.ObjectStreamException;
46
47/**
48 * The {@code TransformAttribute} class provides an immutable
49 * wrapper for a transform so that it is safe to use as an attribute.
50 */
51public final class TransformAttribute implements Serializable {
52
53    /**
54     * The {@code AffineTransform} for this
55     * {@code TransformAttribute}, or {@code null}
56     * if {@code AffineTransform} is the identity transform.
57     */
58    private AffineTransform transform;
59
60    /**
61     * Wraps the specified transform.  The transform is cloned and a
62     * reference to the clone is kept.  The original transform is unchanged.
63     * If null is passed as the argument, this constructor behaves as though
64     * it were the identity transform.  (Note that it is preferable to use
65     * {@link #IDENTITY} in this case.)
66     * @param transform the specified {@link AffineTransform} to be wrapped,
67     * or null.
68     */
69    public TransformAttribute(AffineTransform transform) {
70        if (transform != null && !transform.isIdentity()) {
71            this.transform = new AffineTransform(transform);
72        }
73    }
74
75    /**
76     * Returns a copy of the wrapped transform.
77     * @return an {@code AffineTransform} that is a copy of the wrapped
78     * transform of this {@code TransformAttribute}.
79     */
80    public AffineTransform getTransform() {
81        AffineTransform at = transform;
82        return (at == null) ? new AffineTransform() : new AffineTransform(at);
83    }
84
85    /**
86     * Returns {@code true} if the wrapped transform is
87     * an identity transform.
88     * @return {@code true} if the wrapped transform is
89     * an identity transform; {@code false} otherwise.
90     * @since 1.4
91     */
92    public boolean isIdentity() {
93        return transform == null;
94    }
95
96    /**
97     * A {@code TransformAttribute} representing the identity transform.
98     * @since 1.6
99     */
100    public static final TransformAttribute IDENTITY = new TransformAttribute(null);
101
102    private void writeObject(java.io.ObjectOutputStream s)
103      throws java.lang.ClassNotFoundException,
104             java.io.IOException
105    {
106        // sigh -- 1.3 expects transform is never null, so we need to always write one out
107        if (this.transform == null) {
108            this.transform = new AffineTransform();
109        }
110        s.defaultWriteObject();
111    }
112
113    /*
114     * @since 1.6
115     */
116    private Object readResolve() throws ObjectStreamException {
117        if (transform == null || transform.isIdentity()) {
118            return IDENTITY;
119        }
120        return this;
121    }
122
123    // Added for serial backwards compatibility (4348425)
124    static final long serialVersionUID = 3356247357827709530L;
125
126    /**
127     * @since 1.6
128     */
129    public int hashCode() {
130        return transform == null ? 0 : transform.hashCode();
131    }
132
133    /**
134     * Returns {@code true} if rhs is a {@code TransformAttribute}
135     * whose transform is equal to this {@code TransformAttribute}'s
136     * transform.
137     * @param rhs the object to compare to
138     * @return {@code true} if the argument is a {@code TransformAttribute}
139     * whose transform is equal to this {@code TransformAttribute}'s
140     * transform.
141     * @since 1.6
142     */
143    public boolean equals(Object rhs) {
144        if (rhs != null) {
145            try {
146                TransformAttribute that = (TransformAttribute)rhs;
147                if (transform == null) {
148                    return that.transform == null;
149                }
150                return transform.equals(that.transform);
151            }
152            catch (ClassCastException e) {
153            }
154        }
155        return false;
156    }
157}
158