Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions f...
If you think the Android project Dual-Battery-Widget listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
/**
* Copyright (C) 2009 - 2012 SC 4ViewSoft SRL
* //www.java2s.com
* 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
*/publicclass IndexXYMap<K, V> extends TreeMap<K, V> {
privatefinal List<K> indexList = new ArrayList<K>();
privatedouble maxXDifference = 0;
public IndexXYMap() {
super();
}
public V put(K key, V value) {
indexList.add(key);
updateMaxXDifference();
return super.put(key, value);
}
privatevoid 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));
}
publicdouble getMaxXDifference() {
return maxXDifference;
}
publicvoid 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<K, V> getByIndex(int index) {
K key = indexList.get(index);
returnnew XYEntry<K, V>(key, this.get(key));
}
/**
* Removes entry from map by index
*
* @param index
*/public XYEntry<K, V> removeByIndex(int index) {
K key = indexList.remove(index);
returnnew XYEntry<K, V>(key, this.remove(key));
}
publicint getIndexForKey(K key) {
return Collections.binarySearch(indexList, key, null);
}
}