KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jcckit > plot > TicLabelMap


1 /*
2  * Copyright 2003-2004, Franz-Josef Elmer, All rights reserved
3  *
4  * This library is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation; either version 2.1 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details
13  * (http://www.gnu.org/copyleft/lesser.html).
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package jcckit.plot;
20
21 import java.util.StringTokenizer JavaDoc;
22
23 import jcckit.util.ConfigParameters;
24 import jcckit.util.TicLabelFormat;
25
26 /**
27  * Map of number intervals onto a text label. The map is defined by a
28  * map description string provided by configuration data.
29  * <p>
30  * The map description is a list
31  * of conditions separated by ';'. The conditions are tested from left to
32  * right until a condition is fulfilled for the tic value. If no condition
33  * is fullfilled a '?' will be returned.
34  * <p>
35  * A condition description has one of the following forms:
36  * <pre><tt><i>&lt;label&gt;</i></tt></pre>
37  * <pre><tt><i>&lt;number&gt;</i>=<i>&lt;label&gt;</i></tt></pre>
38  * <pre><tt><i>&lt;number1&gt;</i>:<i>&lt;number2&gt;</i>=<i>&lt;label&gt;</i></tt></pre>
39  * <p>
40  * The first type of condition is always fulfilled. It will return
41  * <tt><i>&lt;label&gt;</i></tt>. This is a kind of else condtion
42  * which is put at the end of the condition list.
43  * <p>
44  * The second form maps a particular number onto a label. In order to be
45  * equal with the sepcified number the tic value should not deviate more
46  * than 1 ppm (part per millions) from <tt><i>&lt;number&gt;</i>.
47  * <p>
48  * The third form maps an interval onto a label. The condition reads
49  * <p>
50  * <tt><i>&lt;number1&gt;</i></tt>&nbsp;&lt;=&nbsp;tic&nbsp;label&nbsp;&lt;&nbsp;<tt><i>&lt;number2&gt;</i></tt>
51  * <p>
52  * Examples:
53  * <pre><tt>
54  * 1=monday;2=tuesday;3=wednesday;4=thursday;5=friday;6=saturday;7=sunday
55  * 0.5:1.5=I; 1.5:2.5 = II; 2.5:3.5 = III; the rest
56  * </tt></pre>
57  *
58  * @author Franz-Josef Elmer
59  */

60 public class TicLabelMap implements TicLabelFormat {
61   public static final String JavaDoc MAP_KEY = "map";
62   
63   private static class MapItem {
64     private double _min = Double.MIN_VALUE;
65     private double _max = Double.MAX_VALUE;
66     private final String JavaDoc label;
67     public MapItem(String JavaDoc item) {
68       int index = item.indexOf('=');
69       if (index < 0) {
70         label = item;
71       } else {
72         label = item.substring(index + 1).trim();
73         item = item.substring(0, index).trim();
74         index = item.indexOf(':');
75         if (index < 0) {
76           _min = new Double JavaDoc(item).doubleValue();
77           _max = _min == 0 ? Double.MIN_VALUE : _min * 1.000001d;
78           _min = _min * 0.999999d;
79           if (_min > _max) {
80             double z = _min;
81             _min = _max;
82             _max = z;
83           }
84         } else {
85           _min = new Double JavaDoc(item.substring(0, index)).doubleValue();
86           _max = new Double JavaDoc(item.substring(index + 1)).doubleValue();
87         }
88       }
89     }
90     public boolean isInside(double value) {
91       return value >= _min && value < _max;
92     }
93   }
94   
95   private final MapItem[] _map;
96   
97   /**
98    * Creates an instance from the specified configuration parameters.
99    * <table border=1 cellpadding=5>
100    * <tr><th>Key &amp; Default Value</th><th>Type</th><th>Mandatory</th>
101    * <th>Description</th></tr>
102    * <tr><td><tt>map</tt></td>
103    * <td><tt>String</tt></td><td>yes</td>
104    * <td>Map description as explained above.</td></tr>
105    * </table>
106    */

107   public TicLabelMap(ConfigParameters config) {
108     StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(config.get(MAP_KEY), ";");
109     _map = new MapItem[tokenizer.countTokens()];
110     for (int i = 0; i < _map.length; i++)
111     {
112       String JavaDoc item = tokenizer.nextToken();
113       try {
114         _map[i] = new MapItem(item.trim());
115       } catch (NumberFormatException JavaDoc e) {
116         throw new NumberFormatException JavaDoc("Item '" + item + "' of "
117             + config.getFullKey(MAP_KEY) + " has an invalid number.");
118       }
119     }
120   }
121   
122   /**
123    * Maps the specified tic value onto a text label in accordance
124    * with the map description.
125    */

126   public String JavaDoc form(double ticValue) {
127     String JavaDoc result = "?";
128     for (int i = 0; i < _map.length; i++) {
129       if (_map[i].isInside(ticValue)) {
130         result = _map[i].label;
131         break;
132       }
133     }
134     return result;
135   }
136 }
137
Popular Tags