1 /****************************************************************
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
18 ****************************************************************/
19
20 package org.apache.james.mime4j.field.address;
21
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.Collection;
25 import java.util.List;
26
27 import org.apache.james.mime4j.codec.EncoderUtil;
28
29 /**
30 * A named group of zero or more mailboxes.
31 */
32 public class Group extends Address {
33
34 private static final long serialVersionUID = 1L;
35
36 private final String name;
37 private final MailboxList mailboxList;
38
39 /**
40 * @param name
41 * The group name.
42 * @param mailboxes
43 * The mailboxes in this group.
44 */
45 public Group(String name, Mailbox... mailboxes) {
46 this(name, new MailboxList(Arrays.asList(mailboxes), true));
47 }
48
49 /**
50 * @param name
51 * The group name.
52 * @param mailboxes
53 * The mailboxes in this group.
54 */
55 public Group(String name, Collection<Mailbox> mailboxes) {
56 this(name, new MailboxList(new ArrayList<Mailbox>(mailboxes), true));
57 }
58
59 /**
60 * @param name
61 * The group name.
62 * @param mailboxes
63 * The mailboxes in this group.
64 */
65 public Group(String name, MailboxList mailboxes) {
66 if (name == null)
67 throw new IllegalArgumentException();
68 if (mailboxes == null)
69 throw new IllegalArgumentException();
70
71 this.name = name;
72 this.mailboxList = mailboxes;
73 }
74
75 /**
76 * Parses the specified raw string into a group address.
77 *
78 * @param rawGroupString
79 * string to parse.
80 * @return a <code>Group</code> object for the specified string.
81 * @throws IllegalArgumentException
82 * if the raw string does not represent a single group address.
83 */
84 public static Group parse(String rawGroupString) {
85 Address address = Address.parse(rawGroupString);
86 if (!(address instanceof Group))
87 throw new IllegalArgumentException("Not a group address");
88
89 return (Group) address;
90 }
91
92 /**
93 * Returns the group name.
94 */
95 public String getName() {
96 return name;
97 }
98
99 /**
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 }