Skip to content

Commit

Permalink
Use a null check rather than foreach
Browse files Browse the repository at this point in the history
  • Loading branch information
johnynek committed Sep 27, 2017
1 parent 4b6c927 commit 6de1884
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions scalding-core/src/main/scala/com/twitter/scalding/Stats.scala
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,14 @@ private[scalding] final case class GenericFlowPCounterImpl(fp: FlowProcess[_], s
}

private[scalding] final case class HadoopFlowPCounterImpl(fp: HadoopFlowProcess, statKey: StatKey) extends CounterImpl {
private[this] val counter: Option[Counter] = (for {
// we use a nullable type here for efficiency
private[this] val counter: Counter = (for {
r <- Option(fp.getReporter)
c <- Option(r.getCounter(statKey.group, statKey.counter))
} yield c)
} yield c).orNull

override def increment(amount: Long): Unit = counter.foreach(_.increment(amount))
override def increment(amount: Long): Unit =
if (counter != null) counter.increment(amount) else ()
}

object Stat {
Expand Down

0 comments on commit 6de1884

Please sign in to comment.