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