| 1 | /*- |
|---|
| 2 | * Copyright (c) 2009, Derek Konigsberg |
|---|
| 3 | * All rights reserved. |
|---|
| 4 | * |
|---|
| 5 | * Redistribution and use in source and binary forms, with or without |
|---|
| 6 | * modification, are permitted provided that the following conditions |
|---|
| 7 | * are met: |
|---|
| 8 | * |
|---|
| 9 | * 1. Redistributions of source code must retain the above copyright |
|---|
| 10 | * notice, this list of conditions and the following disclaimer. |
|---|
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the |
|---|
| 13 | * documentation and/or other materials provided with the distribution. |
|---|
| 14 | * 3. Neither the name of the project nor the names of its |
|---|
| 15 | * contributors may be used to endorse or promote products derived |
|---|
| 16 | * from this software without specific prior written permission. |
|---|
| 17 | * |
|---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
|---|
| 21 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
|---|
| 22 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
|---|
| 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
|---|
| 24 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|---|
| 25 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|---|
| 26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
|---|
| 27 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|---|
| 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
|---|
| 29 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 30 | */ |
|---|
| 31 | package org.logicprobe.LogicMail.model; |
|---|
| 32 | |
|---|
| 33 | import org.logicprobe.LogicMail.util.StringParser; |
|---|
| 34 | |
|---|
| 35 | /** |
|---|
| 36 | * Represents an address in an E-Mail message. |
|---|
| 37 | */ |
|---|
| 38 | public class Address { |
|---|
| 39 | private final String address; |
|---|
| 40 | private final String name; |
|---|
| 41 | |
|---|
| 42 | /** |
|---|
| 43 | * Instantiates a new address. |
|---|
| 44 | * |
|---|
| 45 | * @param address Address string, such as "doej@generic.org". |
|---|
| 46 | * @param name Address display name, such as "John Doe". |
|---|
| 47 | */ |
|---|
| 48 | public Address(String address, String name) { |
|---|
| 49 | //TODO: Add validation and/or cleanup of input strings |
|---|
| 50 | this.address = address; |
|---|
| 51 | this.name = name; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | /** |
|---|
| 55 | * Instantiates a new address |
|---|
| 56 | * |
|---|
| 57 | * @param completeAddress Complete address string, such as "John Doe <doej@generic.org>". |
|---|
| 58 | */ |
|---|
| 59 | public Address(String completeAddress) { |
|---|
| 60 | String[] recipient = StringParser.parseRecipient(completeAddress); |
|---|
| 61 | this.name = recipient[0]; |
|---|
| 62 | this.address = recipient[1]; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | /** |
|---|
| 66 | * Gets the address string. |
|---|
| 67 | * |
|---|
| 68 | * @return the address |
|---|
| 69 | */ |
|---|
| 70 | public String getAddress() { |
|---|
| 71 | return this.address; |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | /** |
|---|
| 75 | * Gets the display name. |
|---|
| 76 | * |
|---|
| 77 | * @return the name |
|---|
| 78 | */ |
|---|
| 79 | public String getName() { |
|---|
| 80 | return this.name; |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | /* (non-Javadoc) |
|---|
| 84 | * @see java.lang.Object#toString() |
|---|
| 85 | */ |
|---|
| 86 | public String toString() { |
|---|
| 87 | String result; |
|---|
| 88 | if(this.name == null) { |
|---|
| 89 | result = this.address; |
|---|
| 90 | } |
|---|
| 91 | else { |
|---|
| 92 | StringBuffer buf = new StringBuffer(); |
|---|
| 93 | buf.append('\"'); |
|---|
| 94 | buf.append(this.name); |
|---|
| 95 | buf.append("\" <"); |
|---|
| 96 | buf.append(this.address); |
|---|
| 97 | buf.append('>'); |
|---|
| 98 | result = buf.toString(); |
|---|
| 99 | } |
|---|
| 100 | return result; |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | /* (non-Javadoc) |
|---|
| 104 | * @see java.lang.Object#hashCode() |
|---|
| 105 | */ |
|---|
| 106 | public int hashCode() { |
|---|
| 107 | int result = 1; |
|---|
| 108 | result = 31 * result + ((address == null) ? 0 : address.hashCode()); |
|---|
| 109 | result = 31 * result + ((name == null) ? 0 : name.hashCode()); |
|---|
| 110 | return result; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | /* (non-Javadoc) |
|---|
| 114 | * @see java.lang.Object#equals(java.lang.Object) |
|---|
| 115 | */ |
|---|
| 116 | public boolean equals(Object obj) { |
|---|
| 117 | if (this == obj) { |
|---|
| 118 | return true; |
|---|
| 119 | } |
|---|
| 120 | if (obj == null) { |
|---|
| 121 | return false; |
|---|
| 122 | } |
|---|
| 123 | if (getClass() != obj.getClass()) { |
|---|
| 124 | return false; |
|---|
| 125 | } |
|---|
| 126 | Address other = (Address) obj; |
|---|
| 127 | if (address == null) { |
|---|
| 128 | if (other.address != null) { |
|---|
| 129 | return false; |
|---|
| 130 | } |
|---|
| 131 | } else if (!address.equals(other.address)) { |
|---|
| 132 | return false; |
|---|
| 133 | } |
|---|
| 134 | if (name == null) { |
|---|
| 135 | if (other.name != null) { |
|---|
| 136 | return false; |
|---|
| 137 | } |
|---|
| 138 | } else if (!name.equals(other.name)) { |
|---|
| 139 | return false; |
|---|
| 140 | } |
|---|
| 141 | return true; |
|---|
| 142 | } |
|---|
| 143 | } |
|---|