
de.tsl2.nano.collection.MapEntrySet Maven / Gradle / Ivy
/*
* File: $HeadURL$
* Id : $Id$
*
* created by: Tom
* created on: 16.01.2014
*
* Copyright: (c) Thomas Schneider 2014, all rights reserved
*/
package de.tsl2.nano.collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Combines the interfaces of {@link List} and {@link Map}. The underlying instance is a map - the list will hold the
* maps entries {@link Map#entrySet()}.
*
* Problem: It is not possible to create a class implementing both interfaces of {@link List} and {@link Map}. There is
* an interface name-clash on the remote(Object) method.
* Errormessages: The return types are incompatible for the inherited methods Map.remove(Object),
* LinkedList>.remove(Object)
*
* Map.Entry is not serializable!
*
* @author Tom
* @version $Revision$
*/
public class MapEntrySet extends LinkedHashSet> {
/** serialVersionUID */
private static final long serialVersionUID = -7564082533237090900L;
Map map;
boolean sync = true;
private boolean internal;
public MapEntrySet(Map map) {
this.map = map;
Set> entrySet = map.entrySet();
internal = true;
for (Map.Entry entry : entrySet) {
add(new Entry(entry, !sync, map));
}
internal = false;
}
/**
* map
*
* @return refreshed map - having all entries of this instance.
*/
public Map map() {
// map.clear();
for (Map.Entry e : this) {
map.put(e.getKey(), e.getValue());
}
return map;
}
@Override
public boolean add(Entry e) {
if (e == null) {
return false;
}
boolean result = super.add(e);
if (result && e.getKey() != null && !internal && sync) {
map.put(e.getKey(), e.getValue());
}
return result;
}
@SuppressWarnings("rawtypes")
@Override
public boolean remove(Object o) {
boolean result = super.remove(o);
if (result && !internal && sync) {
map.remove(((Map.Entry) o).getKey());
}
return result;
}
/**
* setDirectSynchronization
*
* @param sync if true, setting an entries value will be mapped to the map
*/
public void setDirectSynchronization(boolean sync) {
this.sync = sync;
}
public Entry add(K key, V value) {
Entry e = new Entry(key, value);
return add(e) ? e : null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy