Home | History | Annotate | Download | only in address
      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 
     24 /**
     25  * An immutable, random-access list of Strings (that
     26  * are supposedly domain names or domain literals).
     27  *
     28  *
     29  */
     30 public class DomainList {
     31 	private ArrayList<String> domains;
     32 
     33 	/**
     34 	 * @param domains An ArrayList that contains only String objects.
     35 	 * @param dontCopy true iff it is not possible for the domains ArrayList to be modified by someone else.
     36 	 */
     37 	public DomainList(ArrayList<String> domains, boolean dontCopy) {
     38 		if (domains != null)
     39 			this.domains = (dontCopy ? domains : new ArrayList<String>(domains));
     40 		else
     41 			this.domains = new ArrayList<String>(0);
     42 	}
     43 
     44 	/**
     45 	 * The number of elements in this list.
     46 	 */
     47 	public int size() {
     48 		return domains.size();
     49 	}
     50 
     51 	/**
     52 	 * Gets the domain name or domain literal at the
     53 	 * specified index.
     54 	 * @throws IndexOutOfBoundsException If index is &lt; 0 or &gt;= size().
     55 	 */
     56 	public String get(int index) {
     57 		if (0 > index || size() <= index)
     58 			throw new IndexOutOfBoundsException();
     59 		return domains.get(index);
     60 	}
     61 
     62 	/**
     63 	 * Returns the list of domains formatted as a route
     64 	 * string (not including the trailing ':').
     65 	 */
     66 	public String toRouteString() {
     67 		StringBuffer out = new StringBuffer();
     68 		for (int i = 0; i < domains.size(); i++) {
     69 			out.append("@");
     70 			out.append(get(i));
     71 			if (i + 1 < domains.size())
     72 				out.append(",");
     73 		}
     74 		return out.toString();
     75 	}
     76 }
     77