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.TelephoneType; 011 import ezvcard.util.TelUri; 012 013 /* 014 Copyright (c) 2013, Michael Angstadt 015 All rights reserved. 016 017 Redistribution and use in source and binary forms, with or without 018 modification, are permitted provided that the following conditions are met: 019 020 1. Redistributions of source code must retain the above copyright notice, this 021 list of conditions and the following disclaimer. 022 2. Redistributions in binary form must reproduce the above copyright notice, 023 this list of conditions and the following disclaimer in the documentation 024 and/or other materials provided with the distribution. 025 026 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 027 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 028 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 029 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 030 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 031 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 032 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 033 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 034 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 035 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 036 037 The views and conclusions contained in the software and documentation are those 038 of the authors and should not be interpreted as representing official policies, 039 either expressed or implied, of the FreeBSD Project. 040 */ 041 042 /** 043 * A telephone number. 044 * 045 * <p> 046 * <b>Code sample</b> 047 * </p> 048 * 049 * <pre class="brush:java"> 050 * VCard vcard = new VCard(); 051 * Telephone tel = new Telephone("(123) 555-6789"); 052 * tel.addType(TelephoneType.HOME); 053 * tel.setPref(2); //the second-most preferred 054 * vcard.addTelephoneNumber(tel); 055 * 056 * TelUri uri = new TelUri.Builder("+1-800-555-9876").extension("111").build(); 057 * tel = new Telephone(uri); 058 * tel.addType(TelephoneType.WORK); 059 * tel.setPref(1); //the most preferred 060 * vcard.addTelephoneNumber(tel); 061 * </pre> 062 * 063 * <p> 064 * <b>Property name:</b> {@code TEL} 065 * </p> 066 * <p> 067 * <b>Supported versions:</b> {@code 2.1, 3.0, 4.0} 068 * </p> 069 * @author Michael Angstadt 070 */ 071 public class Telephone extends VCardProperty implements HasAltId { 072 private String text; 073 private TelUri uri; 074 075 /** 076 * Creates a telephone property. 077 * @param text the telephone number (e.g. "(123) 555-6789") 078 */ 079 public Telephone(String text) { 080 setText(text); 081 } 082 083 /** 084 * Creates a telephone property. 085 * @param uri a "tel" URI representing the telephone number (vCard 4.0 only) 086 */ 087 public Telephone(TelUri uri) { 088 setUri(uri); 089 } 090 091 /** 092 * Gets the telephone number as a text value. 093 * @return the telephone number or null if the text value is not set 094 */ 095 public String getText() { 096 return text; 097 } 098 099 /** 100 * Sets the telephone number as a text value. 101 * @param text the telephone number 102 */ 103 public void setText(String text) { 104 this.text = text; 105 uri = null; 106 } 107 108 /** 109 * Gets a "tel" URI representing the phone number. 110 * <p> 111 * <b>Supported versions:</b> {@code 4.0} 112 * </p> 113 * @return the "tel" URI or null if it is not set 114 */ 115 public TelUri getUri() { 116 return uri; 117 } 118 119 /** 120 * Sets a "tel" URI representing the phone number. 121 * <p> 122 * <b>Supported versions:</b> {@code 4.0} 123 * </p> 124 * @param uri the "tel" URI 125 */ 126 public void setUri(TelUri uri) { 127 text = null; 128 this.uri = uri; 129 } 130 131 /** 132 * Gets all the TYPE parameters. 133 * @return the TYPE parameters or empty set if there are none 134 */ 135 public Set<TelephoneType> getTypes() { 136 Set<String> values = parameters.getTypes(); 137 Set<TelephoneType> types = new HashSet<TelephoneType>(values.size()); 138 for (String value : values) { 139 types.add(TelephoneType.get(value)); 140 } 141 return types; 142 } 143 144 /** 145 * Adds a TYPE parameter. 146 * @param type the TYPE parameter to add 147 */ 148 public void addType(TelephoneType type) { 149 parameters.addType(type.getValue()); 150 } 151 152 /** 153 * Removes a TYPE parameter. 154 * @param type the TYPE parameter to remove 155 */ 156 public void removeType(TelephoneType type) { 157 parameters.removeType(type.getValue()); 158 } 159 160 @Override 161 public List<Integer[]> getPids() { 162 return super.getPids(); 163 } 164 165 @Override 166 public void addPid(int localId, int clientPidMapRef) { 167 super.addPid(localId, clientPidMapRef); 168 } 169 170 @Override 171 public void removePids() { 172 super.removePids(); 173 } 174 175 @Override 176 public Integer getPref() { 177 return super.getPref(); 178 } 179 180 @Override 181 public void setPref(Integer pref) { 182 super.setPref(pref); 183 } 184 185 //@Override 186 public String getAltId() { 187 return parameters.getAltId(); 188 } 189 190 //@Override 191 public void setAltId(String altId) { 192 parameters.setAltId(altId); 193 } 194 195 @Override 196 protected void _validate(List<Warning> warnings, VCardVersion version, VCard vcard) { 197 if (uri == null && text == null) { 198 warnings.add(new Warning(8)); 199 } 200 201 if (uri != null && (version == VCardVersion.V2_1 || version == VCardVersion.V3_0)) { 202 warnings.add(new Warning(19)); 203 } 204 205 for (TelephoneType type : getTypes()) { 206 if (type == TelephoneType.PREF) { 207 //ignore because it is converted to a PREF parameter for 4.0 vCards 208 continue; 209 } 210 211 if (!type.isSupported(version)) { 212 warnings.add(new Warning(9, type.getValue())); 213 } 214 } 215 } 216 }