Remove redundant Optional usages

This commit is contained in:
DNx5 2016-12-06 01:24:36 +07:00
parent 2d27c0cbb6
commit 2586963780

View File

@ -1,6 +1,5 @@
package fr.xephi.authme.datasource; package fr.xephi.authme.datasource;
import com.google.common.base.Optional;
import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader; import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache; import com.google.common.cache.LoadingCache;
@ -11,22 +10,19 @@ import com.google.common.util.concurrent.ThreadFactoryBuilder;
import fr.xephi.authme.ConsoleLogger; import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.data.auth.PlayerAuth; import fr.xephi.authme.data.auth.PlayerAuth;
import fr.xephi.authme.data.auth.PlayerCache; import fr.xephi.authme.data.auth.PlayerCache;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.datasource.DataSourceType;
import fr.xephi.authme.security.crypts.HashedPassword; import fr.xephi.authme.security.crypts.HashedPassword;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public class CacheDataSource implements DataSource { public class CacheDataSource implements DataSource {
private final DataSource source; private final DataSource source;
private final LoadingCache<String, Optional<PlayerAuth>> cachedAuths; private final LoadingCache<String, PlayerAuth> cachedAuths;
private final ListeningExecutorService executorService; private final ListeningExecutorService executorService;
/** /**
@ -45,25 +41,20 @@ public class CacheDataSource implements DataSource {
cachedAuths = CacheBuilder.newBuilder() cachedAuths = CacheBuilder.newBuilder()
.refreshAfterWrite(5, TimeUnit.MINUTES) .refreshAfterWrite(5, TimeUnit.MINUTES)
.expireAfterAccess(15, TimeUnit.MINUTES) .expireAfterAccess(15, TimeUnit.MINUTES)
.build(new CacheLoader<String, Optional<PlayerAuth>>() { .build(new CacheLoader<String, PlayerAuth>() {
@Override @Override
public Optional<PlayerAuth> load(String key) { public PlayerAuth load(String key) {
return Optional.fromNullable(source.getAuth(key)); return source.getAuth(key);
} }
@Override @Override
public ListenableFuture<Optional<PlayerAuth>> reload(final String key, Optional<PlayerAuth> oldValue) { public ListenableFuture<PlayerAuth> reload(final String key, PlayerAuth oldValue) {
return executorService.submit(new Callable<Optional<PlayerAuth>>() { return executorService.submit(() -> load(key));
@Override
public Optional<PlayerAuth> call() {
return load(key);
}
});
} }
}); });
} }
public LoadingCache<String, Optional<PlayerAuth>> getCachedAuths() { public LoadingCache<String, PlayerAuth> getCachedAuths() {
return cachedAuths; return cachedAuths;
} }
@ -80,9 +71,9 @@ public class CacheDataSource implements DataSource {
@Override @Override
public HashedPassword getPassword(String user) { public HashedPassword getPassword(String user) {
user = user.toLowerCase(); user = user.toLowerCase();
Optional<PlayerAuth> pAuthOpt = cachedAuths.getIfPresent(user); PlayerAuth auth = cachedAuths.getIfPresent(user);
if (pAuthOpt != null && pAuthOpt.isPresent()) { if (auth != null) {
return pAuthOpt.get().getPassword(); return auth.getPassword();
} }
return source.getPassword(user); return source.getPassword(user);
} }
@ -90,7 +81,7 @@ public class CacheDataSource implements DataSource {
@Override @Override
public PlayerAuth getAuth(String user) { public PlayerAuth getAuth(String user) {
user = user.toLowerCase(); user = user.toLowerCase();
return cachedAuths.getUnchecked(user).orNull(); return cachedAuths.getUnchecked(user);
} }
@Override @Override