1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.cosylab.gui.adapters;
21
22 import java.util.HashMap;
23 import java.util.Map;
24
25 import com.cosylab.gui.displayers.CommonDisplayer;
26 import com.cosylab.gui.displayers.DoubleConsumer;
27
28
29
30
31
32
33
34
35
36
37 public class LogarithmicConverter extends SimpleConverterSupport implements
38 DoubleConsumer {
39
40 private static final long serialVersionUID = 7247639283189848843L;
41
42 public static final String SHORT_NAME = "Logarithmic";
43
44 private double base = Math.E, logBase = 1;
45
46
47
48
49
50
51 public LogarithmicConverter(){
52 setBase(Math.E);
53 }
54
55
56
57
58
59
60 public LogarithmicConverter(double base){
61 setBase(base);
62 }
63
64
65
66
67
68 @Override
69 protected double inverseTransform(double value) {
70 return Math.pow(getBase(), value);
71 }
72
73
74
75
76
77 @Override
78 protected double transform(double value) {
79 if (value <= 0) return 0;
80 return Math.log(value)/getLogBase();
81 }
82
83
84
85
86
87
88 public double getBase() {
89 return base;
90 }
91
92
93
94
95
96
97 public void setBase(double base) {
98 if (base <= 0) return;
99 this.base = base;
100 setLogBase(Math.log(base));
101 }
102
103 protected double getLogBase() {
104 return logBase;
105 }
106
107 protected void setLogBase(double logBase) {
108 this.logBase = logBase;
109 }
110
111
112
113
114
115 public String getName(){
116 return SHORT_NAME;
117 }
118
119
120
121
122
123 public String toString(){
124 return SHORT_NAME + ": " + getBase();
125 }
126
127
128
129
130
131 public void setCharacteristics(Map characteristics)
132 {
133
134
135 cacheLastCharacteristics(characteristics);
136
137 HashMap map = new HashMap(characteristics);
138
139 double min;
140 double max;
141
142
143 if (map.containsKey(CommonDisplayer.C_GRAPH_MIN)
144 && map.containsKey(CommonDisplayer.C_GRAPH_MAX)) {
145 min = ((Number)map.get(CommonDisplayer.C_GRAPH_MIN)).doubleValue();
146 max = ((Number)map.get(CommonDisplayer.C_GRAPH_MAX)).doubleValue();
147
148 map.put(CommonDisplayer.C_GRAPH_MIN,
149 new Double(transform(min)));
150 map.put(CommonDisplayer.C_GRAPH_MAX,
151 new Double(transform(max)));
152 }
153
154
155 if (map.containsKey(CommonDisplayer.C_MINIMUM)
156 && map.containsKey(CommonDisplayer.C_MAXIMUM)) {
157 min = ((Number)map.get(CommonDisplayer.C_MINIMUM)).doubleValue();
158 max = ((Number)map.get(CommonDisplayer.C_MAXIMUM)).doubleValue();
159
160 map.put(CommonDisplayer.C_MINIMUM,
161 new Double(transform(min)));
162 map.put(CommonDisplayer.C_MAXIMUM,
163 new Double(transform(max)));
164 }
165
166
167 map.put(CommonDisplayer.C_UNITS,
168 "("+ map.get(CommonDisplayer.C_UNITS) +")");
169
170 super.setCharacteristics(map);
171 }
172
173
174
175
176
177 @Override
178 public boolean equals(Object obj) {
179 if (!(obj instanceof LogarithmicConverter)) return false;
180 LogarithmicConverter c = (LogarithmicConverter)obj;
181 return c.getBase() == getBase();
182 }
183 }