<commit_before>/*
 * Copyright 2013 the original author or authors.
 *
 * 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 net.kuujo.vertigo.component.impl;

import java.util.ArrayList;
import java.util.List;

import net.kuujo.vertigo.VertigoException;
import net.kuujo.vertigo.message.MessageId;
import net.kuujo.vertigo.output.OutputCollector;
import net.kuujo.vertigo.output.impl.DefaultOutputCollector;
import net.kuujo.vertigo.message.schema.MessageSchema;
import net.kuujo.vertigo.acker.Acker;
import net.kuujo.vertigo.acker.DefaultAcker;
import net.kuujo.vertigo.component.Component;
import net.kuujo.vertigo.context.ComponentContext;
import net.kuujo.vertigo.context.InstanceContext;
import net.kuujo.vertigo.context.NetworkContext;
import net.kuujo.vertigo.coordinator.heartbeat.HeartbeatEmitter;
import net.kuujo.vertigo.coordinator.heartbeat.impl.DefaultHeartbeatEmitter;
import net.kuujo.vertigo.hooks.ComponentHook;
import net.kuujo.vertigo.hooks.InputHook;
import net.kuujo.vertigo.hooks.OutputHook;
import net.kuujo.vertigo.input.InputCollector;
import net.kuujo.vertigo.input.impl.DefaultInputCollector;

import org.vertx.java.core.AsyncResult;
import org.vertx.java.core.Future;
import org.vertx.java.core.Handler;
import org.vertx.java.core.Vertx;
import org.vertx.java.core.eventbus.EventBus;
import org.vertx.java.core.eventbus.Message;
import org.vertx.java.core.impl.DefaultFutureResult;
import org.vertx.java.core.json.JsonObject;
import org.vertx.java.core.logging.Logger;
import org.vertx.java.core.logging.impl.LoggerFactory;
import org.vertx.java.platform.Container;

/**
 * A base component.
 *
 * @author <NAME>
 */
public abstract class AbstractComponent<T extends Component<T>> implements Component<T> {
  protected final Vertx vertx;
  protected final EventBus eventBus;
  protected final Container container;
  protected final Logger logger;
  protected final InstanceContext<T> context;
  protected final Acker acker;
  protected final String instanceId;
  protected final String address;
  protected final String networkAddress;
  protected final List<String> auditors;
  protected final HeartbeatEmitter heartbeat;
  protected final InputCollector input;
  protected final OutputCollector output;
  protected final List<ComponentHook> hooks = new ArrayList<>();

  private InputHook inputHook = new InputHook() {
    @Override
    public void handleStart(InputCollector subject) {
      // Do nothing. This hook is called elsewhere.
    }
    @Override
    public void handleReceive(MessageId id) {
      for (ComponentHook hook : hooks) {
        hook.handleReceive(id);
      }
    }
    @Override
    public void handleAck(MessageId id) {
      for (ComponentHook hook : hooks) {
        hook.handleAck(id);
      }
    }
    @Override
    public void handleFail(MessageId id) {
      for (ComponentHook hook : hooks) {
        hook.handleFail(id);
      }
    }
    @Override
    public void handleStop(InputCollector subject) {
      // Do nothing. This hook is called elsewhere.
    }
  };

  private OutputHook outputHook = new OutputHook() {
    @Override
    public void handleStart(OutputCollector subject) {
      // Do nothing. This hook is called elsewhere.
    }
    @Override
    public void handleEmit(MessageId id) {
      for (ComponentHook hook : hooks) {
        hook.handleEmit(id);
      }
    }
    @Override
    public void handleAcked(MessageId id) {
      for (ComponentHook hook : hooks) {
        hook.handleAcked(id);
      }
    }
    @Override
    public void handleFailed(MessageId id) {
      for (ComponentHook hook : hooks) {
        hook.handleFailed(id);
      }
    }
    @Override
    public void handleTimeout(MessageId id) {
      for (ComponentHook hook : hooks) {
        hook.handleTimeout(id);
      }
    }
    @Override
    public void handleStop(OutputCollector subject) {
      // Do nothing. This hook is called elsewhere.
    }
  };

  protected AbstractComponent(Vertx vertx, Container container, InstanceContext<T> context) {
    this.vertx = vertx;
    this.eventBus = vertx.eventBus();
    this.container = container;
    this.logger = LoggerFactory.getLogger(context.address());
    this.context = context;
    this.acker = new DefaultAcker(context.address(), eventBus);
    this.instanceId = context.address();
    this.address = context.componentContext().address();
    NetworkContext networkContext = context.componentContext().networkContext();
    networkAddress = networkContext.address();
    List<String> auditorAddresses = networkContext.auditors();
    auditors = new ArrayList<String>();
    for (String auditorAddress : auditorAddresses) {
      auditors.add(auditorAddress);
    }
    heartbeat = new DefaultHeartbeatEmitter(vertx);
    input = new DefaultInputCollector(vertx, container, context, acker);
    output = new DefaultOutputCollector(vertx, container, context, acker);
    for (ComponentHook hook : context.<ComponentContext<?>>componentContext().hooks()) {
      addHook(hook);
    }
  }

  @Override
  public Vertx vertx() {
    return vertx;
  }

