View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of CosyBeans.
5    *
6    * CosyBeans is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * CosyBeans is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with CosyBeans.  If not, see <http://www.gnu.org/licenses/>.
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   * <code>LogarithmicConverter</code> logarithms the supplied value using the
30   * logarithm of the specified base. If the incomming value is x, the transformed
31   * ont is log_base(x).
32   * 
33   * @author <a href="mailto:jaka.bobnar@cosylab.com">Jaka Bobnar</a>
34   * @version $Id: LogarithmicConverter.java,v 1.13 2008-04-22 12:31:02 jbobnar Exp $
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  	 * Constructs a new LogarithmicConverter with base e = 2.7182...
48  	 * @see Math#E
49  	 *
50  	 */
51  	public LogarithmicConverter(){
52  		setBase(Math.E);
53  	}
54  	
55  	/**
56  	 * Constructs a new LogarithmicConverter with the given base factor.
57  	 * 
58  	 * @param base 
59  	 */
60  	public LogarithmicConverter(double base){
61  		setBase(base);
62  	}
63  	
64  	/*
65  	 * (non-Javadoc)
66  	 * @see com.cosylab.gui.adapters.SimpleConverterSupport#inverseTransform(double)
67  	 */
68  	@Override
69  	protected double inverseTransform(double value) {
70  		return Math.pow(getBase(), value);
71  	}
72  	
73  	/*
74  	 * (non-Javadoc)
75  	 * @see com.cosylab.gui.adapters.SimpleConverterSupport#transform(double)
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  	 * Returns the base of the logarithm.
85  	 * 
86  	 * @return
87  	 */
88  	public double getBase() {
89  		return base;
90  	}
91  
92  	/**
93  	 * Sets the base of the logarithm.
94  	 * 
95  	 * @param base
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 	 * (non-Javadoc)
113 	 * @see com.cosylab.gui.adapters.DataConsumerDispatcher#getName()
114 	 */
115 	public String getName(){
116 		return SHORT_NAME;
117 	}
118 	
119 	/*
120 	 * (non-Javadoc)
121 	 * @see java.lang.Object#toString()
122 	 */
123 	public String toString(){
124 		return SHORT_NAME + ": " + getBase();
125 	}
126 	
127 	/*
128 	 * (non-Javadoc)
129 	 * @see com.cosylab.gui.adapters.AbstractConverter#setCharacteristics(java.util.Map)
130 	 */
131 	public void setCharacteristics(Map characteristics)
132 	{
133 		// We store unmodified characteristic for possible later reinitialization of 
134 		// consumers, eg. if multiplication factor changes
135 		cacheLastCharacteristics(characteristics);
136 
137 		HashMap map = new HashMap(characteristics);
138 
139 		double min;
140 		double max;
141 		
142 		// Modifying the graph_min and graph_max characteristics.
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 		// Modifying the minimum and maximum characteristics.
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 //		// Modifying the units characteristics
167 		map.put(CommonDisplayer.C_UNITS,
168 		    "("+ map.get(CommonDisplayer.C_UNITS) +")");
169 
170 		super.setCharacteristics(map);
171 	}
172 	
173 	/*
174 	 * (non-Javadoc)
175 	 * @see java.lang.Object#equals(java.lang.Object)
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 }