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.dom.address;
021    
022    import java.util.ArrayList;
023    import java.util.Arrays;
024    import java.util.Collection;
025    import java.util.List;
026    
027    /**
028     * A named group of zero or more mailboxes.
029     */
030    public class Group extends Address {
031    
032        private static final long serialVersionUID = 1L;
033    
034        private final String name;
035        private final MailboxList mailboxList;
036    
037        /**
038         * @param name
039         *            The group name.
040         * @param mailboxes
041         *            The mailboxes in this group.
042         */
043        public Group(String name, MailboxList mailboxes) {
044            if (name == null)
045                throw new IllegalArgumentException();
046            if (mailboxes == null)
047                throw new IllegalArgumentException();
048    
049            this.name = name;
050            this.mailboxList = mailboxes;
051        }
052    
053        /**
054         * @param name
055         *            The group name.
056         * @param mailboxes
057         *            The mailboxes in this group.
058         */
059        public Group(String name, Mailbox... mailboxes) {
060            this(name, new MailboxList(Arrays.asList(mailboxes), true));
061        }
062    
063        /**
064         * @param name
065         *            The group name.
066         * @param mailboxes
067         *            The mailboxes in this group.
068         */
069        public Group(String name, Collection<Mailbox> mailboxes) {
070            this(name, new MailboxList(new ArrayList<Mailbox>(mailboxes), true));
071        }
072    
073        /**
074         * Returns the group name.
075         */
076        public String getName() {
077            return name;
078        }
079    
080        /**
081         * Returns the mailboxes in this group.
082         */
083        public MailboxList getMailboxes() {
084            return mailboxList;
085        }
086    
087        @Override
088        protected void doAddMailboxesTo(List<Mailbox> results) {
089            for (Mailbox mailbox : mailboxList) {
090                results.add(mailbox);
091            }
092        }
093    
094        @Override
095        public String toString() {
096            StringBuilder sb = new StringBuilder();
097            sb.append(name);
098            sb.append(':');
099            boolean first = true;
100            for (Mailbox mailbox : mailboxList) {
101                if (first) {
102                    first = false;
103                } else {
104                    sb.append(',');
105                }
106                sb.append(' ');
107                sb.append(mailbox);
108            }
109            sb.append(";");
110            return sb.toString();
111        }
112    
113    }