<commit_before>/*
    Copyright (c) 2014 <NAME>
    
    This file is part of the library "Java Add-in for Microsoft Office".

    This file must be used according to the terms of   
      
      MIT License, http://opensource.org/licenses/MIT

 */
package com.wilutions.com;

/**
 * Objects received from COM are of this class. This class provides functions to
 * invoke properties and methods via the IDispatch interface.
 */
public class Dispatch implements IDispatch {

	/**
	 * Pointer to a memory address of the attached JNI object.
	 */
	protected long ndisp;

	/**
	 * Default constructor. No JNI object is attached.
	 */
	public Dispatch() {
	}

	/**
	 * Initialization constructor called from JNI. Do not invoke this
	 * constructor with an arbitrary value.
	 * 
	 * @param ndisp
	 *            Address of the native COM object.
	 */
	protected Dispatch(long ndisp) {
		this.ndisp = ndisp;
		JoaDll.addRef(this);
	}

	/**
	 * Copy constructor. After initialization, the native object is assigned to
	 * both, the passed object and the constructed object.
	 * 
	 * @param rhs
	 */
	public Dispatch(Dispatch rhs) {
		this.ndisp = rhs.ndisp;
		JoaDll.addRef(this);
	}

	/**
	 * Instantiate the COM object and assign its native pointer to this.
	 * 
	 * @param progId
	 *            Registered program ID.
	 * @throws ComException
	 */
	public Dispatch(String progId) throws ComException {
		JoaDll.nativeCoCreateInstance(progId, this);
	}

