@Override public boolean equals(Object o) { if (o == null || ! (o instanceof Quota)) { return false; } Quota<?> other = (Quota<?>) o; return Objects.equal(used, other.getUsed()) && Objects.equal(limit,other.getLimit()); }
@Override public String toString() { StringBuilder result = new StringBuilder() .append(ImapConstants.QUOTA_RESPONSE_NAME) .append(' ') .append(quotaRoot) .append(' ') .append('(') .append(resourceName) .append(' ') .append(quota.getUsed()) .append(' ') .append(quota.getLimit()) .append(')'); return result.toString(); }
public static <U extends QuotaValue<U>> SerializableQuota<U> newInstance(Quota<U> quota) { return newInstance(quota.getUsed(), quota.getLimit()); }
private HashMap<String, Object> computeScopes() { HashMap<String, Object> scopes = new HashMap<>(); scopes.put("hasExceededSizeThreshold", sizeThreshold.isPresent()); scopes.put("hasExceededCountThreshold", countThreshold.isPresent()); sizeThreshold.ifPresent(value -> scopes.put("sizeThreshold", value.getQuotaOccupationRatioAsPercent())); countThreshold.ifPresent(value -> scopes.put("countThreshold", value.getQuotaOccupationRatioAsPercent())); scopes.put("usedSize", SizeFormat.format(sizeQuota.getUsed().asLong())); scopes.put("hasSizeLimit", sizeQuota.getLimit().isLimited()); if (sizeQuota.getLimit().isLimited()) { scopes.put("limitSize", SizeFormat.format(sizeQuota.getLimit().asLong())); } scopes.put("usedCount", countQuota.getUsed().asLong()); scopes.put("hasCountLimit", countQuota.getLimit().isLimited()); if (countQuota.getLimit().isLimited()) { scopes.put("limitCount", sizeQuota.getLimit().asLong()); } return scopes; }
private <T extends QuotaValue<T>> Quotas.Value<T> quotaToValue(Quota<T> quota) { return new Quotas.Value<>( quotaValueToNumber(quota.getUsed()), quotaValueToOptionalNumber(quota.getLimit())); }
private void writeMessagesCount(ImapResponseComposer composer, Quota<?> quota) throws IOException { composer.message(quota.getUsed().asLong()); composer.message(quota.getLimit().asLong()); }
private void writeMessagesSize(ImapResponseComposer composer, Quota<?> quota) throws IOException { composer.message(quota.getUsed().asLong() / 1024); composer.message(quota.getLimit().asLong() / 1024); }
private void trySizeAddition(long size) throws OverQuotaException { Quota<QuotaSize> afterAdditionQuotaSize = sizeQuota.addValueToQuota(QuotaSize.size(size)); if (afterAdditionQuotaSize.isOverQuota()) { throw new OverQuotaException( "You use too much space in " + quotaRoot.getValue(), afterAdditionQuotaSize.getLimit(), afterAdditionQuotaSize.getUsed()); } }
private void tryCountAddition(long count) throws OverQuotaException { Quota<QuotaCount> afterAdditionQuotaCount = messageQuota.addValueToQuota(QuotaCount.count(count)); if (afterAdditionQuotaCount.isOverQuota()) { throw new OverQuotaException( "You have too many messages in " + quotaRoot.getValue(), messageQuota.getLimit(), messageQuota.getUsed()); } }
public boolean isExceeded(Quota<?> quota) { if (quota.getLimit().isUnlimited()) { return false; } double used = toDouble(quota.getUsed().asLong()); double limit = toDouble(quota.getLimit().asLong()); double ratio = (used / limit); return ratio > quotaOccupationRatio; }
@Test public void deletingAMailboxShouldDecreaseCurrentQuota() throws Exception { resources.fillMailbox(); mailboxManager.deleteMailbox(inbox, session); QuotaRoot quotaRoot = quotaRootResolver.getQuotaRoot(inbox); Quota<QuotaCount> messageQuota = resources.getQuotaManager().getMessageQuota(quotaRoot); Quota<QuotaSize> storageQuota = resources.getQuotaManager().getStorageQuota(quotaRoot); SoftAssertions.assertSoftly(softly -> { softly.assertThat(messageQuota.getUsed()).isEqualTo(QuotaCount.count(0)); softly.assertThat(storageQuota.getUsed()).isEqualTo(QuotaSize.size(0)); }); }
@Test public void deletingAMailboxShouldPreserveQuotaOfOtherMailboxes() throws Exception { resources.fillMailbox(); mailboxManager.getMailbox(subFolder, session) .appendMessage(MessageManager.AppendCommand.from( Message.Builder.of() .setSubject("test") .setBody("testmail", StandardCharsets.UTF_8) .build()), session); mailboxManager.deleteMailbox(subFolder, session); QuotaRoot quotaRoot = quotaRootResolver.getQuotaRoot(inbox); Quota<QuotaCount> messageQuota = resources.getQuotaManager().getMessageQuota(quotaRoot); Quota<QuotaSize> storageQuota = resources.getQuotaManager().getStorageQuota(quotaRoot); SoftAssertions.assertSoftly(softly -> { softly.assertThat(messageQuota.getUsed()).isEqualTo(QuotaCount.count(16)); softly.assertThat(storageQuota.getUsed()).isEqualTo(QuotaSize.size(16 * 247)); }); } }