001/* 002 * The MIT License 003 * Copyright (c) 2012 Microsoft Corporation 004 * 005 * Permission is hereby granted, free of charge, to any person obtaining a copy 006 * of this software and associated documentation files (the "Software"), to deal 007 * in the Software without restriction, including without limitation the rights 008 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 009 * copies of the Software, and to permit persons to whom the Software is 010 * furnished to do so, subject to the following conditions: 011 * 012 * The above copyright notice and this permission notice shall be included in 013 * all copies or substantial portions of the Software. 014 * 015 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 016 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 017 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 018 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 019 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 020 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 021 * THE SOFTWARE. 022 */ 023 024package microsoft.exchange.webservices.data.property.complex; 025 026import microsoft.exchange.webservices.data.core.EwsServiceXmlReader; 027import microsoft.exchange.webservices.data.core.EwsServiceXmlWriter; 028import microsoft.exchange.webservices.data.core.EwsUtilities; 029import microsoft.exchange.webservices.data.core.XmlAttributeNames; 030import microsoft.exchange.webservices.data.core.exception.service.local.ServiceXmlSerializationException; 031import org.apache.commons.lang3.StringUtils; 032 033/** 034 * Represents the Id of an Exchange object. 035 */ 036public abstract class ServiceId extends ComplexProperty { 037 038 /** 039 * The change key. 040 */ 041 private String changeKey; 042 043 /** 044 * The unique id. 045 */ 046 private String uniqueId; 047 048 /** 049 * Initializes a new instance. 050 */ 051 public ServiceId() { 052 super(); 053 } 054 055 /** 056 * Initializes a new instance. 057 * 058 * @param uniqueId The unique id. 059 * @throws Exception the exception 060 */ 061 public ServiceId(String uniqueId) throws Exception { 062 this(); 063 EwsUtilities.validateParam(uniqueId, "uniqueId"); 064 this.uniqueId = uniqueId; 065 } 066 067 public ServiceId(String uniqueId, String changeKey) throws Exception { 068 this(uniqueId); 069 EwsUtilities.validateParam(changeKey, "changeKey"); 070 this.changeKey = changeKey; 071 } 072 073 /** 074 * Read attribute from XML. 075 * 076 * @param reader The reader. 077 * @throws Exception the exception 078 */ 079 @Override 080 public void readAttributesFromXml(EwsServiceXmlReader reader) 081 throws Exception { 082 this.uniqueId = reader.readAttributeValue(XmlAttributeNames.Id); 083 this.changeKey = reader.readAttributeValue(XmlAttributeNames.ChangeKey); 084 085 } 086 087 /** 088 * Writes attribute to XML. 089 * 090 * @param writer The writer. 091 * @throws ServiceXmlSerializationException the service xml serialization exception 092 */ 093 @Override 094 public void writeAttributesToXml(EwsServiceXmlWriter writer) 095 throws ServiceXmlSerializationException { 096 writer.writeAttributeValue(XmlAttributeNames.Id, this.getUniqueId()); 097 writer.writeAttributeValue(XmlAttributeNames.ChangeKey, this 098 .getChangeKey()); 099 } 100 101 /** 102 * Gets the name of the XML element. 103 * 104 * @return XML element name. 105 */ 106 public abstract String getXmlElementName(); 107 108 /** 109 * Writes to XML. 110 * 111 * @param writer The writer. 112 * @throws Exception the exception 113 */ 114 public void writeToXml(EwsServiceXmlWriter writer) throws Exception { 115 this.writeToXml(writer, this.getXmlElementName()); 116 } 117 118 /** 119 * Assigns from existing id. 120 * 121 * @param source The source. 122 */ 123 public void assign(ServiceId source) { 124 this.uniqueId = source.getUniqueId(); 125 this.changeKey = source.getChangeKey(); 126 } 127 128 /** 129 * True if this instance is valid, false otherthise. 130 * 131 * @return true if this instance is valid; otherwise,false 132 */ 133 public boolean isValid() { 134 return (null != this.uniqueId && !this.uniqueId.isEmpty()); 135 } 136 137 /** 138 * Gets the unique Id of the Exchange object. 139 * 140 * @return unique Id of the Exchange object. 141 */ 142 public String getUniqueId() { 143 return uniqueId; 144 } 145 146 /** 147 * Sets the unique Id of the Exchange object. 148 * 149 * @param uniqueId unique Id of the Exchange object. 150 */ 151 public void setUniqueId(String uniqueId) { 152 this.uniqueId = uniqueId; 153 } 154 155 /** 156 * Gets the change key associated with the Exchange object. The change key 157 * represents the version of the associated item or folder. 158 * 159 * @return change key associated with the Exchange object. 160 */ 161 public String getChangeKey() { 162 return changeKey; 163 } 164 165 /** 166 * Sets the change key associated with the Exchange object. The change key 167 * represents the version of the associated item or folder. 168 * 169 * @param changeKey change key associated with the Exchange object. 170 */ 171 public void setChangeKey(String changeKey) { 172 this.changeKey = changeKey; 173 } 174 175 /** 176 * Determines whether two ServiceId instances are equal (including 177 * ChangeKeys). 178 * 179 * @param other The ServiceId to compare with the current ServiceId. 180 * @return true if equal otherwise false. 181 */ 182 public boolean sameIdAndChangeKey(final ServiceId other) { 183 return this.equals(other) && StringUtils.equals(this.getChangeKey(), other.getChangeKey()); 184 } 185 186 /** 187 * Determines whether the specified instance is equal to the current 188 * instance. We do not consider the ChangeKey for ServiceId.Equals. 189 * 190 * @param obj The object to compare with the current instance 191 * @return true if the specified object is equal to the current instance, 192 * otherwise, false. 193 */ 194 @Override 195 public boolean equals(Object obj) { 196 if (super.equals(obj)) { 197 return true; 198 } else { 199 if (!(obj instanceof ServiceId)) { 200 return false; 201 } else { 202 ServiceId other = (ServiceId) obj; 203 if (!(this.isValid() && other.isValid())) { 204 return false; 205 } else { 206 return this.getUniqueId().equals(other.getUniqueId()); 207 } 208 } 209 } 210 } 211 212 /** 213 * Serves as a hash function for a particular type. We do not consider the 214 * change key in the hash code computation. 215 * 216 * @return A hash code for the current 217 */ 218 @Override 219 public int hashCode() { 220 return this.isValid() ? this.getUniqueId().hashCode() : super 221 .hashCode(); 222 } 223 224 /** 225 * Returns a string that represents the current instance. 226 * 227 * @return A string that represents the current instance. 228 */ 229 @Override 230 public String toString() { 231 return (this.uniqueId == null) ? "" : this.uniqueId; 232 } 233}