001    /****************************************************************
002     * Licensed to the Apache Software Foundation (ASF) under one   *
003     * or more contributor license agreements.  See the NOTICE file *
004     * distributed with this work for additional information        *
005     * regarding copyright ownership.  The ASF licenses this file   *
006     * to you under the Apache License, Version 2.0 (the            *
007     * "License"); you may not use this file except in compliance   *
008     * with the License.  You may obtain a copy of the License at   *
009     *                                                              *
010     *   http://www.apache.org/licenses/LICENSE-2.0                 *
011     *                                                              *
012     * Unless required by applicable law or agreed to in writing,   *
013     * software distributed under the License is distributed on an  *
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
015     * KIND, either express or implied.  See the License for the    *
016     * specific language governing permissions and limitations      *
017     * under the License.                                           *
018     ****************************************************************/
019    
020    package org.apache.james.mime4j.field.address;
021    
022    import java.io.Serializable;
023    import java.util.AbstractList;
024    import java.util.ArrayList;
025    import java.util.Collections;
026    import java.util.List;
027    
028    /**
029     * An immutable, random-access list of Mailbox objects.
030     */
031    public class MailboxList extends AbstractList<Mailbox> implements Serializable {
032    
033        private static final long serialVersionUID = 1L;
034    
035        private final List<Mailbox> mailboxes;
036    
037        /**
038         * @param mailboxes
039         *            A List that contains only Mailbox objects.
040         * @param dontCopy
041         *            true iff it is not possible for the mailboxes list to be
042         *            modified by someone else.
043         */
044        public MailboxList(List<Mailbox> mailboxes, boolean dontCopy) {
045            if (mailboxes != null)
046                this.mailboxes = dontCopy ? mailboxes : new ArrayList<Mailbox>(
047                        mailboxes);
048            else
049                this.mailboxes = Collections.emptyList();
050        }
051    
052        /**
053         * The number of elements in this list.
054         */
055        @Override
056        public int size() {
057            return mailboxes.size();
058        }
059    
060        /**
061         * Gets an address.
062         */
063        @Override
064        public Mailbox get(int index) {
065            return mailboxes.get(index);
066        }
067    
068        /**
069         * Dumps a representation of this mailbox list to stdout, for debugging
070         * purposes.
071         */
072        public void print() {
073            for (int i = 0; i < size(); i++) {
074                Mailbox mailbox = get(i);
075                System.out.println(mailbox.toString());
076            }
077        }
078    
079    }