• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * Represents a single e-mail address.
26  *
27  *
28  */
29 public class Mailbox extends Address {
30 	private DomainList route;
31 	private String localPart;
32 	private String domain;
33 
34 	/**
35 	 * Creates a mailbox without a route. Routes are obsolete.
36 	 * @param localPart The part of the e-mail address to the left of the "@".
37 	 * @param domain The part of the e-mail address to the right of the "@".
38 	 */
Mailbox(String localPart, String domain)39 	public Mailbox(String localPart, String domain) {
40 		this(null, localPart, domain);
41 	}
42 
43 	/**
44 	 * Creates a mailbox with a route. Routes are obsolete.
45 	 * @param route The zero or more domains that make up the route. Can be null.
46 	 * @param localPart The part of the e-mail address to the left of the "@".
47 	 * @param domain The part of the e-mail address to the right of the "@".
48 	 */
Mailbox(DomainList route, String localPart, String domain)49 	public Mailbox(DomainList route, String localPart, String domain) {
50 		this.route = route;
51 		this.localPart = localPart;
52 		this.domain = domain;
53 	}
54 
55 	/**
56 	 * Returns the route list.
57 	 */
getRoute()58 	public DomainList getRoute() {
59 		return route;
60 	}
61 
62 	/**
63 	 * Returns the left part of the e-mail address
64 	 * (before "@").
65 	 */
getLocalPart()66 	public String getLocalPart() {
67 		return localPart;
68 	}
69 
70 	/**
71 	 * Returns the right part of the e-mail address
72 	 * (after "@").
73 	 */
getDomain()74 	public String getDomain() {
75 		return domain;
76 	}
77 
78 	/**
79 	 * Formats the address as a string, not including
80 	 * the route.
81 	 *
82 	 * @see #getAddressString(boolean)
83 	 */
getAddressString()84 	public String getAddressString() {
85 		return getAddressString(false);
86 	}
87 
88 	/**
89 	 * Note that this value may not be usable
90 	 * for transport purposes, only display purposes.
91 	 *
92 	 * For example, if the unparsed address was
93 	 *
94 	 *   <"Joe Cheng"@joecheng.com>
95 	 *
96 	 * this method would return
97 	 *
98 	 *   <Joe Cheng@joecheng.com>
99 	 *
100 	 * which is not valid for transport; the local part
101 	 * would need to be re-quoted.
102 	 *
103 	 * @param includeRoute true if the route should be included if it exists.
104 	 */
getAddressString(boolean includeRoute)105 	public String getAddressString(boolean includeRoute) {
106 		return "<" + (!includeRoute || route == null ? "" : route.toRouteString() + ":")
107 			+ localPart
108 			+ (domain == null ? "" : "@")
109 			+ domain + ">";
110 	}
111 
112 	@Override
doAddMailboxesTo(ArrayList<Address> results)113 	protected final void doAddMailboxesTo(ArrayList<Address> results) {
114 		results.add(this);
115 	}
116 
117 	@Override
toString()118 	public String toString() {
119 		return getAddressString();
120 	}
121 }
122