Improve Uuids#parse

thanks @kashike
This commit is contained in:
Luck 2018-09-20 15:30:22 +01:00
parent d28170cc3f
commit 834649b390
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
10 changed files with 23 additions and 24 deletions

View File

@ -35,7 +35,7 @@ import java.util.UUID;
public final class BukkitUuids {
public static UUID lookupUuid(ProgressLogger log, String s) {
UUID uuid = Uuids.parseNullable(s);
UUID uuid = Uuids.parse(s);
if (uuid == null) {
try {
//noinspection deprecation

View File

@ -170,7 +170,7 @@ public class MigrationGroupManager extends SubCommand<Object> {
}
String lastName = user.getLastName();
if (lastName != null && Uuids.parse(lastName).isPresent()) {
if (lastName != null && Uuids.parse(lastName) != null) {
lastName = null;
}

View File

@ -58,7 +58,7 @@ public class CheckCommand extends SingleCommand {
String permission = args.get(1);
User user;
UUID u = Uuids.parseNullable(target);
UUID u = Uuids.parse(target);
if (u != null) {
user = plugin.getUserManager().getIfLoaded(u);
} else {

View File

@ -68,7 +68,7 @@ public class TreeCommand extends SingleCommand {
User user;
if (player != null) {
UUID u = Uuids.parseNullable(player);
UUID u = Uuids.parse(player);
if (u != null) {
user = plugin.getUserManager().getIfLoaded(u);
} else {

View File

@ -81,7 +81,7 @@ public class UserMainCommand extends MainCommand<User, UserIdentifier> {
}
public static UUID parseTargetUuid(String target, LuckPermsPlugin plugin, Sender sender) {
UUID uuid = Uuids.parseNullable(target);
UUID uuid = Uuids.parse(target);
if (uuid == null) {
if (!plugin.getConfiguration().get(ConfigKeys.ALLOW_INVALID_USERNAMES)) {
if (!DataConstraints.PLAYER_USERNAME_TEST.test(target)) {

View File

@ -77,7 +77,7 @@ public class DependencyRegistry {
}
// don't load slf4j if it's already present
if (slf4jPresent()) {
if (dependencies.contains(Dependency.SLF4J_API) || dependencies.contains(Dependency.SLF4J_SIMPLE) && slf4jPresent()) {
dependencies.remove(Dependency.SLF4J_API);
dependencies.remove(Dependency.SLF4J_SIMPLE);
}

View File

@ -165,7 +165,7 @@ public class SeparatedConfigurateDao extends AbstractConfigurateDao {
}
String user = s.substring(0, s.length() - this.fileExtension.length());
UUID uuid = Uuids.parseNullable(user);
UUID uuid = Uuids.parse(user);
if (uuid == null) {
return;
}

View File

@ -27,18 +27,14 @@ package me.lucko.luckperms.common.utils;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Predicate;
import java.util.regex.Pattern;
/**
* Utilities for working with {@link UUID}s.
*/
public final class Uuids {
private static final Pattern UUID_PATTERN = Pattern.compile("(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})");
public static final Predicate<String> PREDICATE = s -> parseNullable(s) != null;
public static final Predicate<String> PREDICATE = s -> parse(s) != null;
public static @Nullable UUID fromString(String s) {
try {
@ -48,18 +44,21 @@ public final class Uuids {
}
}
public static @Nullable UUID parseNullable(String s) {
public static @Nullable UUID parse(String s) {
UUID uuid = fromString(s);
if (uuid == null) {
uuid = fromString(UUID_PATTERN.matcher(s).replaceAll("$1-$2-$3-$4-$5"));
if (uuid == null && s.length() == 32) {
try {
uuid = new UUID(
Long.parseUnsignedLong(s.substring(0, 16), 16),
Long.parseUnsignedLong(s.substring(16), 16)
);
} catch (NumberFormatException e) {
// ignore
}
}
return uuid;
}
public static Optional<UUID> parse(String s) {
return Optional.ofNullable(parseNullable(s));
}
private Uuids() {}
}

View File

@ -130,7 +130,7 @@ public final class WebEditor {
return holder;
} else if (who.startsWith(USER_ID_PATTERN)) {
String user = who.substring(USER_ID_PATTERN.length());
UUID uuid = Uuids.parseNullable(user);
UUID uuid = Uuids.parse(user);
if (uuid == null) {
Message.APPLY_EDITS_TARGET_USER_NOT_UUID.send(sender, user);
return null;

View File

@ -137,7 +137,7 @@ public class SpongeUserManager extends AbstractUserManager<SpongeUser> implement
@Override
public CompletableFuture<LPSubject> loadSubject(String identifier) {
UUID uuid = Uuids.parseNullable(identifier);
UUID uuid = Uuids.parse(identifier);
if (uuid == null) {
throw new IllegalArgumentException("Identifier is not a UUID: " + identifier);
}
@ -152,7 +152,7 @@ public class SpongeUserManager extends AbstractUserManager<SpongeUser> implement
@Override
public Optional<LPSubject> getSubject(String identifier) {
UUID uuid = Uuids.parseNullable(identifier);
UUID uuid = Uuids.parse(identifier);
if (uuid == null) {
return Optional.empty();
}
@ -161,7 +161,7 @@ public class SpongeUserManager extends AbstractUserManager<SpongeUser> implement
@Override
public CompletableFuture<Boolean> hasRegistered(String identifier) {
UUID uuid = Uuids.parseNullable(identifier);
UUID uuid = Uuids.parse(identifier);
if (uuid == null) {
return CompletableFuture.completedFuture(false);
}
@ -178,7 +178,7 @@ public class SpongeUserManager extends AbstractUserManager<SpongeUser> implement
return CompletableFuture.supplyAsync(() -> {
ImmutableSet.Builder<LPSubject> ret = ImmutableSet.builder();
for (String id : identifiers) {
UUID uuid = Uuids.parseNullable(id);
UUID uuid = Uuids.parse(id);
if (uuid == null) {
continue;
}