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    
025    import cascading.flow.FlowProcess;
026    import cascading.operation.ValueAssertion;
027    import cascading.operation.ValueAssertionCall;
028    import cascading.tuple.TupleEntry;
029    
030    /**
031     * Class AssertEqualsAll asserts that every value in the argument values {@link cascading.tuple.Tuple} is equal to the value
032     * provided on the constructor.
033     */
034    public class AssertEqualsAll extends BaseAssertion implements ValueAssertion
035      {
036      /** Field value */
037      private Object value;
038    
039      /**
040       * Constructor AssertEqualsAll creates a new AssertEqualsAll instance.
041       *
042       * @param value of type Comparable
043       */
044      @ConstructorProperties({"value"})
045      public AssertEqualsAll( Object value )
046        {
047        super( "argument '%s' value was: %s, not: %s, in tuple: %s" );
048    
049        if( value == null )
050          throw new IllegalArgumentException( "value may not be null" );
051    
052        this.value = value;
053        }
054    
055      public Object getValue()
056        {
057        return value;
058        }
059    
060      @Override
061      public void doAssert( FlowProcess flowProcess, ValueAssertionCall assertionCall )
062        {
063        TupleEntry input = assertionCall.getArguments();
064        int pos = 0;
065    
066        for( Object element : input.getTuple() )
067          {
068          if( !value.equals( element ) )
069            fail( input.getFields().get( pos ), element, value, input.getTuple().print() );
070    
071          pos++;
072          }
073        }
074    
075      @Override
076      public boolean equals( Object object )
077        {
078        if( this == object )
079          return true;
080        if( !( object instanceof AssertEqualsAll ) )
081          return false;
082        if( !super.equals( object ) )
083          return false;
084    
085        AssertEqualsAll that = (AssertEqualsAll) object;
086    
087        if( value != null ? !value.equals( that.value ) : that.value != 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 + ( value != null ? value.hashCode() : 0 );
098        return result;
099        }
100      }