	/**
	 * Null value for Dispatch objects. Use this value instead on NULL in call,
	 * set and get functions.
	 */
	public final static Dispatch NULL = new Dispatch(0);

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (int) (ndisp ^ (ndisp >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		Dispatch other = (Dispatch) obj;
		if (ndisp != other.ndisp)
			return false;
		return true;
	}

	/**
	 * Finalizer function releases the COM object.
	 */
	@Override
	protected void finalize() throws Throwable {
		JoaDll.release(this);
		super.finalize();
	}

	/**
	 * Convert this object into an object of the given class. A new object of
	 * the given class is created and the internal native object is transferred
	 * to the new object. This object hereby looses its internal native object.
	 * 
	 * @param clazz
	 *            Class object.
	 * @return new object of type clazz.
	 */
	public <T extends Dispatch> T as(Class<T> clazz) {

		// This check ...
		// if (!this.is(clazz)) {
		// throw new ClassCastException("Object does not implement " + clazz);
		// }
		// ... causes an error if the object does not have a dual interface
		// (only an IDispatch)
		// E.g. Page oPage =
		// oInsp.getModifiedFormPages().as(PagesImpl.class).Add("My Page").as(Page.class);
		// would throw an exception on the last "as".

		return uncheckedAs(clazz);
	}

	/**
	 * Static version of {@link #as(Class)}.
	 * 
	 * @param idisp
	 *            Dispatch object
	 * @param clazz
	 *            Class
	 * @return Casted object.
	 */
	public static <T extends Dispatch> T as(Object idisp, Class<T> clazz) {
		T ret = null;
		assert idisp != null;
		assert idisp instanceof Dispatch;
		Dispatch pthis = ((Dispatch) idisp);
		ret = pthis.as(clazz);
		return ret;
	}

	/**
	 * This function tests whether the object implements all COM interfaces of
	 * the given class.
	 * 
	 * @param clazz
	 *            Class or interface.
	 * @return true, if this implements all interfaces of clazz. The interfaces
	 *         to check are found by annotation CoInterface. If clazz does not
	 *         have any CoInterface, the function returns false.
	 */
	public boolean is(Class<?> clazz) {
		return JoaDll.dispatchIs(this, clazz);
	}

	/**
	 * Same as function {@link #as(Class)}.
	 * 
	 * @param clazz
	 * @return new object of type clazz.
	 */
	public <T extends Dispatch> T uncheckedAs(Class<T> clazz) {
		try {
			@SuppressWarnings("unchecked")<commit_msg>Dispatch.as returns null if param is null - no assertion<commit_after>/*
    Copyright (c) 2014 <NAME>
    
    This file is part of the library "Java Add-in for Microsoft Office".

    This file must be used according to the terms of   
      
      MIT License, http://opensource.org/licenses/MIT

 */
package com.wilutions.com;

/**
 * Objects received from COM are of this class. This class provides functions to
 * invoke properties and methods via the IDispatch interface.
 */
public class Dispatch implements IDispatch {

	/**
	 * Pointer to a memory address of the attached JNI object.
	 */
	protected long ndisp;

	/**
	 * Default constructor. No JNI object is attached.
	 */
	public Dispatch() {
	}

	/**
	 * Initialization constructor called from JNI. Do not invoke this
	 * constructor with an arbitrary value.
	 * 
	 * @param ndisp
	 *            Address of the native COM object.
	 */
	protected Dispatch(long ndisp) {
		this.ndisp = ndisp;
		JoaDll.addRef(this);
	}

	/**
	 * Copy constructor. After initialization, the native object is assigned to
	 * both, the passed object and the constructed object.
	 * 
	 * @param rhs
	 */
	public Dispatch(Dispatch rhs) {
		this.ndisp = rhs.ndisp;
		JoaDll.addRef(this);
	}

	/**
	 * Instantiate the COM object and assign its native pointer to this.
	 * 
	 * @param progId
	 *            Registered program ID.
	 * @throws ComException
	 */
	public Dispatch(String progId) throws ComException {
		JoaDll.nativeCoCreateInstance(progId, this);
	}

	/**
	 * Null value for Dispatch objects. Use this value instead on NULL in call,
	 * set and get functions.
	 */
	public final static Dispatch NULL = new Dispatch(0);

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (int) (ndisp ^ (ndisp >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		Dispatch other = (Dispatch) obj;
		if (ndisp != other.ndisp)
			return false;
		return true;
	}

	/**
	 * Finalizer function releases the COM object.
	 */
	@Override
	protected void finalize() throws Throwable {
		JoaDll.release(this);
		super.finalize();
	}

	/**
	 * Convert this object into an object of the given class. A new object of
	 * the given class is created and the internal native object is transferred
	 * to the new object. This object hereby looses its internal native object.
	 * 
	 * @param clazz
	 *            Class object.
	 * @return new object of type clazz.
	 */
	public <T extends Dispatch> T as(Class<T> clazz) {

		// This check ...
		// if (!this.is(clazz)) {
		// throw new ClassCastException("Object does not implement " + clazz);
		// }
		// ... causes an error if the object does not have a dual interface
		// (only an IDispatch)
		// E.g. Page oPage =
		// oInsp.getModifiedFormPages().as(PagesImpl.class).Add("My Page").as(Page.class);
		// would throw an exception on the last "as".

		return uncheckedAs(clazz);
	}

	/**
	 * Static version of {@link #as(Class)}.
	 * 
	 * @param idisp
	 *            Dispatch object
	 * @param clazz
	 *            Class
	 * @return Casted object or null, if idisp is null.
	 */
	public static <T extends Dispatch> T as(Object idisp, Class<T> clazz) {
		T ret = null;
		if (idisp != null) {
			assert idisp instanceof Dispatch;
			Dispatch pthis = ((Dispatch) idisp);
			ret = pthis.as(clazz);
		}
		return ret;
	}

	/**
	 * This function tests whether the object implements all COM interfaces of
	 * the given class.
	 * 
	 * @param clazz
	 *            Class or interface.
	 * @return true, if this implements all interfaces of clazz. The interfaces
	 *         to check are found by annotation CoInterface. If clazz does not
	 *         have any CoInterface, the function returns false.
	 */
	public boolean is(Class<?> clazz) {
		return JoaDll.dispatchIs(this, clazz);
	}

	/**
	 * Same as function {@link #as(Class)}.
	 * 
	 * @param clazz
	 * @return new object of type clazz.
	 */
	public <T extends Dispatch> T uncheckedAs(Class<T> clazz) {
		try {
			@SuppressWarnings("unchecked")
{'fraction_non_alphanumeric': 0.10430727696649895, 'fraction_numerical': 0.0024542888697999753, 'mean_word_length': 2.7679149329634765, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '2391876', 'n_tokens_mistral': 2688, 'n_tokens_neox': 2404, 'n_words': 931}