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

com.github.wglanzer.annosave.api.AnnoSave Maven / Gradle / Ivy

The newest version!
package com.github.wglanzer.annosave.api;

import com.github.wglanzer.annosave.impl.*;
import com.github.wglanzer.annosave.impl.converter.ConverterFactory;
import com.github.wglanzer.annosave.impl.structure.SAnnotationContainer;
import org.apache.commons.io.output.NullOutputStream;
import org.jetbrains.annotations.NotNull;

import java.io.*;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Contains all common API-Methods for AnnoSave
 *
 * @author W.Glanzer, 13.09.2017
 */
@SuppressWarnings({"unused", "UnusedReturnValue"})
public class AnnoSave
{

  /**
   * Converts all annotations for a class-object (including subclasses) into a list
   * Those descriptions are made with JSON.
   *
   * @param pClass Class which should be read
   * @return a list of containers representing all annotations inside pClass
   */
  @NotNull
  public static List write(@NotNull Class pClass)
  {
    return write(pClass, new NullOutputStream());
  }

  /**
   * Writes all annotations for a class-object (including subclasses) to an outputStream.
   * Those descriptions are made with JSON.
   *
   * @param pClass        Class which should be read and written to stream
   * @param pOutputStream Consuming stream. Closed after return.
   * @return a container representing all annotations inside pClass
   */
  @NotNull
  public static List write(@NotNull Class pClass, @NotNull OutputStream pOutputStream)
  {
    List containers = ConverterFactory.createDefaultConverter().convert(pClass);
    new AnnoWriter(pOutputStream).write(containers);
    return Collections.unmodifiableList(containers);
  }

  /**
   * Converts all annotations of an object.
   * Those descriptions are made with JSON.
   *
   * @param pRoot      Root-Object which should be read
   * @param pConverter Converter which describes, how the given object is disassembled to an serializable object
   * @return a container representing all annotations inside pClass
   */
  @NotNull
  public static  List write(T pRoot, IAnnoSaveConverter pConverter)
  {
    return write(pRoot, pConverter, new NullOutputStream());
  }

  /**
   * Writes all annotations for a object to an outputStream.
   * Those descriptions are made with JSON.
   *
   * @param pRoot         Root-Object which should be read and written to stream
   * @param pConverter    Converter which describes, how the given object is disassembled to an serializable object
   * @param pOutputStream Consuming stream. Closed after return.
   * @return a container representing all annotations inside pClass
   */
  @NotNull
  public static  List write(T pRoot, IAnnoSaveConverter pConverter, @NotNull OutputStream pOutputStream)
  {
    List containers = ConverterFactory.createConverter(pConverter).convert(pRoot);
    new AnnoWriter(pOutputStream).write(containers);
    return Collections.unmodifiableList(containers);
  }

  /**
   * Reads an inputStream and converts the JSON back to an IAnnotationContainer
   *
   * @param pInputStream Stream which should be read completely. Closed afterwards.
   * @return the resulting container
   */
  @NotNull
  public static List read(@NotNull InputStream pInputStream)
  {
    return Collections.unmodifiableList(new AnnoReader(pInputStream).read());
  }

  /**
   * Reads an inputStream and converts the JSON back to an IAnnotationContainer
   *
   * @param pInputStream Stream which should be read completely. Closed afterwards.
   * @return the resulting containers as map with name as key
   */
  @NotNull
  public static Map readAsMap(@NotNull InputStream pInputStream)
  {
    List list = read(pInputStream);
    return list.stream().collect(Collectors.toMap(IAnnotationContainer::getName, pE -> pE));
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy