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