001    package ezvcard.property;
002    
003    import java.util.HashSet;
004    import java.util.List;
005    import java.util.Set;
006    
007    import ezvcard.VCard;
008    import ezvcard.VCardVersion;
009    import ezvcard.Warning;
010    import ezvcard.parameter.EmailType;
011    
012    /*
013     Copyright (c) 2013, Michael Angstadt
014     All rights reserved.
015    
016     Redistribution and use in source and binary forms, with or without
017     modification, are permitted provided that the following conditions are met: 
018    
019     1. Redistributions of source code must retain the above copyright notice, this
020     list of conditions and the following disclaimer. 
021     2. Redistributions in binary form must reproduce the above copyright notice,
022     this list of conditions and the following disclaimer in the documentation
023     and/or other materials provided with the distribution. 
024    
025     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
026     ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
027     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
028     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
029     ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
031     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
032     ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
033     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
034     SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
035    
036     The views and conclusions contained in the software and documentation are those
037     of the authors and should not be interpreted as representing official policies, 
038     either expressed or implied, of the FreeBSD Project.
039     */
040    
041    /**
042     * An email address associated with a person.
043     * 
044     * <p>
045     * <b>Code sample</b>
046     * </p>
047     * 
048     * <pre class="brush:java">
049     * VCard vcard = new VCard();
050     * 
051     * Email email = new Email(&quot;superdude55@hotmail.com&quot;);
052     * email.addType(EmailType.HOME);
053     * vcard.addEmail(email);
054     * 
055     * email = new Email(&quot;doe.john@company.com&quot;);
056     * email.addType(EmailType.WORK);
057     * email.setPref(1); //the most preferred email
058     * vcard.addEmail(email);
059     * </pre>
060     * 
061     * <p>
062     * <b>Property name:</b> {@code EMAIL}
063     * </p>
064     * <p>
065     * <b>Supported versions:</b> {@code 2.1, 3.0, 4.0}
066     * </p>
067     * @author Michael Angstadt
068     */
069    public class Email extends TextProperty implements HasAltId {
070            /**
071             * Creates an email property.
072             * @param email the email (e.g. "johndoe@example.com")
073             */
074            public Email(String email) {
075                    super(email);
076            }
077    
078            /**
079             * Gets all the TYPE parameters.
080             * @return the TYPE parameters or empty set if there are none
081             */
082            public Set<EmailType> getTypes() {
083                    Set<String> values = parameters.getTypes();
084                    Set<EmailType> types = new HashSet<EmailType>(values.size());
085                    for (String value : values) {
086                            types.add(EmailType.get(value));
087                    }
088                    return types;
089            }
090    
091            /**
092             * Adds a TYPE parameter.
093             * @param type the TYPE parameter to add
094             */
095            public void addType(EmailType type) {
096                    parameters.addType(type.getValue());
097            }
098    
099            /**
100             * Removes a TYPE parameter.
101             * @param type the TYPE parameter to remove
102             */
103            public void removeType(EmailType type) {
104                    parameters.removeType(type.getValue());
105            }
106    
107            @Override
108            public List<Integer[]> getPids() {
109                    return super.getPids();
110            }
111    
112            @Override
113            public void addPid(int localId, int clientPidMapRef) {
114                    super.addPid(localId, clientPidMapRef);
115            }
116    
117            @Override
118            public void removePids() {
119                    super.removePids();
120            }
121    
122            @Override
123            public Integer getPref() {
124                    return super.getPref();
125            }
126    
127            @Override
128            public void setPref(Integer pref) {
129                    super.setPref(pref);
130            }
131    
132            //@Override
133            public String getAltId() {
134                    return parameters.getAltId();
135            }
136    
137            //@Override
138            public void setAltId(String altId) {
139                    parameters.setAltId(altId);
140            }
141    
142            @Override
143            protected void _validate(List<Warning> warnings, VCardVersion version, VCard vcard) {
144                    super._validate(warnings, version, vcard);
145    
146                    for (EmailType type : getTypes()) {
147                            if (type == EmailType.PREF) {
148                                    //ignore because it is converted to a PREF parameter for 4.0 vCards
149                                    continue;
150                            }
151                            if (!type.isSupported(version)) {
152                                    warnings.add(new Warning(9, type.getValue()));
153                            }
154                    }
155            }
156    }