001 /*
002 * Copyright 1998-2005 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 /*
027 * (C) Copyright Taligent, Inc. 1996 - 1997, All Rights Reserved
028 * (C) Copyright IBM Corp. 1996 - 1998, All Rights Reserved
029 *
030 * The original version of this source code and documentation is
031 * copyrighted and owned by Taligent, Inc., a wholly-owned subsidiary
032 * of IBM. These materials are provided under terms of a License
033 * Agreement between Taligent and Sun. This technology is protected
034 * by multiple US and International patents.
035 *
036 * This notice and attribution to Taligent may not be removed.
037 * Taligent is a registered trademark of Taligent, Inc.
038 *
039 */
040
041 package java.awt.font;
042
043 import java.awt.geom.AffineTransform;
044 import java.io.Serializable;
045 import java.io.ObjectStreamException;
046
047 /**
048 * The <code>TransformAttribute</code> class provides an immutable
049 * wrapper for a transform so that it is safe to use as an attribute.
050 */
051 public final class TransformAttribute implements Serializable {
052
053 /**
054 * The <code>AffineTransform</code> for this
055 * <code>TransformAttribute</code>, or <code>null</code>
056 * if <code>AffineTransform</code> is the identity transform.
057 */
058 private AffineTransform transform;
059
060 /**
061 * Wraps the specified transform. The transform is cloned and a
062 * reference to the clone is kept. The original transform is unchanged.
063 * If null is passed as the argument, this constructor behaves as though
064 * it were the identity transform. (Note that it is preferable to use
065 * {@link #IDENTITY} in this case.)
066 * @param transform the specified {@link AffineTransform} to be wrapped,
067 * or null.
068 */
069 public TransformAttribute(AffineTransform transform) {
070 if (transform != null && !transform.isIdentity()) {
071 this .transform = new AffineTransform(transform);
072 }
073 }
074
075 /**
076 * Returns a copy of the wrapped transform.
077 * @return a <code>AffineTransform</code> that is a copy of the wrapped
078 * transform of this <code>TransformAttribute</code>.
079 */
080 public AffineTransform getTransform() {
081 AffineTransform at = transform;
082 return (at == null) ? new AffineTransform()
083 : new AffineTransform(at);
084 }
085
086 /**
087 * Returns <code>true</code> if the wrapped transform is
088 * an identity transform.
089 * @return <code>true</code> if the wrapped transform is
090 * an identity transform; <code>false</code> otherwise.
091 * @since 1.4
092 */
093 public boolean isIdentity() {
094 return transform == null;
095 }
096
097 /**
098 * A <code>TransformAttribute</code> representing the identity transform.
099 * @since 1.6
100 */
101 public static final TransformAttribute IDENTITY = new TransformAttribute(
102 null);
103
104 private void writeObject(java.io.ObjectOutputStream s)
105 throws java.lang.ClassNotFoundException,
106 java.io.IOException {
107 // sigh -- 1.3 expects transform is never null, so we need to always write one out
108 if (this .transform == null) {
109 this .transform = new AffineTransform();
110 }
111 s.defaultWriteObject();
112 }
113
114 /*
115 * @since 1.6
116 */
117 private Object readResolve() throws ObjectStreamException {
118 if (transform == null || transform.isIdentity()) {
119 return IDENTITY;
120 }
121 return this ;
122 }
123
124 // Added for serial backwards compatability (4348425)
125 static final long serialVersionUID = 3356247357827709530L;
126
127 /**
128 * @since 1.6
129 */
130 public int hashCode() {
131 return transform == null ? 0 : transform.hashCode();
132 }
133
134 /**
135 * Returns <code>true</code> if rhs is a <code>TransformAttribute</code>
136 * whose transform is equal to this <code>TransformAttribute</code>'s
137 * transform.
138 * @param rhs the object to compare to
139 * @return <code>true</code> if the argument is a <code>TransformAttribute</code>
140 * whose transform is equal to this <code>TransformAttribute</code>'s
141 * transform.
142 * @since 1.6
143 */
144 public boolean equals(Object rhs) {
145 try {
146 TransformAttribute that = (TransformAttribute) rhs;
147 if (transform == null) {
148 return that.transform == null;
149 }
150 return transform.equals(that.transform);
151 } catch (ClassCastException e) {
152 }
153 return false;
154 }
155 }
|