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 org.apache.james.mime4j.field.address.parser.AddressListParser;
23 import org.apache.james.mime4j.field.address.parser.ParseException;
24
25 import java.io.Serializable;
26 import java.io.StringReader;
27 import java.util.AbstractList;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.List;
31
32 /**
33 * An immutable, random-access list of Address objects.
34 */
35 public class AddressList extends AbstractList<Address> implements Serializable {
36
37 private static final long serialVersionUID = 1L;
38
39 private final List<? extends Address> addresses;
40
41 /**
42 * @param addresses
43 * A List that contains only Address objects.
44 * @param dontCopy
45 * true iff it is not possible for the addresses list to be
46 * modified by someone else.
47 */
48 public AddressList(List<? extends Address> addresses, boolean dontCopy) {
49 if (addresses != null)
50 this.addresses = dontCopy ? addresses : new ArrayList<Address>(
51 addresses);
52 else
53 this.addresses = Collections.emptyList();
54 }
55
56 /**
57 * The number of elements in this list.
58 */
59 @Override
60 public int size() {
61 return addresses.size();
62 }
63
64 /**
65 * Gets an address.
66 */
67 @Override
68 public Address get(int index) {
69 return addresses.get(index);
70 }
71
72 /**
73 * Returns a flat list of all mailboxes represented in this address list.
74 * Use this if you don't care about grouping.
75 */
76 public MailboxList flatten() {
77 // in the common case, all addresses are mailboxes
78 boolean groupDetected = false;
79 for (Address addr : addresses) {
80 if (!(addr instanceof Mailbox)) {
81 groupDetected = true;
82 break;
83 }
84 }
85
86 if (!groupDetected) {
87 @SuppressWarnings("unchecked")
88 final List<Mailbox> mailboxes = (List<Mailbox>) addresses;
89 return new MailboxList(mailboxes, true);
90 }
91
92 List<Mailbox> results = new ArrayList<Mailbox>();
93 for (Address addr : addresses) {
94 addr.addMailboxesTo(results);
95 }
96
97 // copy-on-construct this time, because subclasses
98 // could have held onto a reference to the results
99 return new MailboxList(results, false);
100 }
101
102 /**
103 * Dumps a representation of this address list to stdout, for debugging
104 * purposes.
105 */
106 public void print() {
107 for (Address addr : addresses) {
108 System.out.println(addr.toString());
109 }
110 }
111
112 /**
113 * Parse the address list string, such as the value of a From, To, Cc, Bcc,
114 * Sender, or Reply-To header.
115 *
116 * The string MUST be unfolded already.
117 */
118 public static AddressList parse(String rawAddressList)
119 throws ParseException {
120 AddressListParser parser = new AddressListParser(new StringReader(
121 rawAddressList));
122 return Builder.getInstance().buildAddressList(parser.parseAddressList());
123 }
124
125 /**
126 * Test console.
127 */
128 public static void main(String[] args) throws Exception {
129 java.io.BufferedReader reader = new java.io.BufferedReader(
130 new java.io.InputStreamReader(System.in));
131 while (true) {
132 try {
133 System.out.print("> ");
134 String line = reader.readLine();
135 if (line.length() == 0 || line.toLowerCase().equals("exit")
136 || line.toLowerCase().equals("quit")) {
137 System.out.println("Goodbye.");
138 return;
139 }
140 AddressList list = parse(line);
141 list.print();
142 } catch (Exception e) {
143 e.printStackTrace();
144 Thread.sleep(300);
145 }
146 }
147 }
148 }