Revert changes to Usermap

This commit is contained in:
snowleo 2012-01-16 02:12:20 +01:00
parent 91563e9dca
commit 81ec87d893

View File

@ -1,20 +1,22 @@
package com.earth2me.essentials; package com.earth2me.essentials;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.util.concurrent.UncheckedExecutionException;
import java.io.File; import java.io.File;
import java.lang.ref.SoftReference; import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.ExecutionException;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
public class UserMap implements IConf public class UserMap extends CacheLoader<String, User> implements IConf
{ {
private final transient IEssentials ess; private final transient IEssentials ess;
private final transient Map<String, SoftReference<User>> users = new HashMap<String, SoftReference<User>>(); private final transient Cache<String, User> users = CacheBuilder.newBuilder().softValues().build(this);
//CacheBuilder.newBuilder().softValues().build(this); private final transient ConcurrentSkipListSet<String> keys = new ConcurrentSkipListSet<String>();
//private final transient ConcurrentSkipListSet<String> keys = new ConcurrentSkipListSet<String>();
public UserMap(final IEssentials ess) public UserMap(final IEssentials ess)
{ {
@ -35,19 +37,16 @@ public class UserMap implements IConf
{ {
return; return;
} }
synchronized (users) keys.clear();
users.invalidateAll();
for (String string : userdir.list())
{ {
users.clear(); if (!string.endsWith(".yml"))
for (String string : userdir.list())
{ {
if (!string.endsWith(".yml")) continue;
{
continue;
}
final String name = string.substring(0, string.length() - 4);
users.put(Util.sanitizeFileName(name), null);
} }
final String name = string.substring(0, string.length() - 4);
keys.add(Util.sanitizeFileName(name));
} }
} }
}); });
@ -55,43 +54,40 @@ public class UserMap implements IConf
public boolean userExists(final String name) public boolean userExists(final String name)
{ {
return users.containsKey(Util.sanitizeFileName(name)); return keys.contains(Util.sanitizeFileName(name));
} }
public User getUser(final String name) public User getUser(final String name)
{ {
try try
{ {
synchronized (users) return users.get(Util.sanitizeFileName(name));
{
final SoftReference<User> softRef = users.get(Util.sanitizeFileName(name));
User user = softRef == null ? null : softRef.get();
if (user == null)
{
user = load(name);
users.put(name, new SoftReference<User>(user));
}
return user;
}
} }
catch (Exception ex) catch (ExecutionException ex)
{
return null;
}
catch (UncheckedExecutionException ex)
{ {
return null; return null;
} }
} }
@Override
public User load(final String name) throws Exception public User load(final String name) throws Exception
{ {
for (Player player : ess.getServer().getOnlinePlayers()) for (Player player : ess.getServer().getOnlinePlayers())
{ {
if (player.getName().equalsIgnoreCase(name)) if (player.getName().equalsIgnoreCase(name))
{ {
keys.add(Util.sanitizeFileName(name));
return new User(player, ess); return new User(player, ess);
} }
} }
final File userFile = getUserFile(name); final File userFile = getUserFile(name);
if (userFile.exists()) if (userFile.exists())
{ {
keys.add(Util.sanitizeFileName(name));
return new User(new OfflinePlayer(name, ess), ess); return new User(new OfflinePlayer(name, ess), ess);
} }
throw new Exception("User not found!"); throw new Exception("User not found!");
@ -105,28 +101,20 @@ public class UserMap implements IConf
public void removeUser(final String name) public void removeUser(final String name)
{ {
synchronized (users) keys.remove(Util.sanitizeFileName(name));
{ users.invalidate(Util.sanitizeFileName(name));
users.remove(Util.sanitizeFileName(name));
}
} }
public Set<String> getAllUniqueUsers() public Set<String> getAllUniqueUsers()
{ {
synchronized (users) return Collections.unmodifiableSet(keys);
{
return new HashSet<String>(users.keySet());
}
} }
public int getUniqueUsers() public int getUniqueUsers()
{ {
synchronized (users) return keys.size();
{
return users.size();
}
} }
public File getUserFile(final String name) public File getUserFile(final String name)
{ {
final File userFolder = new File(ess.getDataFolder(), "userdata"); final File userFolder = new File(ess.getDataFolder(), "userdata");