001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package javax.print.attribute;
019:
020: import java.io.Serializable;
021:
022: public abstract class Size2DSyntax implements Cloneable, Serializable {
023: private static final long serialVersionUID = 5584439964938660530L;
024:
025: public static final int INCH = 25400;
026:
027: public static final int MM = 1000;
028:
029: private final int x;
030:
031: private final int y;
032:
033: protected Size2DSyntax(int x, int y, int units) {
034: if ((x < 0) || (y < 0) || (units < 1)) {
035: throw new IllegalArgumentException("Valid values are:"
036: + "x>=0, y>=0, units>=1");
037: }
038: this .x = x * units;
039: this .y = y * units;
040: }
041:
042: protected Size2DSyntax(float x, float y, int units) {
043: if ((x < 0.0f) || (y < 0.0f) || (units < 1)) {
044: throw new IllegalArgumentException("Valid values are:"
045: + "x>=0.0, y>=0.0, units>=1");
046: }
047: this .x = Math.round(x * units);
048: this .y = Math.round(y * units);
049: }
050:
051: @Override
052: public boolean equals(Object object) {
053: if ((object instanceof Size2DSyntax)
054: && (x == ((Size2DSyntax) object).x)
055: && (y == ((Size2DSyntax) object).y)) {
056: return true;
057: }
058: return false;
059: }
060:
061: public float[] getSize(int units) {
062: return new float[] { getX(units), getY(units) };
063: }
064:
065: public float getX(int units) {
066: if (units < 1) {
067: throw new IllegalArgumentException("units is less than 1");
068: }
069: return ((float) x) / units;
070: }
071:
072: public float getY(int units) {
073: if (units < 1) {
074: throw new IllegalArgumentException("units is less than 1");
075: }
076: return ((float) y) / units;
077: }
078:
079: protected int getXMicrometers() {
080: return x;
081: }
082:
083: protected int getYMicrometers() {
084: return y;
085: }
086:
087: @Override
088: public int hashCode() {
089: return (y | (x << 16));
090: }
091:
092: @Override
093: public String toString() {
094: return (x + "x" + y + " um");
095: }
096:
097: public String toString(int units, String unitsName) {
098: if (unitsName == null) {
099: unitsName = "";
100: }
101: return (getX(units) + "x" + getX(units) + " " + unitsName);
102: }
103: }
|