@Override public MessageUid next() { if (hasNext()) { return MessageUid.of(current++); } else { throw new NoSuchElementException("Max uid of " + to + " was reached before"); } }
@Test public void givenSomeNumbersToRangeShouldReturnThreeRanges() { List<MessageRange> ranges = MessageRange.toRanges( Arrays.asList( MessageUid.of(1L), MessageUid.of(2L), MessageUid.of(3L), MessageUid.of(5L), MessageUid.of(6L), MessageUid.of(9L))); assertThat(ranges).containsExactly( MessageRange.range(MessageUid.of(1), MessageUid.of(3)), MessageRange.range(MessageUid.of(5), MessageUid.of(6)), MessageUid.of(9).toRange()); }
public MessageUid previous() { if (this.compareTo(MIN_VALUE) > 0) { return new MessageUid(uid - 1); } return MIN_VALUE; }
/** * Tries to split the given {@link MessageRange} to a {@link List} of {@link MessageRange}'s which * select only a max amount of items. This only work for {@link MessageRange}'s with {@link Type} of * {@link Type#RANGE}. */ public List<MessageRange> split(int maxItems) { List<MessageRange> ranges = new ArrayList<>(); if (getType() == Type.RANGE) { long from = getUidFrom().asLong(); long to = getUidTo().asLong(); long realTo = to; while (from <= realTo) { if (from + maxItems - 1 < realTo) { to = from + maxItems - 1; } else { to = realTo; } if (from == to) { ranges.add(MessageUid.of(from).toRange()); } else { ranges.add(MessageRange.range(MessageUid.of(from), MessageUid.of(to))); } from = to + 1; } } else { ranges.add(this); } return ranges; }
/** * Return a {@link MessageRange} for the given values. If the MessageRange * can not be generated a {@link MailboxException} will get thrown */ protected MessageRange messageRange(SelectedMailbox selected, IdRange range, boolean useUids) throws MessageRangeException { long lowVal = range.getLowVal(); long highVal = range.getHighVal(); if (useUids == false) { return msnRangeToMessageRange(selected, lowVal, highVal); } else { if (selected.existsCount() <= 0) { return null; } // Take care of "*" and "*:*" values by return the last message in // the mailbox. See IMAP-289 MessageUid lastUid = selected.getLastUid().orElse(MessageUid.MIN_VALUE); if (lowVal == Long.MAX_VALUE && highVal == Long.MAX_VALUE) { return MessageRange.one(lastUid); } else if (highVal == Long.MAX_VALUE && lastUid.compareTo(MessageUid.of(lowVal)) < 0) { // Sequence uid ranges which use *:<uid-higher-then-last-uid> // MUST return at least the highest uid in the mailbox // See IMAP-291 return MessageRange.one(lastUid); } return MessageRange.range(MessageUid.of(lowVal), MessageUid.of(highVal)); } }
/** * Constructs an inclusive ranges of messages. The parameters will be * checked and {@link #from(long)} used where appropriate. * * @param from * first message UID * @param to * last message UID */ public static MessageRange range(MessageUid from, MessageUid to) { if (to.equals(MessageUid.MAX_VALUE) || to.compareTo(from) < 0) { return from(from); } else if (from.equals(to)) { // from and to is the same so no need to construct a real range return one(from); } else { return new MessageRange(Type.RANGE, from, to); } }
private CompletableFuture<Optional<MessageUid>> tryUpdateUid(CassandraId mailboxId, Optional<MessageUid> uid) { if (uid.isPresent()) { MessageUid nextUid = uid.get().next(); return executor.executeReturnApplied( updateStatement.bind() .setUUID(MAILBOX_ID, mailboxId.asUuid()) .setLong(CONDITION, uid.get().asLong()) .setLong(NEXT_UID, nextUid.asLong())) .thenApply(success -> successToUid(nextUid, success)); } else { return tryInsert(mailboxId); } }
public boolean isFirst() { return this.equals(MIN_VALUE); }
public UidRange(MessageUid singleVal) { this.range = singleVal.toRange(); }
@Override public MessageResult next() { if (!hasNext()) { throw new NoSuchElementException(); } final MailboxMessage message = next.next(); MessageResult result; try { result = ResultUtils.loadMessageResult(message, group); cursor = result.getUid(); } catch (MailboxException e) { result = new UnloadedMessageResult(message, e); } cursor = cursor.next(); return result; }
int a = 0; for (int i = 0; i < uids.size(); i++) { long u = uids.get(i).asLong(); if (i == 0) { firstUid = u; if (uids.size() == 1) { ranges.add(MessageUid.of(firstUid).toRange()); ranges.add(MessageRange.range(MessageUid.of(firstUid), MessageUid.of(firstUid + a))); a = 0; if (uids.size() <= i + 1) { ranges.add(MessageUid.of(firstUid).toRange()); ranges.add(MessageRange.range(MessageUid.of(firstUid), MessageUid.of(firstUid + a))); break;
/** * Return true if the uid is within the range */ public boolean includes(MessageUid uid) { switch (type) { case ALL: return true; case FROM: if (getUidFrom().compareTo(uid) <= 0) { return true; } return false; case RANGE: if (getUidFrom().compareTo(uid) <= 0 && getUidTo().compareTo(uid) >= 0) { return true; } return false; case ONE: if (getUidFrom().equals(uid)) { return true; } return false; default: return false; } }
@Override public boolean equals(Object obj) { if (obj instanceof SimpleMessageMetaData) { return uid.equals(((SimpleMessageMetaData) obj).getUid()); } return false; }
private MessageRange msnRangeToMessageRange(SelectedMailbox selected, long lowVal, long highVal) throws MessageRangeException { // Take care of "*" and "*:*" values by return the last message in // the mailbox. See IMAP-289 if (lowVal == Long.MAX_VALUE && highVal == Long.MAX_VALUE) { Optional<MessageUid> last = selected.getLastUid(); if (!last.isPresent()) { throw new MessageRangeException("Mailbox is empty"); } return last.get().toRange(); } MessageUid lowUid = msnlowValToUid(selected, lowVal); MessageUid highUid = msnHighValToUid(selected, highVal); return MessageRange.range(lowUid, highUid); }
private boolean isLastUid(MessageUid uid) { Optional<MessageUid> lastUid = getLastUid(); return !lastUid.isPresent() || lastUid.get().compareTo(uid) < 0; }
@Override public Optional<MessageUid> lastUid(MailboxSession session, Mailbox mailbox) throws MailboxException { AtomicLong last = getLast((InMemoryId) mailbox.getMailboxId()); if (last == null) { return Optional.empty(); } return Optional.of(MessageUid.of(last.get())); }