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