create changelog entry
[debian/openrocket] / android-libraries / achartengine / src / org / achartengine / util / IndexXYMap.java
1 /**
2  * Copyright (C) 2009 - 2012 SC 4ViewSoft SRL
3  *  
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *  
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *  
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.achartengine.util;
17
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.List;
21 import java.util.TreeMap;
22
23 /**
24  * This class requires sorted x values
25  */
26 public class IndexXYMap<K, V> extends TreeMap<K, V> {
27   private final List<K> indexList = new ArrayList<K>();
28
29   private double maxXDifference = 0;
30
31   public IndexXYMap() {
32     super();
33   }
34
35   public V put(K key, V value) {
36     indexList.add(key);
37     updateMaxXDifference();
38     return super.put(key, value);
39   }
40
41   private void updateMaxXDifference() {
42     if (indexList.size() < 2) {
43       maxXDifference = 0;
44       return;
45     }
46
47     if (Math.abs((Double) indexList.get(indexList.size() - 1)
48         - (Double) indexList.get(indexList.size() - 2)) > maxXDifference)
49       maxXDifference = Math.abs((Double) indexList.get(indexList.size() - 1)
50           - (Double) indexList.get(indexList.size() - 2));
51   }
52
53   public double getMaxXDifference() {
54     return maxXDifference;
55   }
56
57   public void clear() {
58     updateMaxXDifference();
59     super.clear();
60     indexList.clear();
61   }
62
63   /**
64    * Returns X-value according to the given index
65    * 
66    * @param index
67    * @return the X value
68    */
69   public K getXByIndex(int index) {
70     return indexList.get(index);
71   }
72
73   /**
74    * Returns Y-value according to the given index
75    * 
76    * @param index
77    * @return the Y value
78    */
79   public V getYByIndex(int index) {
80     K key = indexList.get(index);
81     return this.get(key);
82   }
83
84   /**
85    * Returns XY-entry according to the given index
86    * 
87    * @param index
88    * @return the X and Y values
89    */
90   public XYEntry<K, V> getByIndex(int index) {
91     K key = indexList.get(index);
92     return new XYEntry<K, V>(key, this.get(key));
93   }
94
95   /**
96    * Removes entry from map by index
97    * 
98    * @param index
99    */
100   public XYEntry<K, V> removeByIndex(int index) {
101     K key = indexList.remove(index);
102     return new XYEntry<K, V>(key, this.remove(key));
103   }
104
105   public int getIndexForKey(K key) {
106     return Collections.binarySearch(indexList, key, null);
107   }
108 }