source: contrib/MailArchiver/sources/vendor/mime4j/apache-mime4j-0.7-SNAPSHOT-20110327.010440-17/dom/src/main/java/org/apache/james/mime4j/dom/address/Mailbox.java @ 6785

Revision 6785, 6.8 KB checked in by rafaelraymundo, 12 years ago (diff)

Ticket #2946 - Liberado codigo do MailArchiver?. Documentação na subpasta DOCS.

Line 
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
20package org.apache.james.mime4j.dom.address;
21
22import java.util.Collections;
23import java.util.List;
24import java.util.Locale;
25
26import org.apache.james.mime4j.util.LangUtils;
27
28/**
29 * Represents a single e-mail address.
30 */
31public class Mailbox extends Address {
32
33    private static final long serialVersionUID = 1L;
34
35    private static final DomainList EMPTY_ROUTE_LIST = new DomainList(
36            Collections.<String> emptyList(), true);
37
38    private final String name;
39    private final DomainList route;
40    private final String localPart;
41    private final String domain;
42
43    /**
44     * Creates a named mailbox with a route. Routes are obsolete.
45     *
46     * @param name
47     *            the name of the e-mail address. May be <code>null</code>.
48     * @param route
49     *            The zero or more domains that make up the route. May be
50     *            <code>null</code>.
51     * @param localPart
52     *            The part of the e-mail address to the left of the "@".
53     * @param domain
54     *            The part of the e-mail address to the right of the "@".
55     */
56    public Mailbox(String name, DomainList route, String localPart,
57            String domain) {
58        if (localPart == null || localPart.length() == 0)
59            throw new IllegalArgumentException();
60
61        this.name = name == null || name.length() == 0 ? null : name;
62        this.route = route == null ? EMPTY_ROUTE_LIST : route;
63        this.localPart = localPart;
64        this.domain = domain == null || domain.length() == 0 ? null : domain;
65    }
66
67    /**
68     * Creates a named mailbox based on an unnamed mailbox. Package private;
69     * internally used by Builder.
70     */
71    Mailbox(String name, Mailbox baseMailbox) {
72        this(name, baseMailbox.getRoute(), baseMailbox.getLocalPart(),
73                baseMailbox.getDomain());
74    }
75
76    /**
77     * Creates an unnamed mailbox without a route. Routes are obsolete.
78     *
79     * @param localPart
80     *            The part of the e-mail address to the left of the "@".
81     * @param domain
82     *            The part of the e-mail address to the right of the "@".
83     */
84    public Mailbox(String localPart, String domain) {
85        this(null, null, localPart, domain);
86    }
87
88    /**
89     * Creates an unnamed mailbox with a route. Routes are obsolete.
90     *
91     * @param route
92     *            The zero or more domains that make up the route. May be
93     *            <code>null</code>.
94     * @param localPart
95     *            The part of the e-mail address to the left of the "@".
96     * @param domain
97     *            The part of the e-mail address to the right of the "@".
98     */
99    public Mailbox(DomainList route, String localPart, String domain) {
100        this(null, route, localPart, domain);
101    }
102
103    /**
104     * Creates a named mailbox without a route. Routes are obsolete.
105     *
106     * @param name
107     *            the name of the e-mail address. May be <code>null</code>.
108     * @param localPart
109     *            The part of the e-mail address to the left of the "@".
110     * @param domain
111     *            The part of the e-mail address to the right of the "@".
112     */
113    public Mailbox(String name, String localPart, String domain) {
114        this(name, null, localPart, domain);
115    }
116
117    /**
118     * Returns the name of the mailbox or <code>null</code> if it does not
119     * have a name.
120     */
121    public String getName() {
122        return name;
123    }
124
125    /**
126     * Returns the route list. If the mailbox does not have a route an empty
127     * domain list is returned.
128     */
129    public DomainList getRoute() {
130        return route;
131    }
132
133    /**
134     * Returns the left part of the e-mail address (before "@").
135     */
136    public String getLocalPart() {
137        return localPart;
138    }
139
140    /**
141     * Returns the right part of the e-mail address (after "@").
142     */
143    public String getDomain() {
144        return domain;
145    }
146
147    /**
148     * Returns the address in the form <i>localPart@domain</i>.
149     *
150     * @return the address part of this mailbox.
151     */
152    public String getAddress() {
153        if (domain == null) {
154            return localPart;
155        } else {
156            return localPart + '@' + domain;
157        }
158    }
159
160    @Override
161    protected final void doAddMailboxesTo(List<Mailbox> results) {
162        results.add(this);
163    }
164
165    @Override
166    public int hashCode() {
167        int hash = LangUtils.HASH_SEED;
168        hash = LangUtils.hashCode(hash, this.localPart);
169        hash = LangUtils.hashCode(hash, this.domain != null ?
170                this.domain.toLowerCase(Locale.US) : null);
171        return hash;
172    }
173
174    /**
175     * Indicates whether some other object is "equal to" this mailbox.
176     * <p>
177     * An object is considered to be equal to this mailbox if it is an instance
178     * of class <code>Mailbox</code> that holds the same address as this one.
179     * The domain is considered to be case-insensitive but the local-part is not
180     * (because of RFC 5321: <cite>the local-part of a mailbox MUST BE treated
181     * as case sensitive</cite>).
182     *
183     * @param obj
184     *            the object to test for equality.
185     * @return <code>true</code> if the specified object is a
186     *         <code>Mailbox</code> that holds the same address as this one.
187     */
188    @Override
189    public boolean equals(Object obj) {
190        if (obj == this)
191            return true;
192        if (!(obj instanceof Mailbox))
193            return false;
194        Mailbox that = (Mailbox) obj;
195        return LangUtils.equals(this.localPart, that.localPart) &&
196            LangUtils.equalsIgnoreCase(this.domain, that.domain);
197    }
198
199    @Override
200    public String toString() {
201        return getAddress();
202    }
203
204}
Note: See TracBrowser for help on using the repository browser.