[6785] | 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.dom.address; |
---|
| 21 | |
---|
| 22 | import java.io.Serializable; |
---|
| 23 | import java.util.AbstractList; |
---|
| 24 | import java.util.ArrayList; |
---|
| 25 | import java.util.Collections; |
---|
| 26 | import java.util.List; |
---|
| 27 | |
---|
| 28 | /** |
---|
| 29 | * An immutable, random-access list of Strings (that are supposedly domain names |
---|
| 30 | * or domain literals). |
---|
| 31 | */ |
---|
| 32 | public class DomainList extends AbstractList<String> implements Serializable { |
---|
| 33 | |
---|
| 34 | private static final long serialVersionUID = 1L; |
---|
| 35 | |
---|
| 36 | private final List<String> domains; |
---|
| 37 | |
---|
| 38 | /** |
---|
| 39 | * @param domains |
---|
| 40 | * A List that contains only String objects. |
---|
| 41 | * @param dontCopy |
---|
| 42 | * true iff it is not possible for the domains list to be |
---|
| 43 | * modified by someone else. |
---|
| 44 | */ |
---|
| 45 | public DomainList(List<String> domains, boolean dontCopy) { |
---|
| 46 | if (domains != null) |
---|
| 47 | this.domains = dontCopy ? domains : new ArrayList<String>(domains); |
---|
| 48 | else |
---|
| 49 | this.domains = Collections.emptyList(); |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | /** |
---|
| 53 | * The number of elements in this list. |
---|
| 54 | */ |
---|
| 55 | @Override |
---|
| 56 | public int size() { |
---|
| 57 | return domains.size(); |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | /** |
---|
| 61 | * Gets the domain name or domain literal at the specified index. |
---|
| 62 | * |
---|
| 63 | * @throws IndexOutOfBoundsException |
---|
| 64 | * If index is < 0 or >= size(). |
---|
| 65 | */ |
---|
| 66 | @Override |
---|
| 67 | public String get(int index) { |
---|
| 68 | return domains.get(index); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | /** |
---|
| 72 | * Returns the list of domains formatted as a route string (not including |
---|
| 73 | * the trailing ':'). |
---|
| 74 | */ |
---|
| 75 | public String toRouteString() { |
---|
| 76 | StringBuilder sb = new StringBuilder(); |
---|
| 77 | |
---|
| 78 | for (String domain : domains) { |
---|
| 79 | if (sb.length() > 0) { |
---|
| 80 | sb.append(','); |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | sb.append("@"); |
---|
| 84 | sb.append(domain); |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | return sb.toString(); |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | @Override |
---|
| 91 | public String toString() { |
---|
| 92 | return toRouteString(); |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | } |
---|