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 41 package java.awt.font; 42 43 import java.awt.geom.AffineTransform; 44 import java.io.Serializable; 45 import java.io.ObjectStreamException; 46 47 /** 48 * The <code>TransformAttribute</code> class provides an immutable 49 * wrapper for a transform so that it is safe to use as an attribute. 50 */ 51 public final class TransformAttribute implements Serializable { 52 53 /** 54 * The <code>AffineTransform</code> for this 55 * <code>TransformAttribute</code>, or <code>null</code> 56 * if <code>AffineTransform</code> 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 a <code>AffineTransform</code> that is a copy of the wrapped 78 * transform of this <code>TransformAttribute</code>. 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</code> if the wrapped transform is 87 * an identity transform. 88 * @return <code>true</code> if the wrapped transform is 89 * an identity transform; <code>false</code> otherwise. 90 * @since 1.4 91 */ 92 public boolean isIdentity() { 93 return transform == null; 94 } 95 96 /** 97 * A <code>TransformAttribute</code> 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</code> if rhs is a <code>TransformAttribute</code> 135 * whose transform is equal to this <code>TransformAttribute</code>'s 136 * transform. 137 * @param rhs the object to compare to 138 * @return <code>true</code> if the argument is a <code>TransformAttribute</code> 139 * whose transform is equal to this <code>TransformAttribute</code>'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 } | 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 41 package java.awt.font; 42 43 import java.awt.geom.AffineTransform; 44 import java.io.Serializable; 45 import 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 */ 51 public 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 a {@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 } |