  @Override<commit_msg>Remove unused fields from AstractComponent.<commit_after>/*
 * Copyright 2013 the original author or authors.
 *
 * 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 net.kuujo.vertigo.component.impl;

import java.util.ArrayList;
import java.util.List;

import net.kuujo.vertigo.VertigoException;
import net.kuujo.vertigo.message.MessageId;
import net.kuujo.vertigo.output.OutputCollector;
import net.kuujo.vertigo.output.impl.DefaultOutputCollector;
import net.kuujo.vertigo.message.schema.MessageSchema;
import net.kuujo.vertigo.acker.Acker;
import net.kuujo.vertigo.acker.DefaultAcker;
import net.kuujo.vertigo.component.Component;
import net.kuujo.vertigo.context.ComponentContext;
import net.kuujo.vertigo.context.InstanceContext;
import net.kuujo.vertigo.context.NetworkContext;
import net.kuujo.vertigo.coordinator.heartbeat.HeartbeatEmitter;
import net.kuujo.vertigo.coordinator.heartbeat.impl.DefaultHeartbeatEmitter;
import net.kuujo.vertigo.hooks.ComponentHook;
import net.kuujo.vertigo.hooks.InputHook;
import net.kuujo.vertigo.hooks.OutputHook;
import net.kuujo.vertigo.input.InputCollector;
import net.kuujo.vertigo.input.impl.DefaultInputCollector;

import org.vertx.java.core.AsyncResult;
import org.vertx.java.core.Future;
import org.vertx.java.core.Handler;
import org.vertx.java.core.Vertx;
import org.vertx.java.core.eventbus.EventBus;
import org.vertx.java.core.eventbus.Message;
import org.vertx.java.core.impl.DefaultFutureResult;
import org.vertx.java.core.json.JsonObject;
import org.vertx.java.core.logging.Logger;
import org.vertx.java.core.logging.impl.LoggerFactory;
import org.vertx.java.platform.Container;

/**
 * A base component.
 *
 * @author <NAME>
 */
public abstract class AbstractComponent<T extends Component<T>> implements Component<T> {
  protected final Vertx vertx;
  protected final EventBus eventBus;
  protected final Container container;
  protected final Logger logger;
  protected final InstanceContext<T> context;
  protected final Acker acker;
  protected final String instanceId;
  protected final String address;
  protected final String networkAddress;
  protected final HeartbeatEmitter heartbeat;
  protected final InputCollector input;
  protected final OutputCollector output;
  protected final List<ComponentHook> hooks = new ArrayList<>();

  private InputHook inputHook = new InputHook() {
    @Override
    public void handleStart(InputCollector subject) {
      // Do nothing. This hook is called elsewhere.
    }
    @Override
    public void handleReceive(MessageId id) {
      for (ComponentHook hook : hooks) {
        hook.handleReceive(id);
      }
    }
    @Override
    public void handleAck(MessageId id) {
      for (ComponentHook hook : hooks) {
        hook.handleAck(id);
      }
    }
    @Override
    public void handleFail(MessageId id) {
      for (ComponentHook hook : hooks) {
        hook.handleFail(id);
      }
    }
    @Override
    public void handleStop(InputCollector subject) {
      // Do nothing. This hook is called elsewhere.
    }
  };

  private OutputHook outputHook = new OutputHook() {
    @Override
    public void handleStart(OutputCollector subject) {
      // Do nothing. This hook is called elsewhere.
    }
    @Override
    public void handleEmit(MessageId id) {
      for (ComponentHook hook : hooks) {
        hook.handleEmit(id);
      }
    }
    @Override
    public void handleAcked(MessageId id) {
      for (ComponentHook hook : hooks) {
        hook.handleAcked(id);
      }
    }
    @Override
    public void handleFailed(MessageId id) {
      for (ComponentHook hook : hooks) {
        hook.handleFailed(id);
      }
    }
    @Override
    public void handleTimeout(MessageId id) {
      for (ComponentHook hook : hooks) {
        hook.handleTimeout(id);
      }
    }
    @Override
    public void handleStop(OutputCollector subject) {
      // Do nothing. This hook is called elsewhere.
    }
  };

  protected AbstractComponent(Vertx vertx, Container container, InstanceContext<T> context) {
    this.vertx = vertx;
    this.eventBus = vertx.eventBus();
    this.container = container;
    this.logger = LoggerFactory.getLogger(context.address());
    this.context = context;
    this.acker = new DefaultAcker(context.address(), eventBus);
    this.instanceId = context.address();
    this.address = context.componentContext().address();
    NetworkContext networkContext = context.componentContext().networkContext();
    networkAddress = networkContext.address();
    heartbeat = new DefaultHeartbeatEmitter(vertx);
    input = new DefaultInputCollector(vertx, container, context, acker);
    output = new DefaultOutputCollector(vertx, container, context, acker);
    for (ComponentHook hook : context.<ComponentContext<?>>componentContext().hooks()) {
      addHook(hook);
    }
  }

  @Override
  public Vertx vertx() {
    return vertx;
  }

  @Override
{'fraction_non_alphanumeric': 0.09567985447930878, 'fraction_numerical': 0.001455206912232833, 'mean_word_length': 3.7519446845289544, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 26, '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': '6961982', 'n_tokens_mistral': 3176, 'n_tokens_neox': 3164, 'n_words': 822}