Small extra stuffs. PS.getDistinctChunks and reverseIndex of map.

This commit is contained in:
Olof Larsson 2014-10-13 11:42:25 +02:00
parent 938efaeed4
commit d375ccccb6
2 changed files with 38 additions and 0 deletions

View File

@ -1,6 +1,9 @@
package com.massivecraft.massivecore.ps; package com.massivecraft.massivecore.ps;
import java.io.Serializable; import java.io.Serializable;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.Map.Entry; import java.util.Map.Entry;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -839,6 +842,20 @@ public final class PS implements Cloneable, Serializable, Comparable<PS>
return inSameUniverse(one, two, aspect.getMultiverse()); return inSameUniverse(one, two, aspect.getMultiverse());
} }
// -------------------------------------------- //
// GET SETS
// -------------------------------------------- //
public static Set<PS> getDistinctChunks(Collection<PS> pss)
{
Set<PS> ret = new LinkedHashSet<PS>();
for (PS ps : pss)
{
ret.add(ps.getChunk(true));
}
return ret;
}
// -------------------------------------------- // // -------------------------------------------- //
// HASHCODE (CACHED) // HASHCODE (CACHED)
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -653,6 +653,27 @@ public class MUtil
return ret; return ret;
} }
public static <K, V> Map<V, Set<K>> reverseIndex(Map<K, V> map)
{
Map<V, Set<K>> ret = new LinkedHashMap<V, Set<K>>();
for (Entry<K, V> entry : map.entrySet())
{
K key = entry.getKey();
V value = entry.getValue();
Set<K> set = ret.get(value);
if (set == null)
{
set = new HashSet<K>();
ret.put(value, set);
}
set.add(key);
}
return ret;
}
// -------------------------------------------- // // -------------------------------------------- //
// COLLECTION MANIPULATION // COLLECTION MANIPULATION
// -------------------------------------------- // // -------------------------------------------- //