001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.reef.wake.rx;
020
021import org.apache.reef.wake.metrics.Meter;
022
023import java.util.concurrent.atomic.AtomicBoolean;
024
025/**
026 * An {@link RxStage} that implements metering.
027 *
028 * @param <T> type
029 */
030public abstract class AbstractRxStage<T> implements RxStage<T> {
031
032  protected final AtomicBoolean closed;
033  protected final String name;
034  protected final Meter inMeter;
035  protected final Meter outMeter;
036
037  /**
038   * Constructs an abstact rxstage.
039   *
040   * @param stageName the stage name
041   */
042  public AbstractRxStage(final String stageName) {
043    this.closed = new AtomicBoolean(false);
044    this.name = stageName;
045    this.inMeter = new Meter(stageName + "_in");
046    this.outMeter = new Meter(stageName + "_out");
047  }
048
049  /**
050   * Updates the input meter.
051   * <p>
052   * Stages that want to meter their
053   * input must call this each time an event is input.
054   */
055  protected void beforeOnNext() {
056    inMeter.mark(1);
057  }
058
059  /**
060   * Updates the output meter.
061   * <p>
062   * Stages that want to meter their
063   * output must call this each time an event is output.
064   */
065  protected void afterOnNext() {
066    outMeter.mark(1);
067  }
068
069  /**
070   * Gets the input meter of this stage.
071   *
072   * @return the input meter
073   */
074  public Meter getInMeter() {
075    return inMeter;
076  }
077
078  /**
079   * Gets the output meter of this stage.
080   *
081   * @return the output meter
082   */
083  public Meter getOutMeter() {
084    return outMeter;
085  }
086}