Convert `AccountBadge` to a record

This commit is contained in:
Jon Chambers 2024-10-16 12:45:00 -04:00 committed by Jon Chambers
parent c2270e57df
commit 865e3c5bde
6 changed files with 25 additions and 81 deletions

View File

@ -76,22 +76,22 @@ public class ConfiguredProfileBadgeConverter implements ProfileBadgeConverter, B
final ResourceBundle resourceBundle = headerControlledResourceBundleLookup.getResourceBundle(BASE_NAME,
acceptableLanguages);
List<Badge> badges = accountBadges.stream()
.filter(accountBadge -> (isSelf || accountBadge.isVisible())
&& now.isBefore(accountBadge.getExpiration())
&& knownBadges.containsKey(accountBadge.getId()))
.filter(accountBadge -> (isSelf || accountBadge.visible())
&& now.isBefore(accountBadge.expiration())
&& knownBadges.containsKey(accountBadge.id()))
.map(accountBadge -> {
BadgeConfiguration configuration = knownBadges.get(accountBadge.getId());
BadgeConfiguration configuration = knownBadges.get(accountBadge.id());
return newBadge(
isSelf,
accountBadge.getId(),
accountBadge.id(),
configuration.getCategory(),
resourceBundle.getString(accountBadge.getId() + "_name"),
resourceBundle.getString(accountBadge.getId() + "_description"),
resourceBundle.getString(accountBadge.id() + "_name"),
resourceBundle.getString(accountBadge.id() + "_description"),
configuration.getSprites(),
configuration.getSvg(),
configuration.getSvgs(),
accountBadge.getExpiration(),
accountBadge.isVisible());
accountBadge.expiration(),
accountBadge.visible());
})
.collect(Collectors.toCollection(ArrayList::new));
badges.addAll(badgeIdsEnabledForAll.stream().filter(knownBadges::containsKey).map(id -> {

View File

@ -114,7 +114,7 @@ public record AccountDataReportResponse(UUID reportId,
boolean visible) {
public BadgeDataReport(AccountBadge badge) {
this(badge.getId(), badge.getExpiration(), badge.isVisible());
this(badge.id(), badge.expiration(), badge.visible());
}
}

View File

@ -377,7 +377,7 @@ public class Account {
boolean added = false;
for (int i = 0; i < badges.size(); i++) {
final AccountBadge badgeInList = badges.get(i);
if (Objects.equals(badgeInList.getId(), badge.getId())) {
if (Objects.equals(badgeInList.id(), badge.id())) {
if (added) {
badges.remove(i);
i--;
@ -399,14 +399,14 @@ public class Account {
requireNotStale();
// early exit if it's already the first item in the list
if (!badges.isEmpty() && Objects.equals(badges.get(0).getId(), badgeId)) {
if (!badges.isEmpty() && Objects.equals(badges.get(0).id(), badgeId)) {
purgeStaleBadges(clock);
return;
}
int indexOfBadge = -1;
for (int i = 1; i < badges.size(); i++) {
if (Objects.equals(badgeId, badges.get(i).getId())) {
if (Objects.equals(badgeId, badges.get(i).id())) {
indexOfBadge = i;
break;
}
@ -422,13 +422,13 @@ public class Account {
public void removeBadge(final Clock clock, final String id) {
requireNotStale();
badges.removeIf(accountBadge -> Objects.equals(accountBadge.getId(), id));
badges.removeIf(accountBadge -> Objects.equals(accountBadge.id(), id));
purgeStaleBadges(clock);
}
private void purgeStaleBadges(final Clock clock) {
final Instant now = clock.instant();
badges.removeIf(accountBadge -> now.isAfter(accountBadge.getExpiration()));
badges.removeIf(accountBadge -> now.isAfter(accountBadge.expiration()));
}
public void setRegistrationLockFromAttributes(final AccountAttributes attributes) {

View File

@ -5,31 +5,14 @@
package org.whispersystems.textsecuregcm.storage;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.time.Instant;
import java.util.Objects;
public class AccountBadge {
private final String id;
private final Instant expiration;
private final boolean visible;
@JsonCreator
public AccountBadge(
@JsonProperty("id") String id,
@JsonProperty("expiration") Instant expiration,
@JsonProperty("visible") boolean visible) {
this.id = id;
this.expiration = expiration;
this.visible = visible;
}
public record AccountBadge(String id, Instant expiration, boolean visible) {
/**
* Returns a new AccountBadge that is a merging of the two originals. IDs must match for this operation to make sense.
* The expiration will be the later of the two.
* Visibility will be set if either of the passed in objects is visible.
* The expiration will be the later of the two. Visibility will be set if either of the passed in objects is visible.
*/
public AccountBadge mergeWith(AccountBadge other) {
if (!Objects.equals(other.id, id)) {
@ -62,43 +45,4 @@ public class AccountBadge {
visible);
}
}
public String getId() {
return id;
}
public Instant getExpiration() {
return expiration;
}
public boolean isVisible() {
return visible;
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
AccountBadge that = (AccountBadge) o;
return visible == that.visible && Objects.equals(id, that.id)
&& Objects.equals(expiration, that.expiration);
}
@Override
public int hashCode() {
return Objects.hash(id, expiration, visible);
}
@Override
public String toString() {
return "AccountBadge{" +
"id='" + id + '\'' +
", expiration=" + expiration +
", visible=" + visible +
'}';
}
}

View File

@ -37,7 +37,7 @@ public class ProfileHelper {
final List<AccountBadge> accountBadges) {
LinkedHashMap<String, AccountBadge> existingBadges = new LinkedHashMap<>(accountBadges.size());
for (final AccountBadge accountBadge : accountBadges) {
existingBadges.putIfAbsent(accountBadge.getId(), accountBadge);
existingBadges.putIfAbsent(accountBadge.id(), accountBadge);
}
LinkedHashMap<String, AccountBadge> result = new LinkedHashMap<>(accountBadges.size());
@ -67,7 +67,7 @@ public class ProfileHelper {
for (final Map.Entry<String, AccountBadge> entry : existingBadges.entrySet()) {
if (!result.containsKey(entry.getKey())) {
AccountBadge accountBadge = entry.getValue().withVisibility(false);
result.put(accountBadge.getId(), accountBadge);
result.put(accountBadge.id(), accountBadge);
}
}

View File

@ -233,17 +233,17 @@ class AccountTest {
account.addBadge(clock, new AccountBadge("foo", Instant.ofEpochSecond(50), false));
assertThat(account.getBadges()).hasSize(2).element(0).satisfies(badge -> {
assertThat(badge.getId()).isEqualTo("foo");
assertThat(badge.getExpiration().getEpochSecond()).isEqualTo(50);
assertThat(badge.isVisible()).isFalse();
assertThat(badge.id()).isEqualTo("foo");
assertThat(badge.expiration().getEpochSecond()).isEqualTo(50);
assertThat(badge.visible()).isFalse();
});
account.addBadge(clock, new AccountBadge("foo", Instant.ofEpochSecond(51), true));
assertThat(account.getBadges()).hasSize(2).element(0).satisfies(badge -> {
assertThat(badge.getId()).isEqualTo("foo");
assertThat(badge.getExpiration().getEpochSecond()).isEqualTo(51);
assertThat(badge.isVisible()).isTrue();
assertThat(badge.id()).isEqualTo("foo");
assertThat(badge.expiration().getEpochSecond()).isEqualTo(51);
assertThat(badge.visible()).isTrue();
});
}