155 if (x < 0) { 156 throw new IllegalArgumentException("x < 0"); 157 } 158 if (y < 0) { 159 throw new IllegalArgumentException("y < 0"); 160 } 161 if (units < 1) { 162 throw new IllegalArgumentException("units < 1"); 163 } 164 this.x = x * units; 165 this.y = y * units; 166 } 167 168 /** 169 * Convert a value from micrometers to some other units. The result is 170 * returned as a floating-point number. 171 * 172 * @param x 173 * Value (micrometers) to convert. 174 * @param units 175 * Unit conversion factor, e.g. {@link #INCH <CODE>INCH</CODE>} or 176 * {@link #MM <CODE>MM</CODE>}. 177 * 178 * @return The value of <CODE>x</CODE> converted to the desired units. 179 * 180 * @exception IllegalArgumentException 181 * (unchecked exception) Thrown if <CODE>units</CODE> < 1. 182 */ 183 private static float convertFromMicrometers(int x, int units) { 184 if (units < 1) { 185 throw new IllegalArgumentException("units is < 1"); 186 } 187 return ((float)x) / ((float)units); 188 } 189 190 /** 191 * Get this two-dimensional size attribute's dimensions in the given units 192 * as floating-point values. 193 * 194 * @param units 195 * Unit conversion factor, e.g. {@link #INCH INCH} or {@link #MM MM}. 196 * 197 * @return A two-element array with the X dimension at index 0 and the Y 198 * dimension at index 1. 199 * 200 * @exception IllegalArgumentException 201 * (unchecked exception) Thrown if {@code units < 1}. 221 } 222 223 /** 224 * Returns this two-dimensional size attribute's Y dimension in the given 225 * units as a floating-point value. 226 * 227 * @param units 228 * Unit conversion factor, e.g. {@link #INCH INCH} or {@link #MM MM}. 229 * 230 * @return Y dimension. 231 * 232 * @exception IllegalArgumentException 233 * (unchecked exception) Thrown if {@code units < 1}. 234 */ 235 public float getY(int units) { 236 return convertFromMicrometers(y, units); 237 } 238 239 /** 240 * Returns a string version of this two-dimensional size attribute in the 241 * given units. The string takes the form <CODE>"<I>X</I>x<I>Y</I> 242 * <I>U</I>"</CODE>, where <I>X</I> is the X dimension, <I>Y</I> is the Y 243 * dimension, and <I>U</I> is the units name. The values are displayed in 244 * floating point. 245 * 246 * @param units 247 * Unit conversion factor, e.g. {@link #INCH INCH} or {@link #MM MM}. 248 * 249 * @param unitsName 250 * Units name string, e.g. {@code in} or {@code mm}. If 251 * null, no units name is appended to the result. 252 * 253 * @return String version of this two-dimensional size attribute. 254 * 255 * @exception IllegalArgumentException 256 * (unchecked exception) Thrown if {@code units < 1}. 257 */ 258 public String toString(int units, String unitsName) { 259 StringBuilder result = new StringBuilder(); 260 result.append(getX (units)); 261 result.append('x'); 262 result.append(getY (units)); 263 if (unitsName != null) { 264 result.append(' '); 265 result.append(unitsName); 266 } 267 return result.toString(); 268 } 269 270 /** 271 * Returns whether this two-dimensional size attribute is equivalent to the 272 * passed in object. To be equivalent, all of the following conditions must 273 * be true: 274 * <OL TYPE=1> 275 * <LI> 276 * <CODE>object</CODE> is not null. 277 * <LI> 278 * <CODE>object</CODE> is an instance of class Size2DSyntax. 279 * <LI> 280 * This attribute's X dimension is equal to <CODE>object</CODE>'s X 281 * dimension. 282 * <LI> 283 * This attribute's Y dimension is equal to <CODE>object</CODE>'s Y 284 * dimension. 285 * </OL> 286 * 287 * @param object Object to compare to. 288 * 289 * @return True if <CODE>object</CODE> is equivalent to this 290 * two-dimensional size attribute, false otherwise. 291 */ 292 public boolean equals(Object object) { 293 return(object != null && 294 object instanceof Size2DSyntax && 295 this.x == ((Size2DSyntax) object).x && 296 this.y == ((Size2DSyntax) object).y); 297 } 298 299 /** 300 * Returns a hash code value for this two-dimensional size attribute. 301 */ 302 public int hashCode() { 303 return (((x & 0x0000FFFF) ) | 304 ((y & 0x0000FFFF) << 16)); 305 } 306 307 /** 308 * Returns a string version of this two-dimensional size attribute. The 309 * string takes the form <CODE>"<I>X</I>x<I>Y</I> um"</CODE>, where 310 * <I>X</I> is the X dimension and <I>Y</I> is the Y dimension. 311 * The values are reported in the internal units of micrometers. 312 */ 313 public String toString() { 314 StringBuilder result = new StringBuilder(); 315 result.append(x); 316 result.append('x'); 317 result.append(y); 318 result.append(" um"); 319 return result.toString(); 320 } 321 322 /** 323 * Returns this two-dimensional size attribute's X dimension in units of 324 * micrometers (µm). (For use in a subclass.) 325 * 326 * @return X dimension (µm). 327 */ 328 protected int getXMicrometers(){ 329 return x; | 155 if (x < 0) { 156 throw new IllegalArgumentException("x < 0"); 157 } 158 if (y < 0) { 159 throw new IllegalArgumentException("y < 0"); 160 } 161 if (units < 1) { 162 throw new IllegalArgumentException("units < 1"); 163 } 164 this.x = x * units; 165 this.y = y * units; 166 } 167 168 /** 169 * Convert a value from micrometers to some other units. The result is 170 * returned as a floating-point number. 171 * 172 * @param x 173 * Value (micrometers) to convert. 174 * @param units 175 * Unit conversion factor, e.g. {@link #INCH INCH} or 176 * {@link #MM MM}. 177 * 178 * @return The value of {@code x} converted to the desired units. 179 * 180 * @exception IllegalArgumentException 181 * (unchecked exception) Thrown if {@code units} < 1. 182 */ 183 private static float convertFromMicrometers(int x, int units) { 184 if (units < 1) { 185 throw new IllegalArgumentException("units is < 1"); 186 } 187 return ((float)x) / ((float)units); 188 } 189 190 /** 191 * Get this two-dimensional size attribute's dimensions in the given units 192 * as floating-point values. 193 * 194 * @param units 195 * Unit conversion factor, e.g. {@link #INCH INCH} or {@link #MM MM}. 196 * 197 * @return A two-element array with the X dimension at index 0 and the Y 198 * dimension at index 1. 199 * 200 * @exception IllegalArgumentException 201 * (unchecked exception) Thrown if {@code units < 1}. 221 } 222 223 /** 224 * Returns this two-dimensional size attribute's Y dimension in the given 225 * units as a floating-point value. 226 * 227 * @param units 228 * Unit conversion factor, e.g. {@link #INCH INCH} or {@link #MM MM}. 229 * 230 * @return Y dimension. 231 * 232 * @exception IllegalArgumentException 233 * (unchecked exception) Thrown if {@code units < 1}. 234 */ 235 public float getY(int units) { 236 return convertFromMicrometers(y, units); 237 } 238 239 /** 240 * Returns a string version of this two-dimensional size attribute in the 241 * given units. The string takes the form <code>"<I>X</I>x<I>Y</I> 242 * <I>U</I>"</code>, where <I>X</I> is the X dimension, <I>Y</I> is the Y 243 * dimension, and <I>U</I> is the units name. The values are displayed in 244 * floating point. 245 * 246 * @param units 247 * Unit conversion factor, e.g. {@link #INCH INCH} or {@link #MM MM}. 248 * 249 * @param unitsName 250 * Units name string, e.g. {@code in} or {@code mm}. If 251 * null, no units name is appended to the result. 252 * 253 * @return String version of this two-dimensional size attribute. 254 * 255 * @exception IllegalArgumentException 256 * (unchecked exception) Thrown if {@code units < 1}. 257 */ 258 public String toString(int units, String unitsName) { 259 StringBuilder result = new StringBuilder(); 260 result.append(getX (units)); 261 result.append('x'); 262 result.append(getY (units)); 263 if (unitsName != null) { 264 result.append(' '); 265 result.append(unitsName); 266 } 267 return result.toString(); 268 } 269 270 /** 271 * Returns whether this two-dimensional size attribute is equivalent to the 272 * passed in object. To be equivalent, all of the following conditions must 273 * be true: 274 * <OL TYPE=1> 275 * <LI> 276 * {@code object} is not null. 277 * <LI> 278 * {@code object} is an instance of class Size2DSyntax. 279 * <LI> 280 * This attribute's X dimension is equal to {@code object}'s X 281 * dimension. 282 * <LI> 283 * This attribute's Y dimension is equal to {@code object}'s Y 284 * dimension. 285 * </OL> 286 * 287 * @param object Object to compare to. 288 * 289 * @return True if {@code object} is equivalent to this 290 * two-dimensional size attribute, false otherwise. 291 */ 292 public boolean equals(Object object) { 293 return(object != null && 294 object instanceof Size2DSyntax && 295 this.x == ((Size2DSyntax) object).x && 296 this.y == ((Size2DSyntax) object).y); 297 } 298 299 /** 300 * Returns a hash code value for this two-dimensional size attribute. 301 */ 302 public int hashCode() { 303 return (((x & 0x0000FFFF) ) | 304 ((y & 0x0000FFFF) << 16)); 305 } 306 307 /** 308 * Returns a string version of this two-dimensional size attribute. The 309 * string takes the form <code>"<I>X</I>x<I>Y</I> um"</code>, where 310 * <I>X</I> is the X dimension and <I>Y</I> is the Y dimension. 311 * The values are reported in the internal units of micrometers. 312 */ 313 public String toString() { 314 StringBuilder result = new StringBuilder(); 315 result.append(x); 316 result.append('x'); 317 result.append(y); 318 result.append(" um"); 319 return result.toString(); 320 } 321 322 /** 323 * Returns this two-dimensional size attribute's X dimension in units of 324 * micrometers (µm). (For use in a subclass.) 325 * 326 * @return X dimension (µm). 327 */ 328 protected int getXMicrometers(){ 329 return x; |