| 1 | package ezvcard.property; |
| 2 | |
| 3 | import java.util.List; |
| 4 | |
| 5 | import ezvcard.VCard; |
| 6 | import ezvcard.VCardVersion; |
| 7 | import ezvcard.ValidationWarning; |
| 8 | import ezvcard.parameter.EmailType; |
| 9 | import ezvcard.parameter.Pid; |
| 10 | |
| 11 | /* |
| 12 | Copyright (c) 2012-2023, Michael Angstadt |
| 13 | All rights reserved. |
| 14 | |
| 15 | Redistribution and use in source and binary forms, with or without |
| 16 | modification, are permitted provided that the following conditions are met: |
| 17 | |
| 18 | 1. Redistributions of source code must retain the above copyright notice, this |
| 19 | list of conditions and the following disclaimer. |
| 20 | 2. Redistributions in binary form must reproduce the above copyright notice, |
| 21 | this list of conditions and the following disclaimer in the documentation |
| 22 | and/or other materials provided with the distribution. |
| 23 | |
| 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 25 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 26 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 27 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 28 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 29 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 30 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 31 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 32 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 33 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 34 | |
| 35 | The views and conclusions contained in the software and documentation are those |
| 36 | of the authors and should not be interpreted as representing official policies, |
| 37 | either expressed or implied, of the FreeBSD Project. |
| 38 | */ |
| 39 | |
| 40 | /** |
| 41 | * <p> |
| 42 | * Defines an email address associated with the person. |
| 43 | * </p> |
| 44 | * |
| 45 | * <p> |
| 46 | * <b>Code sample</b> |
| 47 | * </p> |
| 48 | * |
| 49 | * <pre class="brush:java"> |
| 50 | * VCard vcard = new VCard(); |
| 51 | * |
| 52 | * Email email = new Email("[email protected]"); |
| 53 | * email.getTypes().add(EmailType.HOME); |
| 54 | * vcard.addEmail(email); |
| 55 | * |
| 56 | * email = new Email("[email protected]"); |
| 57 | * email.getTypes().add(EmailType.WORK); |
| 58 | * email.setPref(1); //the most preferred email |
| 59 | * vcard.addEmail(email); |
| 60 | * </pre> |
| 61 | * |
| 62 | * <p> |
| 63 | * <b>Property name:</b> {@code EMAIL} |
| 64 | * </p> |
| 65 | * <p> |
| 66 | * <b>Supported versions:</b> {@code 2.1, 3.0, 4.0} |
| 67 | * </p> |
| 68 | * @author Michael Angstadt |
| 69 | * @see <a href="http://tools.ietf.org/html/rfc6350#page-36">RFC 6350 p.36</a> |
| 70 | * @see <a href="http://tools.ietf.org/html/rfc2426#page-15">RFC 2426 p.15</a> |
| 71 | * @see <a href="http://www.imc.org/pdi/vcard-21.doc">vCard 2.1 p.15</a> |
| 72 | */ |
| 73 | public class Email extends TextProperty implements HasAltId { |
| 74 | /** |
| 75 | * Creates an email property. |
| 76 | * @param email the email (e.g. "[email protected]") |
| 77 | */ |
| 78 | public Email(String email) { |
| 79 | super(email); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Copy constructor. |
| 84 | * @param original the property to make a copy of |
| 85 | */ |
| 86 | public Email(Email original) { |
| 87 | super(original); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Gets the list that stores this property's email types (TYPE parameters). |
| 92 | * @return the email types (this list is mutable) (e.g. "INTERNET", "WORK") |
| 93 | */ |
| 94 | public List<EmailType> getTypes() { |
| 95 | return parameters.new TypeParameterList<EmailType>() { |
| 96 | @Override |
| 97 | protected EmailType _asObject(String value) { |
| 98 | return EmailType.get(value); |
| 99 | } |
| 100 | }; |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public List<Pid> getPids() { |
| 105 | return super.getPids(); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public Integer getPref() { |
| 110 | return super.getPref(); |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public void setPref(Integer pref) { |
| 115 | super.setPref(pref); |
| 116 | } |
| 117 | |
| 118 | //@Override |
| 119 | public String getAltId() { |
| 120 | return parameters.getAltId(); |
| 121 | } |
| 122 | |
| 123 | //@Override |
| 124 | public void setAltId(String altId) { |
| 125 | parameters.setAltId(altId); |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | protected void _validate(List<ValidationWarning> warnings, VCardVersion version, VCard vcard) { |
| 130 | super._validate(warnings, version, vcard); |
| 131 | |
| 132 | for (EmailType type : getTypes()) { |
| 133 | if (type == EmailType.PREF) { |
| 134 | //ignore because it is converted to a PREF parameter for 4.0 vCards |
| 135 | continue; |
| 136 | } |
| 137 | if (!type.isSupportedBy(version)) { |
| 138 | warnings.add(new ValidationWarning(9, type.getValue())); |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public Email copy() { |
| 145 | return new Email(this); |
| 146 | } |
| 147 | } |