All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.palindromicity.syslog.AbstractSyslogParser Maven / Gradle / Ivy

package com.github.palindromicity.syslog;

import com.github.palindromicity.syslog.util.Validate;
import java.io.BufferedReader;
import java.io.Reader;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import org.jspecify.nullness.NullMarked;

/**
 * The type Abstract syslog parser.
 * Abstract base implementation of {@link SyslogParser}
 *
 * @param  the type parameter
 */
@NullMarked
abstract class AbstractSyslogParser implements SyslogParser {

  /**
   * {@link SyslogMessageConsumer} for storing results.
   *
   */
  AbstractSyslogMessageHandler syslogBuilder;

  /**
   * Create a new {@code AbstractSyslogParser}.
   *
   * @param syslogBuilder {@link AbstractSyslogMessageHandler} used gather data
   */
  AbstractSyslogParser(AbstractSyslogMessageHandler syslogBuilder) {
    Validate.notNull(syslogBuilder, "syslogBuilder");
    this.syslogBuilder = syslogBuilder;
  }

  /**
   * Get the {@link SyslogMessageConsumer}.
   *
   * @return {@link SyslogMessageConsumer}
   */
  AbstractSyslogMessageHandler getSyslogBuilder() {
    return syslogBuilder;
  }

  @Override
  public abstract T parseLine(String syslogLine);

  @Override
  public void parseLine(String line, Consumer consumer) {
    Validate.notNull(consumer, "consumer");
    consumer.accept(parseLine(line));
  }

  @Override
  public List parseLines(Reader reader) {
    Validate.notNull(reader, "reader");
    return new BufferedReader(reader).lines().map(this::parseLine).collect(Collectors.toList());
  }

  @Override
  public void parseLines(Reader reader, Consumer consumer) {
    Validate.notNull(reader, "reader");
    Validate.notNull(consumer, "consumer");
    new BufferedReader(reader).lines().map(this::parseLine).forEach(consumer);
  }

  @Override
  public void parseLines(Reader reader, Consumer messageConsumer,
                         BiConsumer errorConsumer) {
    Validate.notNull(reader, "reader");
    Validate.notNull(reader, "messageConsumer");
    Validate.notNull(reader, "errorConsumer");

    List lines = new BufferedReader(reader).lines().collect(Collectors.toList());
    lines.forEach((line) -> {
      try {
        messageConsumer.accept(parseLine(line));
      } catch (Throwable throwable) {
        errorConsumer.accept(line, throwable);
      }
    });
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy