001/*
002 * Copyright (c) 2016-2017 Chris K Wensel <chris@wensel.net>. All Rights Reserved.
003 * Copyright (c) 2007-2017 Xplenty, Inc. All Rights Reserved.
004 *
005 * Project and contact information: http://www.cascading.org/
006 *
007 * This file is part of the Cascading project.
008 *
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *     http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 */
021
022package cascading.operation.assertion;
023
024import java.beans.ConstructorProperties;
025
026import cascading.flow.FlowProcess;
027import cascading.management.annotation.Property;
028import cascading.management.annotation.PropertyDescription;
029import cascading.management.annotation.Visibility;
030import cascading.operation.ValueAssertion;
031import cascading.operation.ValueAssertionCall;
032import cascading.tuple.TupleEntry;
033
034/**
035 * Class AssertSizeLessThan asserts that the current {@link cascading.tuple.Tuple} in the stream has a size less than (&lt;) the given size.
036 * <p>
037 * On evaluation, {@link cascading.tuple.Tuple#size()} is called (note Tuples may hold {@code null} values).
038 */
039public class AssertSizeLessThan extends BaseAssertion implements ValueAssertion
040  {
041  /** Field size */
042  private final int size;
043
044  /**
045   * Constructor AssertSizeLessThan creates a new AssertSizeLessThan instance.
046   *
047   * @param size of type int
048   */
049  @ConstructorProperties({"size"})
050  public AssertSizeLessThan( int size )
051    {
052    super( "tuple size %s, is more than or equal to: %s, in tuple: %s" );
053    this.size = size;
054    }
055
056  @Property(name = "size", visibility = Visibility.PRIVATE)
057  @PropertyDescription("The maximum tuple size.")
058  public int getSize()
059    {
060    return size;
061    }
062
063  @Override
064  public void doAssert( FlowProcess flowProcess, ValueAssertionCall assertionCall )
065    {
066    TupleEntry input = assertionCall.getArguments();
067
068    if( input.size() >= size )
069      fail( input.size(), size, input.getTuple().print() );
070    }
071
072  @Override
073  public boolean equals( Object object )
074    {
075    if( this == object )
076      return true;
077    if( !( object instanceof AssertSizeLessThan ) )
078      return false;
079    if( !super.equals( object ) )
080      return false;
081
082    AssertSizeLessThan that = (AssertSizeLessThan) object;
083
084    if( size != that.size )
085      return false;
086
087    return true;
088    }
089
090  @Override
091  public int hashCode()
092    {
093    int result = super.hashCode();
094    result = 31 * result + size;
095    return result;
096    }
097  }