mapper.java
字号:
package piy;
import java.util.*;
/**
* A class that provides mappings in both directions, ie key->value and value->key.
* It is not necessary that values are unique, but only one key will be returned for
* a given value. Automatically resizes itself.
* @author David Vivash
* @version 1.0, 19/11/00
*/
public class Mapper implements java.io.Serializable
{
private List keys;
private List values;
/**
* Construct a new Mapper object with the specified size. The mapper will
* automatically grow if this initial size is not enough.
* @param size the initial size of the Mapper object
*/
public Mapper(int size) {
keys = new ArrayList(size);
values = new ArrayList(size);
}
/**
* Stores a key->value mapping. The key mustn't already map to a value, otherwise
* the old mapping will be lost and the new mapping will be specified. The old value
* that key mapped to will be returned.
* @param key the thing being mapped from
* @param value the thing being mapped to
* @return the old value that key mapped to, or null
*/
public Object put(Object key, Object value) {
//Note that we only add elements in key order, so the key list is always sorted.
int position = Collections.binarySearch(keys, key);
Object oldValue = null;
//position represents either where the key is currently placed, or if the
//key is not present, position represents -(insertionPoint)-1
if (position < 0) { //Key not currently being used
int insertionPoint = -(position+1);
keys.add(insertionPoint, key);
values.add(insertionPoint, value);
} else {
oldValue = values.get(position);
values.remove(position); //get rid of the old value
values.add(position, value);
}
return oldValue;
}
/**
* Gets a value from a specified key mapping. This method should run in log(n) time where
* n represents the number of keys to search through. Keys are checked for equality using
* the equals method
* @param key the key for the required value
* @return the value that the specified key maps to, or null if the key is not found
*/
public Object getValue(Object key) {
int position = Collections.binarySearch(keys, key);
if (position >= 0) return values.get(position);
else return null;
}
/**
* More expensive that getValue(), this method runs in linear time. It will return a key
* that maps to the value specified, or null if the value is not found. (Objects are
* compared using the equals method
* @param value the thing to find a mapping to
* @return a key that maps to the specified value, or null if the value is not found
*/
public Object getKey(Object value) {
int position = values.indexOf(value);
if (position >= 0) return keys.get(position);
else return null;
}
/**
* Retrieves a shallow copy of the list of values mapped to in this object. The entries
* in the list are the references to the values, but modifying the list or the values
* will not affect this object.
* @return a list of values mapped to in this object
*/
public List values() { return (List)((ArrayList)values).clone(); }
/**
* Retrieves a shallow copy of the list of keys used in the mapping in this object. The
* entries in the list are the references to the keys. Modifying the list will not affect
* this object, but modifying the keys being referenced will have undetermined results.
* @return a list of keys involved in the mappings in this object
*/
public List keys() { return (List)((ArrayList)keys).clone(); }
/**
* Removes a mapping given a valid key.
* @param key the key in a key->value mapping to be removed
* @return the value that was mapped to from the key
*/
public Object removeKey(Object key) {
int position = Collections.binarySearch(keys, key);
if (position >= 0) {
keys.remove(position);
return values.remove(position);
}
else return null;
}
/**
* Removes a mapping given a valid values.
* @param value the value in a key->value mapping to be removed
* @return a key that maps to the value. Other keys may exist for which a
* key->value mapping is still present
*/
public Object removeValue(Object value) {
int position = values.indexOf(value);
if (position >= 0) {
values.remove(position);
return keys.remove(position);
}
else return null;
}
/**
* Clears all mappings.
*/
public void clear() {
keys.clear();
values.clear();
}
/**
* Returns th number of mapping that have curerntly been set up. This is not
* the same as the current <i>capacity</i> of this Mapper object.
* @return the number of elements mapped in this Mapper object
*/
public int size() {
return keys.size();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -