001/*
002 * Copyright (c) 2007-2017 Xplenty, 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
021package cascading.operation.assertion;
022
023import java.beans.ConstructorProperties;
024
025import cascading.flow.FlowProcess;
026import cascading.management.annotation.Property;
027import cascading.management.annotation.PropertyDescription;
028import cascading.management.annotation.Visibility;
029import cascading.operation.ValueAssertion;
030import cascading.operation.ValueAssertionCall;
031import cascading.tuple.TupleEntry;
032
033/**
034 * Class AssertSizeLessThan asserts that the current {@link cascading.tuple.Tuple} in the stream has a size less than (<) the given size.
035 * </p>
036 * On evaluation, {@link cascading.tuple.Tuple#size()} is called (note Tuples may hold {@code null} values).
037 */
038public class AssertSizeLessThan extends BaseAssertion implements ValueAssertion
039  {
040  /** Field size */
041  private final int size;
042
043  /**
044   * Constructor AssertSizeLessThan creates a new AssertSizeLessThan instance.
045   *
046   * @param size of type int
047   */
048  @ConstructorProperties({"size"})
049  public AssertSizeLessThan( int size )
050    {
051    super( "tuple size %s, is more than or equal to: %s, in tuple: %s" );
052    this.size = size;
053    }
054
055  @Property(name = "size", visibility = Visibility.PRIVATE)
056  @PropertyDescription("The maximum tuple size.")
057  public int getSize()
058    {
059    return size;
060    }
061
062  @Override
063  public void doAssert( FlowProcess flowProcess, ValueAssertionCall assertionCall )
064    {
065    TupleEntry input = assertionCall.getArguments();
066
067    if( input.size() >= size )
068      fail( input.size(), size, input.getTuple().print() );
069    }
070
071  @Override
072  public boolean equals( Object object )
073    {
074    if( this == object )
075      return true;
076    if( !( object instanceof AssertSizeLessThan ) )
077      return false;
078    if( !super.equals( object ) )
079      return false;
080
081    AssertSizeLessThan that = (AssertSizeLessThan) object;
082
083    if( size != that.size )
084      return false;
085
086    return true;
087    }
088
089  @Override
090  public int hashCode()
091    {
092    int result = super.hashCode();
093    result = 31 * result + size;
094    return result;
095    }
096  }