com.jmatio.common.DeterministicKeyMap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of matfilerw Show documentation
Show all versions of matfilerw Show documentation
MatFileRW - Read and write .mat files
/*
* Code licensed under new-style BSD (see LICENSE).
* All code up to tags/original: Copyright (c) 2006, Wojciech Gradkowski
* All code after tags/original: Copyright (c) 2015, DiffPlug
*/
package com.jmatio.common;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
/**
* A map implementation which guarantees that all of its iterators
* (keySet(), values(), and entrySet()) will be in the same order
* as the keyOrder set which is passed in the constructor.
*
* The keySet must contain all of the keys in the delegate, but it's
* okay if the keySet contains more.
*
* Useful in MLObject and MLStruct for ensuring that arrays have
* their fields in the same order.
*/
public class DeterministicKeyMap extends ForwardingMap {
private final Set keyOrder;
/**
*
* @param keyOrder A set which must always contain all of the keys in delegate, and may contain more.
* @param delegate An underlying map.
*/
public DeterministicKeyMap(Set keyOrder, Map delegate) {
super(delegate);
this.keyOrder = keyOrder;
}
/** Returns the keyset of this map in the same order as keyOrder. */
@SuppressWarnings("unchecked")
@Override
public Set keySet() {
return new DeterministicSet(delegate.keySet(), (Function) identity);
}
/** Returns the values of this map in the same order as keyOrder. */
@Override
public Collection values() {
return new DeterministicCollection(delegate.values(), new Function() {
@Override
public V apply(K input) {
return delegate.get(input);
}
});
}
/** Returns the entries of this map in the same order as keyOrder. */
@Override
public Set> entrySet() {
return new DeterministicSet>(delegate.entrySet(), new Function>() {
@Override
public Map.Entry apply(final K key) {
return new Map.Entry() {
@Override
public K getKey() {
return key;
}
@Override
public V getValue() {
return delegate.get(key);
}
@Override
public V setValue(V value) {
return delegate.put(key, value);
}
};
}
});
}
@Override
public boolean equals(Object other) {
return super.equals(other);
}
@Override
public int hashCode() {
return super.hashCode();
}
/** Java didn't find functional programming until Java 8. */
static interface Function {
R apply(T input);
}
static final Function