All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.icegreen.greenmail.store.ListBasedStoredMessageCollection Maven / Gradle / Ivy

There is a newer version: 2.1.2
Show newest version
/* -------------------------------------------------------------------
 * This software is released under the Apache license 2.0
 * -------------------------------------------------------------------
 */
package com.icegreen.greenmail.store;

import com.icegreen.greenmail.foedus.util.MsgRangeFilter;
import com.icegreen.greenmail.imap.commands.IdRange;
import jakarta.mail.Flags;

import java.util.*;

/**
 * @author Raimund Klein 
 */
public class ListBasedStoredMessageCollection implements StoredMessageCollection {
    private final List mailMessages = Collections.synchronizedList(new ArrayList<>());

    @Override
    public int size() {
        return mailMessages.size();
    }

    @Override
    public void add(StoredMessage storedMessage) {
        mailMessages.add(storedMessage);
    }

    @Override
    public StoredMessage remove(long uid) {
        synchronized (mailMessages) {
            for (int i = 0; i < mailMessages.size(); i++) {
                StoredMessage message = mailMessages.get(i);
                if (message.getUid() == uid) {
                    mailMessages.remove(i);
                    return message;
                }
            }
        }
        throw new IllegalArgumentException("No message for uid " + uid + " exists");
    }

    @Override
    public void clear() {
        mailMessages.clear();
    }

    private void expungeMessage(int msn, Collection mailboxListeners) {
        // Notify all the listeners of the pending delete
        synchronized (mailboxListeners) {
            deleteMessage(msn);
            for (FolderListener expungeListener : mailboxListeners) {
                expungeListener.expunged(msn);
            }
        }
    }

    private void deleteMessage(int msn) {
        mailMessages.remove(msn - 1); // input is 1 based index
    }

    @Override
    public int getFirstUnseen() {
        synchronized (mailMessages) {
            for (int i = 0; i < mailMessages.size(); i++) {
                StoredMessage message = mailMessages.get(i);
                if (!message.isSet(Flags.Flag.SEEN)) {
                    return i + 1;
                }
            }
        }
        return -1;
    }

    @Override
    public int getMsn(long uid) throws FolderException {
        synchronized (mailMessages) {
            for (int i = 0; i < mailMessages.size(); i++) {
                StoredMessage message = mailMessages.get(i);
                if (message.getUid() == uid) {
                    return i + 1;
                }
            }
        }
        throw new FolderException("No such message of uid " + uid + ".");
    }

    @Override
    public List getMessages(MsgRangeFilter range) {
        List ret = new ArrayList<>();

        synchronized (mailMessages) {
            for (int i = 0; i < mailMessages.size(); i++) {
                if (range.includes(i + 1)) {
                    ret.add(mailMessages.get(i));
                }
            }
        }

        return ret;
    }

    @Override
    public List getMessages() {
        return Collections.unmodifiableList(mailMessages);
    }

    @Override
    public long[] getMessageUids() {
        synchronized (mailMessages) {
            long[] uids = new long[mailMessages.size()];
            for (int i = 0; i < mailMessages.size(); i++) {
                StoredMessage message = mailMessages.get(i);
                uids[i] = message.getUid();
            }
            return uids;
        }
    }

    @Override
    public void expunge(List folderListeners) {
        expunge(folderListeners, null);
    }

    @Override
    public void expunge(List folderListeners, IdRange[] idRanges) {
        synchronized (mailMessages) {
            for (int i = mailMessages.size() - 1; i >= 0; i--) {
                StoredMessage message = mailMessages.get(i);
                if (message.isSet(Flags.Flag.DELETED) &&
                    (idRanges == null || IdRange.containsUid(idRanges, message.getUid()))) {
                    expungeMessage(i + 1, folderListeners); // MSNs start counting at 1
                }
            }
        }
    }

    @Override
    public StoredMessage get(int i) {
        return mailMessages.get(i);
    }

    @Override
    public Iterator iterator() {
        return mailMessages.iterator();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy