KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > krysalis > barcode > tools > Length


1 /*
2  * $Id: Length.java,v 1.5 2003/08/18 19:08:51 jmaerki Exp $
3  * ============================================================================
4  * The Krysalis Patchy Software License, Version 1.1_01
5  * Copyright (c) 2002-2003 Nicola Ken Barozzi. All rights reserved.
6  *
7  * This Licence is compatible with the BSD licence as described and
8  * approved by http://www.opensource.org/, and is based on the
9  * Apache Software Licence Version 1.1.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in
20  * the documentation and/or other materials provided with the
21  * distribution.
22  *
23  * 3. The end-user documentation included with the redistribution,
24  * if any, must include the following acknowledgment:
25  * "This product includes software developed for project
26  * Krysalis (http://www.krysalis.org/)."
27  * Alternately, this acknowledgment may appear in the software itself,
28  * if and wherever such third-party acknowledgments normally appear.
29  *
30  * 4. The names "Krysalis" and "Nicola Ken Barozzi" and
31  * "Krysalis Barcode" must not be used to endorse or promote products
32  * derived from this software without prior written permission. For
33  * written permission, please contact nicolaken@krysalis.org.
34  *
35  * 5. Products derived from this software may not be called "Krysalis",
36  * "Krysalis Barcode", nor may "Krysalis" appear in their name,
37  * without prior written permission of Nicola Ken Barozzi.
38  *
39  * 6. This software may contain voluntary contributions made by many
40  * individuals, who decided to donate the code to this project in
41  * respect of this licence, and was originally created by
42  * Jeremias Maerki <jeremias@maerki.org>.
43  *
44  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
45  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
46  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
47  * DISCLAIMED. IN NO EVENT SHALL THE KRYSALIS PROJECT OR
48  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
49  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
50  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
51  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
52  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
53  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
54  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55  * SUCH DAMAGE.
56  * ====================================================================
57  */

58 package org.krysalis.barcode.tools;
59
60 /**
61  * This class represents a length (value plus unit). It is used to parse
62  * expressions like "0.21mm".
63  *
64  * @author Jeremias Maerki
65  */

66 public class Length {
67     
68     private double value;
69     private String JavaDoc unit;
70     
71     /**
72      * Creates a Length instance.
73      * @param value the value
74      * @param unit the unit (ex. "cm")
75      */

76     public Length(double value, String JavaDoc unit) {
77         this.value = value;
78         this.unit = unit.toLowerCase();
79     }
80     
81     /**
82      * Creates a Length instance.
83      * @param text the String to parse
84      * @param defaultUnit the default unit to assume
85      */

86     public Length(String JavaDoc text, String JavaDoc defaultUnit) {
87         parse(text, defaultUnit);
88     }
89     
90     /**
91      * Creates a Length instance. The default unit assumed is "mm".
92      * @param text the String to parse
93      */

94     public Length(String JavaDoc text) {
95         this(text, null);
96     }
97     
98     /**
99      * Parses a value with unit.
100      * @param text the String to parse
101      * @param defaultUnit the default unit to assume
102      */

103     protected void parse(String JavaDoc text, String JavaDoc defaultUnit) {
104         final String JavaDoc s = text.trim();
105         if (s.length() == 0) {
106             throw new IllegalArgumentException JavaDoc("Length is empty");
107         }
108         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(s.length());
109         int mode = 0;
110         int i = 0;
111         while (i < s.length()) {
112             char c = s.charAt(i);
113             
114             if (mode == 0) {
115                 //Parse value
116
if (Character.isDigit(c) || c == '.' || c == ',') {
117                     if (c == ',') {
118                         c = '.';
119                     }
120                     sb.append(c);
121                     i++;
122                 } else {
123                     this.value = Double.parseDouble(sb.toString());
124                     sb.setLength(0);
125                     mode = 1;
126                 }
127             } else if (mode == 1) {
128                 //Parse optional whitespace
129
if (Character.isWhitespace(c)) {
130                     i++;
131                     continue;
132                 }
133                 mode = 2;
134             } else if (mode == 2) {
135                 //Parse unit
136
if (!Character.isWhitespace(c)) {
137                     sb.append(c);
138                     i++;
139                 } else {
140                     //Break on first white space after unit
141
break;
142                 }
143                 
144             }
145         }
146         if (mode == 0) {
147             this.value = Double.parseDouble(sb.toString());
148             mode = 1;
149         }
150         if (mode != 2) {
151             if ((mode > 0) && (defaultUnit != null)) {
152                 this.unit = defaultUnit.toLowerCase();
153                 return;
154             }
155             throw new IllegalArgumentException JavaDoc("Invalid length specified. "
156                     + "Expected '<value> <unit>' (ex. 1.7mm) but got: " + text);
157         }
158         this.unit = sb.toString().toLowerCase();
159     }
160
161     /**
162      * Returns the unit.
163      * @return String
164      */

165     public String JavaDoc getUnit() {
166         return this.unit;
167     }
168
169     /**
170      * Returns the value.
171      * @return double
172      */

173     public double getValue() {
174         return this.value;
175     }
176
177     /**
178      * Returns the value converted to internal units (mm).
179      * @return the value (in mm)
180      */

181     public double getValueAsMillimeter() {
182         if (this.unit.equals("mm")) {
183             return this.value;
184         } else if (this.unit.equals("cm")) {
185             return this.value * 10;
186         } else if (this.unit.equals("pt")) {
187             return UnitConv.pt2mm(this.value);
188         } else if (this.unit.equals("in")) {
189             return UnitConv.in2mm(this.value);
190         } else {
191             throw new IllegalStateException JavaDoc("Don't know how to convert "
192                     + this.unit + " to mm");
193         }
194     }
195     
196     /** {@inheritDoc} */
197     public String JavaDoc toString() {
198         return getValue() + getUnit();
199     }
200     
201 }
202
Popular Tags