@Test public void checkUsername_invalidCharacters() { assertEquals(UsernameUtil.InvalidReason.INVALID_CHARACTERS, UsernameUtil.checkUsername("$abcd").get()); assertEquals(UsernameUtil.InvalidReason.INVALID_CHARACTERS, UsernameUtil.checkUsername(" abcd").get()); assertEquals(UsernameUtil.InvalidReason.INVALID_CHARACTERS, UsernameUtil.checkUsername("ab cde").get()); assertEquals(UsernameUtil.InvalidReason.INVALID_CHARACTERS, UsernameUtil.checkUsername("%%%%%").get()); assertEquals(UsernameUtil.InvalidReason.INVALID_CHARACTERS, UsernameUtil.checkUsername("-----").get()); assertEquals(UsernameUtil.InvalidReason.INVALID_CHARACTERS, UsernameUtil.checkUsername("asĸ_me").get()); assertEquals(UsernameUtil.InvalidReason.INVALID_CHARACTERS, UsernameUtil.checkUsername("+18675309").get()); }
public static Optional<InvalidReason> checkUsername(@Nullable String value) { if (value == null) { return Optional.of(InvalidReason.TOO_SHORT); } else if (value.length() < MIN_LENGTH) { return Optional.of(InvalidReason.TOO_SHORT); } else if (value.length() > MAX_LENGTH) { return Optional.of(InvalidReason.TOO_LONG); } else if (DIGIT_START_PATTERN.matcher(value).matches()) { return Optional.of(InvalidReason.STARTS_WITH_NUMBER); } else if (!FULL_PATTERN.matcher(value).matches()) { return Optional.of(InvalidReason.INVALID_CHARACTERS); } else { return Optional.absent(); } }
UsernameUtil { public static Optional<InvalidReason> checkUsername(@Nullable String value) { if (value == null) { return Optional.of(InvalidReason.TOO_SHORT); } else if (value.length() < MIN_LENGTH) { return Optional.of(InvalidReason.TOO_SHORT); } else if (value.length() > MAX_LENGTH) { return Optional.of(InvalidReason.TOO_LONG); } else if (DIGIT_START_PATTERN.matcher(value).matches()) { return Optional.of(InvalidReason.STARTS_WITH_NUMBER); } else if (!FULL_PATTERN.matcher(value).matches()) { return Optional.of(InvalidReason.INVALID_CHARACTERS); } else { return Optional.absent(); } } }
UsernameUtil { public static Optional<InvalidReason> checkUsername(@Nullable String value) { if (value == null) { return Optional.of(InvalidReason.TOO_SHORT); } else if (value.length() < MIN_LENGTH) { return Optional.of(InvalidReason.TOO_SHORT); } else if (value.length() > MAX_LENGTH) { return Optional.of(InvalidReason.TOO_LONG); } else if (DIGIT_START_PATTERN.matcher(value).matches()) { return Optional.of(InvalidReason.STARTS_WITH_NUMBER); } else if (!FULL_PATTERN.matcher(value).matches()) { return Optional.of(InvalidReason.INVALID_CHARACTERS); } else { return Optional.absent(); } }  }
UsernameUtil { public static Optional<InvalidReason> checkUsername(@Nullable String value) { if (value == null) { return Optional.of(InvalidReason.TOO_SHORT); } else if (value.length() < MIN_LENGTH) { return Optional.of(InvalidReason.TOO_SHORT); } else if (value.length() > MAX_LENGTH) { return Optional.of(InvalidReason.TOO_LONG); } else if (DIGIT_START_PATTERN.matcher(value).matches()) { return Optional.of(InvalidReason.STARTS_WITH_NUMBER); } else if (!FULL_PATTERN.matcher(value).matches()) { return Optional.of(InvalidReason.INVALID_CHARACTERS); } else { return Optional.absent(); } }  static boolean isValidUsernameForSearch(@Nullable String value); static Optional<InvalidReason> checkUsername(@Nullable String value); @WorkerThread static @NonNull Optional<UUID> fetchUuidForUsername(@NonNull Context context, @NonNull String username); }
UsernameUtil { public static Optional<InvalidReason> checkUsername(@Nullable String value) { if (value == null) { return Optional.of(InvalidReason.TOO_SHORT); } else if (value.length() < MIN_LENGTH) { return Optional.of(InvalidReason.TOO_SHORT); } else if (value.length() > MAX_LENGTH) { return Optional.of(InvalidReason.TOO_LONG); } else if (DIGIT_START_PATTERN.matcher(value).matches()) { return Optional.of(InvalidReason.STARTS_WITH_NUMBER); } else if (!FULL_PATTERN.matcher(value).matches()) { return Optional.of(InvalidReason.INVALID_CHARACTERS); } else { return Optional.absent(); } }  static boolean isValidUsernameForSearch(@Nullable String value); static Optional<InvalidReason> checkUsername(@Nullable String value); @WorkerThread static @NonNull Optional<UUID> fetchUuidForUsername(@NonNull Context context, @NonNull String username); static final int MIN_LENGTH; static final int MAX_LENGTH; }