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

org.springframework.data.gemfire.snapshot.filter.ComposableSnapshotFilter Maven / Gradle / Ivy

There is a newer version: 2.3.9.RELEASE
Show newest version
/*
 * Copyright 2010-2013 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.data.gemfire.snapshot.filter;

import java.util.Map;

import com.gemstone.gemfire.cache.snapshot.SnapshotFilter;

/**
 * The ComposableSnapshotFilter class is a GemFire SnapshotFilter implementation of the Composition design pattern
 * allowing 2 or more SnapshotFilters to be combined by logical AND and OR operators acting as a single SnapshotFilter.
 *
 * @author John Blum
 * @see com.gemstone.gemfire.cache.snapshot.SnapshotFilter
 * @since 1.7.0
 */
@SuppressWarnings("unused")
public class ComposableSnapshotFilter implements SnapshotFilter {

	/**
	 * Operator is an enumeration of logical operators (AND, OR) used to compose SnapshotFilters
	 * into a boolean expression.
	 */
	protected enum Operator {
		AND,
		OR;

		public boolean isAnd() {
			return (this == AND);
		}

		public boolean isOr() {
			return (this == OR);
		}

		public boolean operate(boolean leftOperand, boolean rightOperand) {
			return (isAnd() ? (leftOperand && rightOperand) : (leftOperand || rightOperand));
		}
	}

	private final Operator operator;

	private final SnapshotFilter leftOperand;
	private final SnapshotFilter rightOperand;

	/**
	 * Constructs an instance of ComposableSnapshotFilter initialized with a logical operator combining the resulting
	 * boolean expressions from the evaluation of the operands.
	 *
	 * @param leftOperand the left operand in the boolean-based expression.
	 * @param operator the right operand in the boolean-based expression.
	 * @param rightOperand the operator used to combine the resulting boolean expressions
	 * from the evaluation of the operands.
	 * @see ComposableSnapshotFilter.Operator
	 * @see com.gemstone.gemfire.cache.snapshot.SnapshotFilter
	 */
	private ComposableSnapshotFilter(SnapshotFilter leftOperand, Operator operator, SnapshotFilter rightOperand) {
		this.leftOperand = leftOperand;
		this.operator = operator;
		this.rightOperand = rightOperand;
	}

	/* (non-Javadoc) */
	@SuppressWarnings("unchecked")
	static  SnapshotFilter[] nullSafeArray(SnapshotFilter... array) {
		return (array != null ? array : new SnapshotFilter[0]);
	}

	/**
	 * Composes the array of SnapshotFilters into a logical boolean expression using the specified Operator.
	 *
	 * @param  the class type of the SnapshotFilter key.
	 * @param  the class type of the SnapshotFilter value.
	 * @param operator the logical operator used to compose the SnapshotFilters.
	 * @param snapshotFilters the array of SnapshotFilters to compose into a logical boolean expression
	 * using the Operator.
	 * @return a SnapshotFilter implementation composed of the SnapshotFilters using the specified Operator.
	 * @see ComposableSnapshotFilter.Operator
	 * @see com.gemstone.gemfire.cache.snapshot.SnapshotFilter
	 */
	protected static  SnapshotFilter compose(Operator operator, SnapshotFilter... snapshotFilters) {
		SnapshotFilter composedSnapshotFilter = null;

		for (SnapshotFilter snapshotFilter : nullSafeArray(snapshotFilters)) {
			composedSnapshotFilter = (composedSnapshotFilter == null ? snapshotFilter
				: new ComposableSnapshotFilter(snapshotFilter, operator, composedSnapshotFilter));
		}

		return composedSnapshotFilter;
	}

	/**
	 * Composes the array of SnapshotFilters into a logical boolean expression using the AND Operator.
	 *
	 * @param  the class type of the SnapshotFilter key.
	 * @param  the class type of the SnapshotFilter value.
	 * @param snapshotFilters the array of SnapshotFilters to compose into a logical boolean expression
	 * using the AND Operator.
	 * @return a SnapshotFilter implementation composed of the SnapshotFilters using the AND Operator.
	 * @see ComposableSnapshotFilter.Operator#AND
	 * @see com.gemstone.gemfire.cache.snapshot.SnapshotFilter
	 */
	public static  SnapshotFilter and(SnapshotFilter... snapshotFilters) {
		return compose(Operator.AND, snapshotFilters);
	}

	/**
	 * Composes the array of SnapshotFilters into a logical boolean expression using the OR Operator.
	 *
	 * @param  the class type of the SnapshotFilter key.
	 * @param  the class type of the SnapshotFilter value.
	 * @param snapshotFilters the array of SnapshotFilters to compose into a logical boolean expression
	 * using the OR Operator.
	 * @return a SnapshotFilter implementation composed of the SnapshotFilters using the OR Operator.
	 * @see ComposableSnapshotFilter.Operator#OR
	 * @see com.gemstone.gemfire.cache.snapshot.SnapshotFilter
	 */
	public static  SnapshotFilter or(SnapshotFilter... snapshotFilters) {
		return compose(Operator.OR, snapshotFilters);
	}

	/**
	 * Determines whether the following Map Entry is accepted by this composed SnapshotFilter implementation.
	 *
	 * @param entry the Map.Entry to evaluate.
	 * @return a boolean value indicating whether this composed SnapshotFilter accepts the Map Entry.
	 * @see ComposableSnapshotFilter.Operator
	 * @see com.gemstone.gemfire.cache.snapshot.SnapshotFilter#accept(Map.Entry)
	 * @see java.util.Map.Entry
	 */
	@Override
	public boolean accept(final Map.Entry entry) {
		return operator.operate(leftOperand.accept(entry), rightOperand.accept(entry));
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy