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.definition;
025
026import microsoft.exchange.webservices.data.core.EwsServiceXmlWriter;
027import microsoft.exchange.webservices.data.core.XmlAttributeNames;
028import microsoft.exchange.webservices.data.core.XmlElementNames;
029import microsoft.exchange.webservices.data.core.exception.service.local.ServiceXmlSerializationException;
030
031/**
032 * Represents an indexed property definition.
033 */
034public final class IndexedPropertyDefinition extends
035    ServiceObjectPropertyDefinition {
036
037  // Index attribute of IndexedFieldURI element.
038  /**
039   * The index.
040   */
041  private String index;
042
043  /**
044   * Initializes a new instance of the IndexedPropertyDefinition class.
045   *
046   * @param uri   The FieldURI attribute of the IndexedFieldURI element.
047   * @param index The Index attribute of the IndexedFieldURI element.
048   */
049  public IndexedPropertyDefinition(String uri, String index) {
050    super(uri);
051    this.index = index;
052  }
053
054  /**
055   * Determines whether two specified instances of IndexedPropertyDefinition
056   * are equal.
057   *
058   * @param idxPropDef1 First indexed property definition.
059   * @param idxPropDef2 Second indexed property definition.
060   * @return True if indexed property definitions are equal.
061   */
062  protected static boolean isEqualTo(IndexedPropertyDefinition idxPropDef1,
063      IndexedPropertyDefinition idxPropDef2) {
064    return (idxPropDef1 == idxPropDef2) ||
065        (idxPropDef1 != null &&
066            idxPropDef2 != null &&
067            idxPropDef1.getUri().equalsIgnoreCase(
068                idxPropDef2.getUri()) && idxPropDef1.index
069            .equalsIgnoreCase(idxPropDef2.index));
070  }
071
072  /**
073   * Gets the index of the property.
074   *
075   * @return The index string of the property.
076   */
077  public String getIndex() {
078    return this.index;
079  }
080
081  /**
082   * Writes the attribute to XML.
083   *
084   * @param writer the writer
085   * @throws ServiceXmlSerializationException the service xml serialization exception
086   */
087  @Override
088  protected void writeAttributesToXml(EwsServiceXmlWriter writer)
089      throws ServiceXmlSerializationException {
090    super.writeAttributesToXml(writer);
091    writer.writeAttributeValue(XmlAttributeNames.FieldIndex, this
092        .getIndex());
093  }
094
095  /**
096   * Gets the name of the XML element.
097   *
098   * @return XML element name.
099   */
100  @Override
101  protected String getXmlElementName() {
102    return XmlElementNames.IndexedFieldURI;
103  }
104
105  /**
106   * Gets the property definition's printable name.
107   *
108   * @return The property definition's printable name.
109   */
110  @Override public String getPrintableName() {
111    return String.format("%s:%s", this.getUri(), this.getIndex());
112  }
113
114
115  /**
116   * Determines whether a given indexed property definition is equal to this
117   * indexed property definition.
118   *
119   * @param obj The
120   *            object to check for equality.
121   * @return True if the property definitions define the same indexed
122   * property.
123   */
124  @Override
125  public boolean equals(Object obj) {
126    if (this == obj) {
127      return true;
128    }
129    if (obj instanceof IndexedPropertyDefinition) {
130      return IndexedPropertyDefinition.isEqualTo(
131          (IndexedPropertyDefinition) obj, this);
132    } else {
133      return false;
134    }
135  }
136
137  /**
138   * Serves as a hash function for a particular type.
139   *
140   * @return A hash code for the current System.Object
141   */
142  @Override
143  public int hashCode() {
144    return this.getUri().hashCode() ^ this.getIndex().hashCode();
145  }
146
147  /**
148   * Gets the property type.
149   */
150  @Override
151  public Class<String> getType() {
152    return String.class;
153  }
154
155}