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

net.avcompris.commons.query.impl.AbstractConnectorProxy Maven / Gradle / Ivy

There is a newer version: 0.6.3
Show newest version
package net.avcompris.commons.query.impl;

import static com.google.common.base.Preconditions.checkNotNull;

import java.lang.reflect.Array;

import javax.annotation.Nullable;

import net.avcompris.commons.query.Filtering;

abstract class AbstractConnectorProxy, U extends Filtering.Field>
		extends AbstractFilteringProxy implements ConnectorProxy {

	private final String connector;
	protected final Filtering[] fs;

	protected AbstractConnectorProxy(final Class filteringClass, final String connector,
			final Filtering[] fs0) {

		super(filteringClass);

		this.connector = checkNotNull(connector, "connector");

		@SuppressWarnings("unchecked")
		final Filtering[] fs = (Filtering[]) Array.newInstance(filteringClass, fs0.length);

		System.arraycopy(fs0, 0, fs, 0, fs0.length);

		this.fs = fs;
	}

	protected AbstractConnectorProxy(final Class filteringClass, final String connector,
			final Filtering f1, final Filtering f2) {

		super(filteringClass);

		this.connector = checkNotNull(connector, "connector");

		@SuppressWarnings("unchecked")
		final Filtering[] fs = (Filtering[]) Array.newInstance(filteringClass, 2);

		fs[0] = f1;
		fs[1] = f2;

		this.fs = fs;
	}

	protected AbstractConnectorProxy(final Class filteringClass, final String connector,
			final Filtering f, final Filtering[] fs0) {

		super(filteringClass);

		this.connector = checkNotNull(connector, "connector");

		@SuppressWarnings("unchecked")
		final Filtering[] fs = (Filtering[]) Array.newInstance(filteringClass, 1 + fs0.length);

		fs[0] = f;

		System.arraycopy(fs0, 0, fs, 1, fs0.length);

		this.fs = fs;
	}

	protected AbstractConnectorProxy(final Class filteringClass, final String connector,
			final Filtering[] fs0, final Filtering f) {

		super(filteringClass);

		this.connector = checkNotNull(connector, "connector");

		@SuppressWarnings("unchecked")
		final Filtering[] fs = (Filtering[]) Array.newInstance(filteringClass, fs0.length + 1);

		System.arraycopy(fs0, 0, fs, 0, fs0.length);

		fs[fs0.length] = f;

		this.fs = fs;
	}

	protected AbstractConnectorProxy(final Class filteringClass, final String connector,
			final Filtering[] fs1, final Filtering[] fs2) {

		super(filteringClass);

		this.connector = checkNotNull(connector, "connector");

		@SuppressWarnings("unchecked")
		final Filtering[] fs = (Filtering[]) Array.newInstance(filteringClass, fs1.length, fs2.length);

		System.arraycopy(fs1, 0, fs, 0, fs1.length);
		System.arraycopy(fs2, 0, fs, fs1.length, fs2.length);

		this.fs = fs;
	}

	@Override
	public final String getConnector() {

		return connector;
	}

	@Override
	public final Filtering[] getFs() {

		return fs;
	}

	@Override
	public final int hashCode() {

		int hashCode = this.getClass().hashCode();

		for (final Filtering f : fs) {

			hashCode += f.hashCode();
		}

		return hashCode;
	}

	@Override
	public final boolean equals(@Nullable final Object arg) {

		if (arg == null || !(arg instanceof FilteringProxy) || !(arg instanceof ConnectorProxy)) {

			return false;
		}

		@SuppressWarnings("unchecked")
		final FilteringProxy filteringProxy = (FilteringProxy) arg;

		if (!filteringClass.equals(filteringProxy.getFilteringClass())) {

			return false;
		}

		@SuppressWarnings("unchecked")
		final ConnectorProxy refProxy = (ConnectorProxy) arg;

		if (!connector.equals(refProxy.getConnector())) {
			return false;
		}

		final Filtering[] refFs = refProxy.getFs();

		if (fs.length != refFs.length) {

			return false;
		}

		for (int i = 0; i < fs.length; ++i) {

			if (!fs[i].equals(refFs[i])) {

				return false;
			}
		}

		return true;
	}

	@Override
	public final String toString() {

		final StringBuilder sb = new StringBuilder("(");

		boolean start = true;

		for (final Filtering f : fs) {

			if (start) {

				start = false;

			} else {

				sb.append(' ').append(connector).append(' ');
			}

			sb.append(f);
		}

		return sb.append(")").toString();
	}
}