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 020package org.apache.james.mime4j.dom.address; 021 022import java.io.Serializable; 023import java.util.AbstractList; 024import java.util.ArrayList; 025import java.util.Collections; 026import java.util.List; 027 028/** 029 * An immutable, random-access list of Address objects. 030 */ 031public class AddressList extends AbstractList<Address> implements Serializable { 032 033 private static final long serialVersionUID = 1L; 034 035 private final List<? extends Address> addresses; 036 037 /** 038 * @param addresses 039 * A List that contains only Address objects. 040 * @param dontCopy 041 * true iff it is not possible for the addresses list to be 042 * modified by someone else. 043 */ 044 public AddressList(List<? extends Address> addresses, boolean dontCopy) { 045 if (addresses != null) 046 this.addresses = dontCopy ? addresses : new ArrayList<Address>( 047 addresses); 048 else 049 this.addresses = Collections.emptyList(); 050 } 051 052 /** 053 * The number of elements in this list. 054 */ 055 @Override 056 public int size() { 057 return addresses.size(); 058 } 059 060 /** 061 * Gets an address. 062 */ 063 @Override 064 public Address get(int index) { 065 return addresses.get(index); 066 } 067 068 /** 069 * Returns a flat list of all mailboxes represented in this address list. 070 * Use this if you don't care about grouping. 071 */ 072 public MailboxList flatten() { 073 // in the common case, all addresses are mailboxes 074 boolean groupDetected = false; 075 for (Address addr : addresses) { 076 if (!(addr instanceof Mailbox)) { 077 groupDetected = true; 078 break; 079 } 080 } 081 082 if (!groupDetected) { 083 @SuppressWarnings("unchecked") 084 final List<Mailbox> mailboxes = (List<Mailbox>) addresses; 085 return new MailboxList(mailboxes, true); 086 } 087 088 List<Mailbox> results = new ArrayList<Mailbox>(); 089 for (Address addr : addresses) { 090 addr.addMailboxesTo(results); 091 } 092 093 // copy-on-construct this time, because subclasses 094 // could have held onto a reference to the results 095 return new MailboxList(results, false); 096 } 097 098}