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 AssertSizeEquals asserts that the current {@link cascading.tuple.Tuple} in the stream is exactly the given size.
032     * </p>
033     * On evaluation, {@link cascading.tuple.Tuple#size()} is called (note Tuples may hold {@code null} values).
034     */
035    public class AssertSizeEquals extends BaseAssertion implements ValueAssertion
036      {
037      /** Field size */
038      private final int size;
039    
040      /**
041       * Constructor AssertSizeEquals creates a new AssertSizeEquals instance.
042       *
043       * @param size of type int
044       */
045      @ConstructorProperties({"size"})
046      public AssertSizeEquals( int size )
047        {
048        super( "tuple size %s, is not equal to: %s, in tuple: %s" );
049        this.size = size;
050        }
051    
052      public int getSize()
053        {
054        return size;
055        }
056    
057      @Override
058      public void doAssert( FlowProcess flowProcess, ValueAssertionCall assertionCall )
059        {
060        TupleEntry input = assertionCall.getArguments();
061    
062        if( input.size() != size )
063          fail( input.size(), size, input.getTuple().print() );
064        }
065    
066      @Override
067      public boolean equals( Object object )
068        {
069        if( this == object )
070          return true;
071        if( !( object instanceof AssertSizeEquals ) )
072          return false;
073        if( !super.equals( object ) )
074          return false;
075    
076        AssertSizeEquals that = (AssertSizeEquals) object;
077    
078        if( size != that.size )
079          return false;
080    
081        return true;
082        }
083    
084      @Override
085      public int hashCode()
086        {
087        int result = super.hashCode();
088        result = 31 * result + size;
089        return result;
090        }
091      }