    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.state;
    022
    023    import java.beans.ConstructorProperties;
    024
    025    import cascading.flow.FlowProcess;
    026    import cascading.operation.BaseOperation;
    027    import cascading.operation.Filter;
    028    import cascading.operation.FilterCall;
    029
    030    /**
    031     * Class Counter is a {@link Filter} that increments a given {@link Enum} counter by 1 or by the given {@code increment} value.
    032     * <p/>
    033     * Internally, the {@link #isRemove(cascading.flow.FlowProcess, cascading.operation.FilterCall)} method calls
    034     * {@link FlowProcess#increment(Enum, long)}.
    035     * <p/>
    036     * No {@link cascading.tuple.Tuple} instances are ever discarded.
    037     *
    038     * @see FlowProcess
    039     * @see Filter
    040     */
    041    public class Counter extends BaseOperation implements Filter
    042      {
    043      private final Enum counterEnum;
    044      private final String groupString;
    045      private final String counterString;
    046      private final int increment;
    047
    048      /**
    049       * Constructor Counter creates a new Counter instance.
    050       *
    051       * @param counter of type Enum
    052       */
    053      @ConstructorProperties({"counter"})
    054      public Counter( Enum counter )
    055        {
    056        this( counter, 1 );
    057        }
    058
    059      /**
    060       * Constructor Counter creates a new Counter instance.
    061       *
    062       * @param counter   of type Enum
    063       * @param increment of type int
    064       */
    065      @ConstructorProperties({"counter", "increment"})
    066      public Counter( Enum counter, int increment )
    067        {
    068        this.counterEnum = counter;
    069        this.groupString = null;
    070        this.counterString = null;
    071        this.increment = increment;
    072        }
    073
    074      @ConstructorProperties({"group", "counter"})
    075      public Counter( String group, String counter )
    076        {
    077        this( group, counter, 1 );
    078        }
    079
    080      @ConstructorProperties({"group", "counter", "increment"})
    081      public Counter( String group, String counter, int increment )
    082        {
    083        this.counterEnum = null;
    084        this.groupString = group;
    085        this.counterString = counter;
    086        this.increment = increment;
    087        }
    088
    089      public Enum getCounterEnum()
    090        {
    091        return counterEnum;
    092        }
    093
    094      public String getGroupString()
    095        {
    096        return groupString;
    097        }
    098
    099      public String getCounterString()
    100        {
    101        return counterString;
    102        }
    103
    104      public int getIncrement()
    105        {
    106        return increment;
    107        }
    108
    109      @Override
    110      public boolean isRemove( FlowProcess flowProcess, FilterCall filterCall )
    111        {
    112        if( counterEnum != null )
    113          flowProcess.increment( counterEnum, increment );
    114        else
    115          flowProcess.increment( groupString, counterString, increment );
    116
    117        return false;
    118        }
    119
    120      @Override
    121      public boolean equals( Object object )
    122        {
    123        if( this == object )
    124          return true;
    125        if( !( object instanceof Counter ) )
    126          return false;
    127        if( !super.equals( object ) )
    128          return false;
    129
    130        Counter counter = (Counter) object;
    131
    132        if( increment != counter.increment )
    133          return false;
    134        if( counterEnum != null ? !counterEnum.equals( counter.counterEnum ) : counter.counterEnum != null )
    135          return false;
    136        if( counterString != null ? !counterString.equals( counter.counterString ) : counter.counterString != null )
    137          return false;
    138        if( groupString != null ? !groupString.equals( counter.groupString ) : counter.groupString != null )
    139          return false;
    140
    141        return true;
    142        }
    143
    144      @Override
    145      public int hashCode()
    146        {
    147        int result = super.hashCode();
    148        result = 31 * result + ( counterEnum != null ? counterEnum.hashCode() : 0 );
    149        result = 31 * result + ( groupString != null ? groupString.hashCode() : 0 );
    150        result = 31 * result + ( counterString != null ? counterString.hashCode() : 0 );
    151        result = 31 * result + increment;
    152        return result;
    153        }
    154      }