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

org.infinispan.commons.configuration.attributes.AttributeSerializer Maven / Gradle / Ivy

There is a newer version: 15.1.0.Dev04
Show newest version
package org.infinispan.commons.configuration.attributes;

import java.util.Arrays;
import java.util.Collection;
import java.util.stream.Collectors;

import org.infinispan.commons.configuration.io.ConfigurationWriter;

/**
 * AttributeSerializer.
 *
 * @since 10.0
 */
public interface AttributeSerializer {
   AttributeSerializer DEFAULT = (writer, name, value) -> {
      if (Boolean.class == value.getClass()) {
         writer.writeAttribute(name, (Boolean) value);
      } else {
         writer.writeAttribute(name, value.toString());
      }
   };
   AttributeSerializer SECRET = (writer, name, value) -> {
      if (writer.clearTextSecrets()) {
         writer.writeAttribute(name, value);
      } else {
         writer.writeAttribute(name, "***");
      }
   };
   AttributeSerializer STRING_ARRAY = (writer, name, value) -> writer.writeAttribute(name, Arrays.asList(value));
   AttributeSerializer> STRING_COLLECTION = ConfigurationWriter::writeAttribute;
   AttributeSerializer>> ENUM_COLLECTION = (writer, name, value) -> writer.writeAttribute(name, value.stream().map(Enum::toString).collect(Collectors.toList()));
   AttributeSerializer>> ENUM_SET = (writer, name, value) -> writer.writeAttribute(name, value.stream().map(Enum::toString).collect(Collectors.toSet()));
   AttributeSerializer INSTANCE_CLASS_NAME = ((writer, name, value) -> writer.writeAttribute(name, value.getClass().getName()));
   AttributeSerializer CLASS_NAME = ((writer, name, value) -> writer.writeAttribute(name, value.getName()));

   void serialize(ConfigurationWriter writer, String name, T value);

   default boolean defer() {
      return false;
   }
}