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

org.approvaltests.writers.DefaultApprovalWriterFactory Maven / Gradle / Ivy

There is a newer version: 24.9.0
Show newest version
package org.approvaltests.writers;

import com.spun.util.Tuple;
import org.lambda.functions.Function1;
import org.lambda.query.Queryable;

import java.io.File;
import java.sql.ResultSet;

public class DefaultApprovalWriterFactory
{
  private static Queryable, ApprovalWriterFactory>> factories = new Queryable<>();
  static
  {
    addDefault(o -> o instanceof ResultSet, (c, o) -> new ResultSetApprovalWriter((ResultSet) c));
    addDefault(o -> o instanceof File, (c, o) -> new FileApprovalWriter((File) c));
    addDefault(o -> o instanceof ResultSet, (c, o) -> new ResultSetApprovalWriter((ResultSet) c));
  }
  public static ApprovalWriterFactory getDefaultFactory()
  {
    return (c, o) -> {
      Tuple, ApprovalWriterFactory> first = factories.first(t -> t.getFirst().call(c));
      if (first != null)
      {
        return first.getSecond().create(c, o);
      }
      else
      {
        return ApprovalTextWriter.getFactory().create(c, o);
      }
    };
  }
  public static void addDefault(Function1 isValidFor, ApprovalWriterFactory factory)
  {
    factories.add(0, new Tuple<>(isValidFor, factory));
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy