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 Strings (that are supposedly domain names
030 * or domain literals).
031 */
032public class DomainList extends AbstractList<String> implements Serializable {
033
034    private static final long serialVersionUID = 1L;
035
036    private final List<String> domains;
037
038    /**
039     * @param domains
040     *            A List that contains only String objects.
041     * @param dontCopy
042     *            true iff it is not possible for the domains list to be
043     *            modified by someone else.
044     */
045    public DomainList(List<String> domains, boolean dontCopy) {
046        if (domains != null)
047            this.domains = dontCopy ? domains : new ArrayList<String>(domains);
048        else
049            this.domains = Collections.emptyList();
050    }
051
052    /**
053     * The number of elements in this list.
054     */
055    @Override
056    public int size() {
057        return domains.size();
058    }
059
060    /**
061     * Gets the domain name or domain literal at the specified index.
062     *
063     * @throws IndexOutOfBoundsException
064     *             If index is &lt; 0 or &gt;= size().
065     */
066    @Override
067    public String get(int index) {
068        return domains.get(index);
069    }
070
071    /**
072     * Returns the list of domains formatted as a route string (not including
073     * the trailing ':').
074     */
075    public String toRouteString() {
076        StringBuilder sb = new StringBuilder();
077
078        for (String domain : domains) {
079            if (sb.length() > 0) {
080                sb.append(',');
081            }
082
083            sb.append("@");
084            sb.append(domain);
085        }
086
087        return sb.toString();
088    }
089
090    @Override
091    public String toString() {
092        return toRouteString();
093    }
094
095}