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  
27  /**
28   * <code>ExponentialConverter</code> uses the supplied values
29   * as the exponential factor to the specified base. If the received value is
30   * x, the returned value is <code>base^x</code>.
31   * 
32   * @author <a href="mailto:jaka.bobnar@cosylab.com">Jaka Bobnar</a>
33   * @version $Id: ExponentialConverter.java,v 1.7 2008-04-22 12:31:02 jbobnar Exp $
34   *
35   */
36  public class ExponentialConverter extends SimpleConverterSupport {
37  
38  	private static final long serialVersionUID = -1789030566494308338L;
39  
40  	public static final String SHORT_NAME = "exponential";
41  	
42  	private double base = Math.E, logBase = 1;
43  	
44  	/**
45  	 * Constructs a new ExponentialConverter with base e = 2.71828...
46  	 * 
47  	 * @see Math#E
48  	 */
49  	public ExponentialConverter() {
50  		setBase(Math.E);
51  	}
52  	
53  	/**
54  	 * Constructs a new ExponentialConverter with the provided base.
55  	 * 
56  	 * @param base
57  	 */
58  	public ExponentialConverter(double base){
59  		setBase(base);
60  	}
61  	
62  	/*
63  	 * (non-Javadoc)
64  	 * @see com.cosylab.gui.adapters.SimpleConverterSupport#inverseTransform(double)
65  	 */
66  	@Override
67  	protected double inverseTransform(double value) {
68  		if (value <= 0) return 0;
69  		return Math.log(value)/getLogBase();
70  	}
71  
72  	/*
73  	 * (non-Javadoc)
74  	 * @see com.cosylab.gui.adapters.SimpleConverterSupport#transform(double)
75  	 */
76  	@Override
77  	protected double transform(double value) {
78  		return Math.pow(getBase(), value);
79  	}
80  	
81  	/*
82  	 * (non-Javadoc)
83  	 * @see com.cosylab.gui.adapters.DataConsumerDispatcher#getName()
84  	 */
85  	public String getName(){
86  		return SHORT_NAME;
87  	}
88  	
89  	/*
90  	 * (non-Javadoc)
91  	 * @see java.lang.Object#toString()
92  	 */
93  	public String toString(){
94  		return SHORT_NAME + ": " + getBase();
95  	}
96  	
97  	/**
98  	 * Returns the base used in this converter.
99  	 * 
100 	 * @return
101 	 */
102 	public double getBase() {
103 		return base;
104 	}
105 
106 	/**
107 	 * Sets the base factor used for converting values.
108 	 * 
109 	 * @param base
110 	 */
111 	public void setBase(double base) {
112 		if (base <= 0) return;
113 		this.base = base;
114 		setLogBase(Math.log(base));
115 	}
116 
117 	protected double getLogBase() {
118 		return logBase;
119 	}
120 
121 	protected void setLogBase(double logBase) {
122 		this.logBase = logBase;
123 	}
124 	
125 	public void setCharacteristics(Map characteristics)
126 	{
127 		// We store unmodified characteristic for possible later reinitialization of 
128 		// consumers, eg. if multiplication factor changes
129 		cacheLastCharacteristics(characteristics);
130 
131 		HashMap map = new HashMap(characteristics);
132 
133 		double min;
134 		double max;
135 		
136 		// Modifying the graph_min and graph_max characteristics.
137 		if (map.containsKey(CommonDisplayer.C_GRAPH_MIN)
138 		    && map.containsKey(CommonDisplayer.C_GRAPH_MAX)) {
139 			min = ((Number)map.get(CommonDisplayer.C_GRAPH_MIN)).doubleValue();
140 			max = ((Number)map.get(CommonDisplayer.C_GRAPH_MAX)).doubleValue();
141 				
142 			map.put(CommonDisplayer.C_GRAPH_MIN,
143 			    new Double(transform(min)));
144 			map.put(CommonDisplayer.C_GRAPH_MAX,
145 			    new Double(transform(max)));
146 		}
147 		
148 		// Modifying the minimum and maximum characteristics.
149 		if (map.containsKey(CommonDisplayer.C_MINIMUM)
150 		    && map.containsKey(CommonDisplayer.C_MAXIMUM)) {
151 			min = ((Number)map.get(CommonDisplayer.C_MINIMUM)).doubleValue();
152 			max = ((Number)map.get(CommonDisplayer.C_MAXIMUM)).doubleValue();
153 						
154 			map.put(CommonDisplayer.C_MINIMUM,
155 			    new Double(transform(min)));
156 			map.put(CommonDisplayer.C_MAXIMUM,
157 			    new Double(transform(max)));
158 		}
159 
160 		// Modifying the units characteristics
161 		map.put(CommonDisplayer.C_UNITS,
162 		    "log_"+getBase()+"("+ map.get(CommonDisplayer.C_UNITS)+")");
163 
164 		super.setCharacteristics(map);
165 	}
166 	
167 	/*
168 	 * (non-Javadoc)
169 	 * @see java.lang.Object#equals(java.lang.Object)
170 	 */
171 	@Override
172 	public boolean equals(Object obj) {
173 		if (!(obj instanceof ExponentialConverter)) return false;
174 		ExponentialConverter c = (ExponentialConverter)obj;
175 		return c.getBase() == getBase();
176 	}
177 }