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

com.monitorjbl.json.Match Maven / Gradle / Ivy

package com.monitorjbl.json;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.BiFunction;

public class Match {
  private final Set includes = new HashSet<>();
  private final Set excludes = new HashSet<>();
  private final Map> transforms = new HashMap<>();

  Match() {

  }

  /**
   * Mark fields for inclusion during serialization.
   *
   * @param fields The fields to include
   * @return Match
   */
  public Match include(String... fields) {
    if(fields != null) {
      includes.addAll(Arrays.asList(fields));
    }
    return this;
  }

  /**
   * Mark fields for exclusion during serialization.
   *
   * @param fields The fields to exclude
   * @return Match
   */
  public Match exclude(String... fields) {
    if(fields != null) {
      excludes.addAll(Arrays.asList(fields));
    }
    return this;
  }

  /**
   * Mark a field for transformation during serialization.
   *
   * @param field       The fields to include
   * @param transformer The function to transform the field. Will be provided with the whole object and the field.
   * @param          The object being serialized
   * @param          The field being serialized
   * @param          The value of the field to serialize
   * @return Match
   */
  @SuppressWarnings("unchecked")
  public  Match transform(String field, BiFunction transformer) {
    transforms.put(field, (BiFunction) transformer);
    return this;
  }

  Set getIncludes() {
    return includes;
  }

  Set getExcludes() {
    return excludes;
  }

  Map> getTransforms() {
    return transforms;
  }

  public static Match match() {
    return new Match();
  }

  @Override
  public String toString() {
    return "Match{" +
        "includes=" + includes +
        ", excludes=" + excludes +
        ", transforms=" + transforms +
        '}';
  }

  @Override
  public boolean equals(Object o) {
    if(this == o) return true;
    if(o == null || getClass() != o.getClass()) return false;

    Match match = (Match) o;

    if(includes != null ? !includes.equals(match.includes) : match.includes != null) return false;
    if(excludes != null ? !excludes.equals(match.excludes) : match.excludes != null) return false;
    return transforms != null ? transforms.equals(match.transforms) : match.transforms == null;
  }

  @Override
  public int hashCode() {
    int result = includes != null ? includes.hashCode() : 0;
    result = 31 * result + (excludes != null ? excludes.hashCode() : 0);
    result = 31 * result + (transforms != null ? transforms.hashCode() : 0);
    return result;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy