<commit_before>package org.exist.fluent;

import java.util.Date;

import javax.xml.datatype.Duration;
import javax.xml.datatype.XMLGregorianCalendar;

import org.exist.dom.NodeProxy;
import org.exist.storage.DBBroker;
import org.exist.storage.serializers.Serializer;
import org.exist.xquery.Constants;
import org.exist.xquery.XPathException;
import org.exist.xquery.value.*;
import org.xml.sax.SAXException;

/**
 * An XML item in the database.  While most often used to represent XML
 * elements, it can also stand in for any DOM node or an atomic value.  However, it
 * is not used to represent entire XML documents (see {@link org.exist.fluent.XMLDocument}).
 * Not all operations are valid in all cases.
 * 
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version $Revision: 1.13 $ ($Date: 2006/04/14 04:12:04 $)
 */
public class Item extends Resource {
	
	protected final org.exist.xquery.value.Item item;
	
	Item() {
		super(null, null);
		this.item = null;
	}
	
	Item(org.exist.xquery.value.Item item, NamespaceMap namespaceBindings, Database db) {
		super(namespaceBindings, db);
		// the item should've been tracked (Database.trackNode) before getting here!
		this.item = item;
	}
	
	/**
	 * Return this item cast as a node.
	 * 
	 * @return this item cast as a node
	 * @throws DatabaseException if this item is not a node
	 */
	public Node node() {
		throw new DatabaseException("this item is not a node: " + this);
	}
	
	@Override public boolean equals(Object o) {
		if (!(o instanceof Item)) return false;
		Item that = (Item) o;
		if (this.item == that.item) return true;
		if (this.item instanceof AtomicValue && that.item instanceof AtomicValue) {
			AtomicValue thisValue = (AtomicValue) this.item, thatValue = (AtomicValue) that.item;
			try {
				return
						thisValue.getType() == thatValue.getType()
						&& thisValue.compareTo(null, Constants.EQ, thatValue);
			} catch (XPathException e) {
				// fall through
			}
		}
		return false;
	}
	
	/**
	 * The hash code computation can be expensive, and the hash codes may not be very well distributed.
	 * You probably shouldn't use items in situations where they might get hashed.
	 */
	@Override public int hashCode() {
		if (item instanceof AtomicValue) {
			AtomicValue value = (AtomicValue) item;
			try {
				return value.getType() ^ value.getStringValue().hashCode();
			} catch (XPathException e) {
				return value.getType();
			}
		} else {
			return item.hashCode();
		}
	}
	
	/**
	 * Return whether this item really exists.  Examples of items that don't exist even though they have an object
	 * representing them include virtual placeholders returned for an optional query that didn't select an item, and
	 * items that were deleted from the database after being selected.
	 * 
	 * @return <code>true</code> if the item exists, <code>false</code> otherwise
	 */
	public boolean extant() {
		return true;
	}

	@Override	Sequence convertToSequence() {
		return item.toSequence();
	}
	
	/**
	 * Return a singleton item list consisting of this item.
	 *
	 * @return an item list that contains only this item
	 */<commit_msg>Fluent: added Item.type() method.<commit_after>package org.exist.fluent;

import java.util.Date;

import javax.xml.datatype.Duration;
import javax.xml.datatype.XMLGregorianCalendar;

import org.exist.dom.NodeProxy;
import org.exist.storage.DBBroker;
import org.exist.storage.serializers.Serializer;
import org.exist.xquery.Constants;
import org.exist.xquery.XPathException;
import org.exist.xquery.value.*;
import org.xml.sax.SAXException;

/**
 * An XML item in the database.  While most often used to represent XML
 * elements, it can also stand in for any DOM node or an atomic value.  However, it
 * is not used to represent entire XML documents (see {@link org.exist.fluent.XMLDocument}).
 * Not all operations are valid in all cases.
 * 
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version $Revision: 1.13 $ ($Date: 2006/04/14 04:12:04 $)
 */
public class Item extends Resource {
	
	protected final org.exist.xquery.value.Item item;
	
	Item() {
		super(null, null);
		this.item = null;
	}
	
	Item(org.exist.xquery.value.Item item, NamespaceMap namespaceBindings, Database db) {
		super(namespaceBindings, db);
		// the item should've been tracked (Database.trackNode) before getting here!
		this.item = item;
	}
	
	/**
	 * Return this item cast as a node.
	 * 
	 * @return this item cast as a node
	 * @throws DatabaseException if this item is not a node
	 */
	public Node node() {
		throw new DatabaseException("this item is not a node: " + this);
	}
	
	@Override public boolean equals(Object o) {
		if (!(o instanceof Item)) return false;
		Item that = (Item) o;
		if (this.item == that.item) return true;
		if (this.item instanceof AtomicValue && that.item instanceof AtomicValue) {
			AtomicValue thisValue = (AtomicValue) this.item, thatValue = (AtomicValue) that.item;
			try {
				return
						thisValue.getType() == thatValue.getType()
						&& thisValue.compareTo(null, Constants.EQ, thatValue);
			} catch (XPathException e) {
				// fall through
			}
		}
		return false;
	}
	
	/**
	 * The hash code computation can be expensive, and the hash codes may not be very well distributed.
	 * You probably shouldn't use items in situations where they might get hashed.
	 */
	@Override public int hashCode() {
		if (item instanceof AtomicValue) {
			AtomicValue value = (AtomicValue) item;
			try {
				return value.getType() ^ value.getStringValue().hashCode();
			} catch (XPathException e) {
				return value.getType();
			}
		} else {
			return item.hashCode();
		}
	}
	
	/**
	 * Return the type of this item, e.g. element() or xs:string.
	 *
	 * @return the type of this item
	 */
	public String type() {
		return Type.getTypeName(item.getType());
	}
	
	/**
	 * Return whether this item really exists.  Examples of items that don't exist even though they have an object
	 * representing them include virtual placeholders returned for an optional query that didn't select an item, and
	 * items that were deleted from the database after being selected.
	 * 
	 * @return <code>true</code> if the item exists, <code>false</code> otherwise
	 */
	public boolean extant() {
		return true;
	}

	@Override	Sequence convertToSequence() {
		return item.toSequence();
	}
	
	/**
	 * Return a singleton item list consisting of this item.
	 *
	 * @return an item list that contains only this item
	 */
{'fraction_non_alphanumeric': 0.10915328239513489, 'fraction_numerical': 0.005301730859192266, 'mean_word_length': 3.555397727272727, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2487516', 'n_tokens_mistral': 2121, 'n_tokens_neox': 1882, 'n_words': 680}