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