1 19 package jcckit.plot; 20 21 import java.util.StringTokenizer ; 22 23 import jcckit.util.ConfigParameters; 24 import jcckit.util.TicLabelFormat; 25 26 60 public class TicLabelMap implements TicLabelFormat { 61 public static final String 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 label; 67 public MapItem(String 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 (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 (item.substring(0, index)).doubleValue(); 86 _max = new Double (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 107 public TicLabelMap(ConfigParameters config) { 108 StringTokenizer tokenizer = new StringTokenizer (config.get(MAP_KEY), ";"); 109 _map = new MapItem[tokenizer.countTokens()]; 110 for (int i = 0; i < _map.length; i++) 111 { 112 String item = tokenizer.nextToken(); 113 try { 114 _map[i] = new MapItem(item.trim()); 115 } catch (NumberFormatException e) { 116 throw new NumberFormatException ("Item '" + item + "' of " 117 + config.getFullKey(MAP_KEY) + " has an invalid number."); 118 } 119 } 120 } 121 122 126 public String form(double ticValue) { 127 String 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 |