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

eu.stratosphere.core.testing.RecordEqualer Maven / Gradle / Ivy

The newest version!
/***********************************************************************************************************************
 *
 * Copyright (C) 2010-2013 by the Stratosphere project (http://stratosphere.eu)
 *
 * 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 eu.stratosphere.core.testing;

import eu.stratosphere.types.Record;
import eu.stratosphere.types.Value;

/**
 * Checks whether two Stratosphere {@link Record}s are equal according to the given schema.
 */
public class RecordEqualer implements Equaler {
	private final Class[] schema;

	/**
	 * Initializes RecordEqualer with the given schema.
	 */
	public RecordEqualer(final Class firstFieldType, final Class... otherFieldTypes) {
		this.schema = SchemaUtils.combineSchema(firstFieldType, otherFieldTypes);
	}

	/**
	 * Initializes RecordEqualer with the given schema.
	 */
	public RecordEqualer(final Class[] schema) {
		this.schema = schema;
	}

	/*
	 * (non-Javadoc)
	 * @see eu.stratosphere.core.testing.Equaler#equal(java.lang.Object, java.lang.Object)
	 */
	@Override
	public boolean equal(final Record object1, final Record object2) {
		return recordsEqual(object1, object2, this.schema);
	}

	/**
	 * Returns the schema.
	 * 
	 * @return the schema
	 */
	public Class[] getSchema() {
		return this.schema;
	}

	/**
	 * Convenience method to check for equality on the given records with the provided schema.
	 * 
	 * @return true if both records hold the same values
	 */
	public static boolean recordsEqual(final Record object1, final Record object2,
			final Class[] schema) {
		if (object1.getNumFields() != schema.length)
			return false;

		for (int index = 0; index < schema.length; index++)
			if (!SafeEqualer.get().equal(object1.getField(index, schema[index]),
				object2.getField(index, schema[index])))
				return false;
		return true;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy