X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=android-libraries%2Fachartengine%2Fsrc%2Forg%2Fachartengine%2Futil%2FIndexXYMap.java;fp=android-libraries%2Fachartengine%2Fsrc%2Forg%2Fachartengine%2Futil%2FIndexXYMap.java;h=f957262229d9061402118bb4487a0baf00581e40;hb=dfe153139b917cc91dddc1efa0298d4204616462;hp=0000000000000000000000000000000000000000;hpb=a2fe0ba2c348102bb0c6486b201be097523c2c9a;p=debian%2Fopenrocket diff --git a/android-libraries/achartengine/src/org/achartengine/util/IndexXYMap.java b/android-libraries/achartengine/src/org/achartengine/util/IndexXYMap.java new file mode 100644 index 00000000..f9572622 --- /dev/null +++ b/android-libraries/achartengine/src/org/achartengine/util/IndexXYMap.java @@ -0,0 +1,108 @@ +/** + * Copyright (C) 2009 - 2012 SC 4ViewSoft SRL + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.achartengine.util; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.TreeMap; + +/** + * This class requires sorted x values + */ +public class IndexXYMap extends TreeMap { + private final List indexList = new ArrayList(); + + private double maxXDifference = 0; + + public IndexXYMap() { + super(); + } + + public V put(K key, V value) { + indexList.add(key); + updateMaxXDifference(); + return super.put(key, value); + } + + private void updateMaxXDifference() { + if (indexList.size() < 2) { + maxXDifference = 0; + return; + } + + if (Math.abs((Double) indexList.get(indexList.size() - 1) + - (Double) indexList.get(indexList.size() - 2)) > maxXDifference) + maxXDifference = Math.abs((Double) indexList.get(indexList.size() - 1) + - (Double) indexList.get(indexList.size() - 2)); + } + + public double getMaxXDifference() { + return maxXDifference; + } + + public void clear() { + updateMaxXDifference(); + super.clear(); + indexList.clear(); + } + + /** + * Returns X-value according to the given index + * + * @param index + * @return the X value + */ + public K getXByIndex(int index) { + return indexList.get(index); + } + + /** + * Returns Y-value according to the given index + * + * @param index + * @return the Y value + */ + public V getYByIndex(int index) { + K key = indexList.get(index); + return this.get(key); + } + + /** + * Returns XY-entry according to the given index + * + * @param index + * @return the X and Y values + */ + public XYEntry getByIndex(int index) { + K key = indexList.get(index); + return new XYEntry(key, this.get(key)); + } + + /** + * Removes entry from map by index + * + * @param index + */ + public XYEntry removeByIndex(int index) { + K key = indexList.remove(index); + return new XYEntry(key, this.remove(key)); + } + + public int getIndexForKey(K key) { + return Collections.binarySearch(indexList, key, null); + } +}