<commit_before>/*
 * Copyright 2013 Cloudera Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.kitesdk.morphline.base;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.kitesdk.morphline.api.Command;
import org.kitesdk.morphline.api.CommandBuilder;
import org.kitesdk.morphline.api.MorphlineCompilationException;
import org.kitesdk.morphline.api.MorphlineContext;
import org.kitesdk.morphline.api.Record;
import com.codahale.metrics.Counter;
import com.codahale.metrics.Histogram;
import com.codahale.metrics.Meter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
import com.google.common.base.Preconditions;
import com.typesafe.config.Config;

/**
 * Base class for convenient implementation of {@link Command} classes.
 */
public abstract class AbstractCommand implements Command {
  
  private final Config config;
  private final Command parent;
  private final Command child;
  private final MorphlineContext context;
  private final String name;
  private final Configs configs;
  private final Meter numProcessCallsMeter;
  private final Meter numNotifyCallsMeter;
  
  private static final boolean IS_MEASURING_METRICS = 
      "true".equals(System.getProperty("isMeasuringMetrics", "true"));

  protected final Logger LOG = LoggerFactory.getLogger(getClass());
    
  /**
   * Using the given <code>builder</code>, constructs a command rooted at the given morphline JSON
   * <code>config</code>.
   * 
   * The command will feed records into <code>child</code>. The command will have
   * <code>parent</code> as it's parent. Additional parameters can be passed via the morphline
   * <code>context</code>.
   */
  protected AbstractCommand(CommandBuilder builder, Config config, Command parent, Command child, MorphlineContext context) {
    Preconditions.checkNotNull(builder);
    Preconditions.checkNotNull(config);
    Preconditions.checkNotNull(parent);
    Preconditions.checkNotNull(child);
    Preconditions.checkNotNull(context);
    this.config = config;
    this.parent = parent;
    this.child = child;
    this.context = context;
    Preconditions.checkArgument(builder.getNames().size() > 0);
    this.name = "morphline." + builder.getNames().iterator().next();
    this.configs = new Configs();
    this.numProcessCallsMeter = getMeter(Metrics.NUM_PROCESS_CALLS);
    this.numNotifyCallsMeter = getMeter(Metrics.NUM_NOTIFY_CALLS);
  }
    
  /** Deprecated; will be removed in the next release */
  @Deprecated
  protected AbstractCommand(Config config, Command parent, Command child, MorphlineContext context) {
    Preconditions.checkNotNull(config);
    Preconditions.checkNotNull(parent);
    Preconditions.checkNotNull(child);
    Preconditions.checkNotNull(context);
    this.config = config;
    this.parent = parent;
    this.child = child;
    this.context = context;
    this.name = "morphline." + getShortClassName(getClass());
    this.configs = new Configs();
    this.numProcessCallsMeter = getMeter(Metrics.NUM_PROCESS_CALLS);
    this.numNotifyCallsMeter = getMeter(Metrics.NUM_NOTIFY_CALLS);    
  }
  
  @Override
  public Command getParent() {
    return parent;
  }
  
  /**
   * Returns the child of this command. The parent of a command A is the command B that passes
   * records to A. A is the child of B.
   */
  protected Command getChild() {
    return child;
  }
  
  /** Returns the JSON configuration of this command. */ 
  protected Config getConfig() {
    return config;
  }
  
  /** Returns the context of this command. */ 
  protected MorphlineContext getContext() {
    return context;
  }
  
  /** Returns a helper for convenient access to the JSON configuration of this command. */ 
  protected Configs getConfigs() {
    return configs;
  }
  
  @Override
  public final void notify(Record notification) {
    if (isMeasuringMetrics()) {
      numNotifyCallsMeter.mark();
    }
    beforeNotify(notification);
    doNotify(notification);
  }

  private void beforeNotify(Record notification) {
    if (LOG.isTraceEnabled()) {    
      LOG.trace("beforeNotify: {}", notification);
    } else {
      LOG.debug("beforeNotify()");
    }
  }

  /**
   * Processes the given notification on the control plane of the subtree rooted at this command.
   */
  protected void doNotify(Record notification) {
    getChild().notify(notification);
  }
  
