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

net.avcompris.commons.query.impl.NotProxy 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 static net.avcompris.commons.query.Filtering.Type.NOT;

import javax.annotation.Nullable;

import net.avcompris.commons.query.Filtering;
import net.avcompris.commons.query.FilteringHandler;
import net.avcompris.commons.query.Not;

final class NotProxy, U extends Filtering.Field> extends AbstractFilteringProxy
		implements Not {

	private final Filtering f;

	public NotProxy(final Class filteringClass, final Filtering f) {

		super(filteringClass);

		this.f = checkNotNull(f, "f");
	}

	@Override
	public boolean match(final Object arg) {

		return !f.match(arg);
	}

	@Override
	public void applyTo(final FilteringHandler handler) {

		final FilteringHandler subHandler = handler.newNotMember();

		f.applyTo(subHandler);
	}

	@Override
	public int hashCode() {

		return 10_000 - 2 * f.hashCode();
	}

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

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

			return false;
		}

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

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

			return false;
		}

		final NotProxy refProxy = (NotProxy) filteringProxy.getImpl();

		return f.equals(refProxy.f);
	}

	@Override
	public String toString() {

		return "not(" + f + ")";
	}

	@Override
	public Type getType() {

		return NOT;
	}

	@Override
	public Filtering getArg() {

		return f;
	}
}