001 /* 002 * Copyright (c) 2007-2014 Concurrent, Inc. All Rights Reserved. 003 * 004 * Project and contact information: http://www.cascading.org/ 005 * 006 * This file is part of the Cascading project. 007 * 008 * Licensed under the Apache License, Version 2.0 (the "License"); 009 * you may not use this file except in compliance with the License. 010 * You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, software 015 * distributed under the License is distributed on an "AS IS" BASIS, 016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 017 * See the License for the specific language governing permissions and 018 * limitations under the License. 019 */ 020 021 package cascading.operation.assertion; 022 023 import java.beans.ConstructorProperties; 024 import java.util.Collection; 025 026 import cascading.flow.FlowProcess; 027 import cascading.operation.ValueAssertion; 028 import cascading.operation.ValueAssertionCall; 029 import cascading.tuple.Tuple; 030 import cascading.tuple.Tuples; 031 032 /** 033 * Class AssertEquals asserts the number of constructor values is equal 034 * to the number of argument values {@link Tuple} and each constructor value is {@code .equals()} to its corresponding argument value. 035 */ 036 public class AssertEquals extends BaseAssertion implements ValueAssertion 037 { 038 /** Field values */ 039 private Tuple values; 040 041 /** 042 * Constructor AssertEquals creates a new AssertEquals instance. 043 * 044 * @param values of type Object... 045 */ 046 @ConstructorProperties({"values"}) 047 public AssertEquals( Object... values ) 048 { 049 // set to 1 if null, will fail immediately afterwards 050 super( values == null ? 1 : values.length, "argument tuple: %s was not equal to values: %s" ); 051 052 if( values == null ) 053 throw new IllegalArgumentException( "values may not be null" ); 054 055 if( values.length == 0 ) 056 throw new IllegalArgumentException( "values may not be empty" ); 057 058 this.values = new Tuple( values ); 059 } 060 061 public Collection getValues() 062 { 063 return Tuples.asCollection( values ); 064 } 065 066 @Override 067 public void doAssert( FlowProcess flowProcess, ValueAssertionCall assertionCall ) 068 { 069 Tuple tuple = assertionCall.getArguments().getTuple(); 070 071 if( !tuple.equals( values ) ) 072 fail( tuple.print(), values.print() ); 073 } 074 075 @Override 076 public boolean equals( Object object ) 077 { 078 if( this == object ) 079 return true; 080 if( !( object instanceof AssertEquals ) ) 081 return false; 082 if( !super.equals( object ) ) 083 return false; 084 085 AssertEquals that = (AssertEquals) object; 086 087 if( values != null ? !values.equals( that.values ) : that.values != null ) 088 return false; 089 090 return true; 091 } 092 093 @Override 094 public int hashCode() 095 { 096 int result = super.hashCode(); 097 result = 31 * result + ( values != null ? values.hashCode() : 0 ); 098 return result; 099 } 100 }