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.util.ArrayList;
023    import java.util.Arrays;
024    import java.util.Collection;
025    import java.util.List;
026    
027    import org.apache.james.mime4j.codec.EncoderUtil;
028    
029    /**
030     * A named group of zero or more mailboxes.
031     */
032    public class Group extends Address {
033    
034        private static final long serialVersionUID = 1L;
035    
036        private final String name;
037        private final MailboxList mailboxList;
038    
039        /**
040         * @param name
041         *            The group name.
042         * @param mailboxes
043         *            The mailboxes in this group.
044         */
045        public Group(String name, Mailbox... mailboxes) {
046            this(name, new MailboxList(Arrays.asList(mailboxes), true));
047        }
048    
049        /**
050         * @param name
051         *            The group name.
052         * @param mailboxes
053         *            The mailboxes in this group.
054         */
055        public Group(String name, Collection<Mailbox> mailboxes) {
056            this(name, new MailboxList(new ArrayList<Mailbox>(mailboxes), true));
057        }
058    
059        /**
060         * @param name
061         *            The group name.
062         * @param mailboxes
063         *            The mailboxes in this group.
064         */
065        public Group(String name, MailboxList mailboxes) {
066            if (name == null)
067                throw new IllegalArgumentException();
068            if (mailboxes == null)
069                throw new IllegalArgumentException();
070    
071            this.name = name;
072            this.mailboxList = mailboxes;
073        }
074    
075        /**
076         * Parses the specified raw string into a group address.
077         * 
078         * @param rawGroupString
079         *            string to parse.
080         * @return a <code>Group</code> object for the specified string.
081         * @throws IllegalArgumentException
082         *             if the raw string does not represent a single group address.
083         */
084        public static Group parse(String rawGroupString) {
085            Address address = Address.parse(rawGroupString);
086            if (!(address instanceof Group))
087                throw new IllegalArgumentException("Not a group address");
088    
089            return (Group) address;
090        }
091    
092        /**
093         * Returns the group name.
094         */
095        public String getName() {
096            return name;
097        }
098    
099        /**
100         * Returns the mailboxes in this group.
101         */
102        public MailboxList getMailboxes() {
103            return mailboxList;
104        }
105    
106        @Override
107        public String getDisplayString(boolean includeRoute) {
108            StringBuilder sb = new StringBuilder();
109    
110            sb.append(name);
111            sb.append(':');
112    
113            boolean first = true;
114            for (Mailbox mailbox : mailboxList) {
115                if (first) {
116                    first = false;
117                } else {
118                    sb.append(',');
119                }
120    
121                sb.append(' ');
122                sb.append(mailbox.getDisplayString(includeRoute));
123            }
124    
125            sb.append(";");
126    
127            return sb.toString();
128        }
129    
130        @Override
131        public String getEncodedString() {
132            StringBuilder sb = new StringBuilder();
133    
134            sb.append(EncoderUtil.encodeAddressDisplayName(name));
135            sb.append(':');
136    
137            boolean first = true;
138            for (Mailbox mailbox : mailboxList) {
139                if (first) {
140                    first = false;
141                } else {
142                    sb.append(',');
143                }
144    
145                sb.append(' ');
146                sb.append(mailbox.getEncodedString());
147            }
148    
149            sb.append(';');
150    
151            return sb.toString();
152        }
153    
154        @Override
155        protected void doAddMailboxesTo(List<Mailbox> results) {
156            for (Mailbox mailbox : mailboxList) {
157                results.add(mailbox);
158            }
159        }
160    
161    }