  @Override
  public final boolean process(Record record) {
    if (isMeasuringMetrics()) {
      numProcessCallsMeter.mark();
    }
    beforeProcess(record);
    boolean returnValue = doProcess(record);
    if (!returnValue) {
      LOG.debug("Command failed!");
    }
    return returnValue;<commit_msg>use better variable name<commit_after>/*
 * Copyright 2013 Cloudera Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.kitesdk.morphline.base;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.kitesdk.morphline.api.Command;
import org.kitesdk.morphline.api.CommandBuilder;
import org.kitesdk.morphline.api.MorphlineCompilationException;
import org.kitesdk.morphline.api.MorphlineContext;
import org.kitesdk.morphline.api.Record;
import com.codahale.metrics.Counter;
import com.codahale.metrics.Histogram;
import com.codahale.metrics.Meter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
import com.google.common.base.Preconditions;
import com.typesafe.config.Config;

/**
 * Base class for convenient implementation of {@link Command} classes.
 */
public abstract class AbstractCommand implements Command {
  
  private final Config config;
  private final Command parent;
  private final Command child;
  private final MorphlineContext context;
  private final String name;
  private final Configs configs;
  private final Meter numProcessCallsMeter;
  private final Meter numNotifyCallsMeter;
  
  private static final boolean IS_MEASURING_METRICS = 
      "true".equals(System.getProperty("isMeasuringMetrics", "true"));

  protected final Logger LOG = LoggerFactory.getLogger(getClass());
    
  /**
   * Using the given <code>builder</code>, constructs a command rooted at the given morphline JSON
   * <code>config</code>.
   * 
   * The command will feed records into <code>child</code>. The command will have
   * <code>parent</code> as it's parent. Additional parameters can be passed via the morphline
   * <code>context</code>.
   */
  protected AbstractCommand(CommandBuilder builder, Config config, Command parent, Command child, MorphlineContext context) {
    Preconditions.checkNotNull(builder);
    Preconditions.checkNotNull(config);
    Preconditions.checkNotNull(parent);
    Preconditions.checkNotNull(child);
    Preconditions.checkNotNull(context);
    this.config = config;
    this.parent = parent;
    this.child = child;
    this.context = context;
    Preconditions.checkArgument(builder.getNames().size() > 0);
    this.name = "morphline." + builder.getNames().iterator().next();
    this.configs = new Configs();
    this.numProcessCallsMeter = getMeter(Metrics.NUM_PROCESS_CALLS);
    this.numNotifyCallsMeter = getMeter(Metrics.NUM_NOTIFY_CALLS);
  }
    
  /** Deprecated; will be removed in the next release */
  @Deprecated
  protected AbstractCommand(Config config, Command parent, Command child, MorphlineContext context) {
    Preconditions.checkNotNull(config);
    Preconditions.checkNotNull(parent);
    Preconditions.checkNotNull(child);
    Preconditions.checkNotNull(context);
    this.config = config;
    this.parent = parent;
    this.child = child;
    this.context = context;
    this.name = "morphline." + getShortClassName(getClass());
    this.configs = new Configs();
    this.numProcessCallsMeter = getMeter(Metrics.NUM_PROCESS_CALLS);
    this.numNotifyCallsMeter = getMeter(Metrics.NUM_NOTIFY_CALLS);    
  }
  
  @Override
  public Command getParent() {
    return parent;
  }
  
  /**
   * Returns the child of this command. The parent of a command A is the command B that passes
   * records to A. A is the child of B.
   */
  protected Command getChild() {
    return child;
  }
  
  /** Returns the JSON configuration of this command. */ 
  protected Config getConfig() {
    return config;
  }
  
  /** Returns the context of this command. */ 
  protected MorphlineContext getContext() {
    return context;
  }
  
  /** Returns a helper for convenient access to the JSON configuration of this command. */ 
  protected Configs getConfigs() {
    return configs;
  }
  
  @Override
  public final void notify(Record notification) {
    if (isMeasuringMetrics()) {
      numNotifyCallsMeter.mark();
    }
    beforeNotify(notification);
    doNotify(notification);
  }

  private void beforeNotify(Record notification) {
    if (LOG.isTraceEnabled()) {    
      LOG.trace("beforeNotify: {}", notification);
    } else {
      LOG.debug("beforeNotify()");
    }
  }

  /**
   * Processes the given notification on the control plane of the subtree rooted at this command.
   */
  protected void doNotify(Record notification) {
    getChild().notify(notification);
  }
  
  @Override
  public final boolean process(Record record) {
    if (isMeasuringMetrics()) {
      numProcessCallsMeter.mark();
    }
    beforeProcess(record);
    boolean success = doProcess(record);
    if (!success) {
      LOG.debug("Command failed!");
    }
    return success;
{'fraction_non_alphanumeric': 0.09746918807037351, 'fraction_numerical': 0.0020698090130774295, 'mean_word_length': 3.8406193078324224, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5715994', 'n_tokens_mistral': 3045, 'n_tokens_neox': 3007, 'n_words': 872}