2 * Copyright (C) 2009 - 2012 SC 4ViewSoft SRL
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.achartengine.util;
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.List;
21 import java.util.TreeMap;
24 * This class requires sorted x values
26 public class IndexXYMap<K, V> extends TreeMap<K, V> {
27 private final List<K> indexList = new ArrayList<K>();
29 private double maxXDifference = 0;
35 public V put(K key, V value) {
37 updateMaxXDifference();
38 return super.put(key, value);
41 private void updateMaxXDifference() {
42 if (indexList.size() < 2) {
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));
53 public double getMaxXDifference() {
54 return maxXDifference;
58 updateMaxXDifference();
64 * Returns X-value according to the given index
69 public K getXByIndex(int index) {
70 return indexList.get(index);
74 * Returns Y-value according to the given index
79 public V getYByIndex(int index) {
80 K key = indexList.get(index);
85 * Returns XY-entry according to the given index
88 * @return the X and Y values
90 public XYEntry<K, V> getByIndex(int index) {
91 K key = indexList.get(index);
92 return new XYEntry<K, V>(key, this.get(key));
96 * Removes entry from map by index
100 public XYEntry<K, V> removeByIndex(int index) {
101 K key = indexList.remove(index);
102 return new XYEntry<K, V>(key, this.remove(key));
105 public int getIndexForKey(K key) {
106 return Collections.binarySearch(indexList, key, null);