All Downloads are FREE. Search and download functionalities are using the official Maven repository.

microsoft.exchange.webservices.data.StringList Maven / Gradle / Ivy

Go to download

The source came from http://archive.msdn.microsoft.com/ewsjavaapi Support for Maven has been added.

There is a newer version: 1.1.5.2
Show newest version
/**************************************************************************
 * copyright file="StringList.java" company="Microsoft"
 *     Copyright (c) Microsoft Corporation.  All rights reserved.
 * 
 * Defines the StringList.java.
 **************************************************************************/
package microsoft.exchange.webservices.data;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.xml.stream.XMLStreamException;

/**
 * Represents a list of strings.
 */
public class StringList extends ComplexProperty implements Iterable {

	/** The items. */
	private List items = new ArrayList();

	/** The item xml element name. */
	private String itemXmlElementName = XmlElementNames.String;
	
	/**
	 * Initializes a new instance of the "StringList" class.
	 */
	public StringList() {
	}

	/**
	 * Initializes a new instance of the  class.
	 * 
	 * @param strings
	 *            The strings.
	 */
	public StringList(Iterable strings) {
		this.addRange(strings);
	}

	/**
	 * Initializes a new instance of the "StringList" class.
	 * 
	 * @param itemXmlElementName
	 *            Name of the item XML element.
	 */
	protected StringList(String itemXmlElementName) {
		this.itemXmlElementName = itemXmlElementName;
	}

	/**
	 * * Tries to read element from XML.
	 * 
	 * @param reader
	 *            accepts EwsServiceXmlReader
	 * @return True if element was read
	 * @throws XMLStreamException
	 *             the xML stream exception
	 * @throws ServiceXmlDeserializationException
	 *             the service xml deserialization exception
	 */
	@Override
	protected boolean tryReadElementFromXml(EwsServiceXmlReader reader)
			throws XMLStreamException, ServiceXmlDeserializationException {
		if (reader.getLocalName().equals(this.itemXmlElementName)) {
			this.add(reader.readValue());
			return true;
		} else {
			return false;
		}
	}

	/**
	 * * Writes elements to XML.
	 * 
	 * @param writer
	 *            accepts EwsServiceXmlWriter
	 * @throws ServiceXmlSerializationException
	 *             the service xml serialization exception
	 * @throws XMLStreamException
	 *             the xML stream exception
	 */
	@Override
	protected void writeElementsToXml(EwsServiceXmlWriter writer)
			throws ServiceXmlSerializationException, XMLStreamException {
		for (String item : this.items) {
			writer.writeStartElement(XmlNamespace.Types,
					this.itemXmlElementName);
			writer.writeValue(item, this.itemXmlElementName);
			writer.writeEndElement();
		}
	}

	/**
	 * Adds a string to the list.
	 * 
	 * @param s
	 *            The string to add.
	 */
	public void add(String s) {		
			this.items.add(s);
			this.changed();		
	}

	/**
	 * Adds multiple strings to the list.
	 * 
	 * @param strings
	 *            The strings to add.
	 */
	public void addRange(Iterable strings) {
		boolean changed = false;

		for (String s : strings) {
			if (!this.contains(s)) {
				this.items.add(s);
				changed = true;
			}
		}
		if (changed) {
			this.changed();
		}
	}

	/**
	 * Determines whether the list contains a specific string.
	 * 
	 * @param s
	 *            The string to check the presence of.
	 * @return True if s is present in the list, false otherwise.
	 */
	public boolean contains(String s) {
		return this.items.contains(s);
	}

	/**
	 * Removes a string from the list.
	 * 
	 * @param s
	 *            The string to remove.
	 * @return True is s was removed, false otherwise.
	 */
	public boolean remove(String s) {
		boolean result = this.items.remove(s);
		if (result) {
			this.changed();
		}
		return result;
	}

	/**
	 * Removes the string at the specified position from the list.
	 * 
	 * @param index
	 *            The index of the string to remove.
	 */
	public void removeAt(int index) {
		if (index < 0 || index >= this.getSize()) {
			throw new ArrayIndexOutOfBoundsException(Strings.IndexIsOutOfRange);
		}
		this.items.remove(index);
		this.changed();
	}

	/**
	 * Clears the list.
	 */
	public void clearList() {
		this.items.clear();
		this.changed();
	}

	/**
	 * Returns a string representation of the object. In general, the
	 * toString method returns a string that "textually represents"
	 * this object. The result should be a concise but informative
	 * representation that is easy for a person to read. It is recommended that
	 * all subclasses override this method.
	 * 

* The toString method for class Object returns a * string consisting of the name of the class of which the object is an * instance, the at-sign character `@', and the unsigned * hexadecimal representation of the hash code of the object. In other * words, this method returns a string equal to the value of:

* *
	 * getClass().getName() + '@' + Integer.toHexString(hashCode())
	 * 
* *
* * @return a string representation of the object. */ @Override public String toString() { StringBuffer temp = new StringBuffer(); for (String str : this.items) { temp.append(str.concat(",")); } String tempString = temp.toString(); return tempString; } /** * Gets the number of strings in the list. * * @return the size */ public int getSize() { return this.items.size(); } /** * Gets the string at the specified index. * * @param index * The index of the string to get or set. * @return The string at the specified index. */ public String getString(int index) { if (index < 0 || index >= this.getSize()) { throw new ArrayIndexOutOfBoundsException(Strings.IndexIsOutOfRange); } return this.items.get(index); } /** * Sets the string at the specified index. * * @param index * The index * @param object * The object. */ public void setString(int index, Object object) { if (index < 0 || index >= this.getSize()) { throw new ArrayIndexOutOfBoundsException(Strings.IndexIsOutOfRange); } if (this.items.get(index) != object) { this.items.set(index, (String) object); this.changed(); } } /** * Gets an iterator that iterates through the elements of the collection. * * @return An Iterator for the collection. */ public Iterator getIterator() { return this.items.iterator(); } /** * Indicates whether some other object is "equal to" this one. *

* The equals method implements an equivalence relation on * non-null object references: *

    *
  • It is reflexive: for any non-null reference value * x, x.equals(x) should return true. *
  • It is symmetric: for any non-null reference values * x and y, x.equals(y) should return * true if and only if y.equals(x) returns * true. *
  • It is transitive: for any non-null reference values * x, y, and z, if * x.equals(y) returns true and * y.equals(z) returns true, then * x.equals(z) should return true. *
  • It is consistent: for any non-null reference values * x and y, multiple invocations of * x.equals(y) consistently return true or * consistently return false, provided no information used in * equals comparisons on the objects is modified. *
  • For any non-null reference value x, * x.equals(null) should return false. *
*

* The equals method for class Object implements the * most discriminating possible equivalence relation on objects; that is, * for any non-null reference values x and y, this * method returns true if and only if x and * y refer to the same object (x == y has the * value true). *

* Note that it is generally necessary to override the hashCode * method whenever this method is overridden, so as to maintain the general * contract for the hashCode method, which states that equal * objects must have equal hash codes. * * @param obj * the reference object with which to compare. * @return if this object is the same as the obj argument; otherwise. * @see #hashCode() * @see java.util.Hashtable */ @Override public boolean equals(Object obj) { if (obj instanceof StringList) { StringList other = (StringList) obj; return this.toString().equals(other.toString()); } else { return false; } } /** * Serves as a hash function for a particular type. * * @return A hash code for the current "T:System.Object". */ @Override public int hashCode() { return this.toString().hashCode(); } /** * Returns an iterator over a set of elements of type T. * * @return an Iterator. */ @Override public Iterator iterator() { return items.iterator(); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy