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

com.theoryinpractise.halbuilder5.Links Maven / Gradle / Ivy

package com.theoryinpractise.halbuilder5;

import io.vavr.Function1;
import io.vavr.collection.Map;
import io.vavr.control.Either;
import io.vavr.control.Option;
import java.lang.Boolean;
import java.lang.Object;
import java.lang.Override;
import java.lang.String;
import java.util.function.Supplier;

public final class Links {
  private static final Link.Cases relGetter = Links.cases((rel, href, templated) -> rel, (rel, href, templated, properties) -> rel);

  private static final Link.Cases hrefGetter = Links.cases((rel, href, templated) -> href, (rel, href, templated, properties) -> href);

  private static final Link.Cases templatedGetter = Links.cases((rel, href, templated) -> templated, (rel, href, templated, properties) -> templated);

  private static final Link.Cases>> propertiesGetter = Links.cases((rel, href, templated) -> Option.none(),
  (rel, href, templated, properties) -> Option.some(properties));

  private Links() {
  }

  public static Link create(String rel, String href, String... properties) {
    return Link.create(rel, href, properties);
  }

  public static Link create(String rel, String href, Map properties) {
    return Link.create(rel, href, properties);
  }

  public static Link create(String rel, String href, java.util.Map properties) {
    return Link.create(rel, href, properties);
  }

  public static  Link.Cases cases(SimpleMapper simple, FullMapper full) {
    return new LambdaCases<>(simple, full);
  }

  static Link simple0(String rel, String href, Boolean templated) {
    if (rel == null) throw new NullPointerException("rel must not be null");
    if (href == null) throw new NullPointerException("href must not be null");
    if (templated == null) throw new NullPointerException("templated must not be null");
    return new Simple(rel, href, templated);
  }

  static Link full0(String rel, String href, Boolean templated, Map properties) {
    if (rel == null) throw new NullPointerException("rel must not be null");
    if (href == null) throw new NullPointerException("href must not be null");
    if (templated == null) throw new NullPointerException("templated must not be null");
    if (properties == null) throw new NullPointerException("properties must not be null");
    return new Full(rel, href, templated, properties);
  }

  public static Link lazy(Supplier link) {
    return new Lazy(link);
  }

  public static CasesMatchers.TotalMatcher_Simple cases() {
    return CasesMatchers.totalMatcher_Simple;
  }

  public static CaseOfMatchers.TotalMatcher_Simple caseOf(Link link) {
    return new CaseOfMatchers.TotalMatcher_Simple(link);
  }

  public static String getRel(Link link) {
    return link.match(relGetter);
  }

  public static String getHref(Link link) {
    return link.match(hrefGetter);
  }

  public static Boolean getTemplated(Link link) {
    return link.match(templatedGetter);
  }

  public static Option> getProperties(Link link) {
    return link.match(propertiesGetter);
  }

  static Function1 setRel0(String newRel) {
    return modRel0(__ -> newRel);
  }

  static Function1 modRel0(Function1 relMod) {
    Link.Cases cases = com.theoryinpractise.halbuilder5.Links.cases((rel, href, templated) -> simple0(relMod.apply(rel), href, templated),
        (rel, href, templated, properties) -> full0(relMod.apply(rel), href, templated, properties));
    return link -> link.match(cases);
  }

  static Function1 setHref0(String newHref) {
    return modHref0(__ -> newHref);
  }

  static Function1 modHref0(Function1 hrefMod) {
    Link.Cases cases = com.theoryinpractise.halbuilder5.Links.cases((rel, href, templated) -> simple0(rel, hrefMod.apply(href), templated),
        (rel, href, templated, properties) -> full0(rel, hrefMod.apply(href), templated, properties));
    return link -> link.match(cases);
  }

  static Function1 setTemplated0(Boolean newTemplated) {
    return modTemplated0(__ -> newTemplated);
  }

  static Function1 modTemplated0(Function1 templatedMod) {
    Link.Cases cases = com.theoryinpractise.halbuilder5.Links.cases((rel, href, templated) -> simple0(rel, href, templatedMod.apply(templated)),
        (rel, href, templated, properties) -> full0(rel, href, templatedMod.apply(templated), properties));
    return link -> link.match(cases);
  }

  static Function1 setProperties0(Map newProperties) {
    return modProperties0(__ -> newProperties);
  }

  static Function1 modProperties0(Function1, Map> propertiesMod) {
    Link.Cases cases = com.theoryinpractise.halbuilder5.Links.cases(Links::simple0,
        (rel, href, templated, properties) -> full0(rel, href, templated, propertiesMod.apply(properties)));
    return link -> link.match(cases);
  }

  public interface SimpleMapper {
    R simple(String rel, String href, Boolean templated);
  }

  public interface FullMapper {
    R full(String rel, String href, Boolean templated, Map properties);
  }

  private static final class LambdaCases implements Link.Cases {
    private final SimpleMapper simple;

    private final FullMapper full;

    LambdaCases(SimpleMapper simple, FullMapper full) {
      this.simple = simple;
      this.full = full;
    }

    @Override
    public R simple(String rel, String href, Boolean templated) {
      return this.simple.simple(rel, href, templated);
    }

    @Override
    public R full(String rel, String href, Boolean templated, Map properties) {
      return this.full.full(rel, href, templated, properties);
    }
  }

  private static final class Simple extends Link {
    private final String rel;

    private final String href;

    private final Boolean templated;

    Simple(String rel, String href, Boolean templated) {
      this.rel = rel;
      this.href = href;
      this.templated = templated;
    }

    @Override
    public  R match(Link.Cases cases) {
      return cases.simple(this.rel, this.href, this.templated);
    }

    @Override
    public boolean equals(Object o) {
      return (o instanceof Link) && ((Link) o).match(Links.cases((rel, href, templated) -> this.rel.equals(rel) && this.href.equals(href) && this.templated.equals(templated),
          (rel, href, templated, properties) -> false));
    }

    @Override
    public int hashCode() {
      return ((23 + this.rel.hashCode()) * 23 + this.href.hashCode()) * 23 + this.templated.hashCode();
    }
  }

  private static final class Full extends Link {
    private final String rel;

    private final String href;

    private final Boolean templated;

    private final Map properties;

    Full(String rel, String href, Boolean templated, Map properties) {
      this.rel = rel;
      this.href = href;
      this.templated = templated;
      this.properties = properties;
    }

    @Override
    public  R match(Link.Cases cases) {
      return cases.full(this.rel, this.href, this.templated, this.properties);
    }

    @Override
    public boolean equals(Object o) {
      return (o instanceof Link) && ((Link) o).match(Links.cases((rel, href, templated) -> false,
          (rel, href, templated, properties) -> this.rel.equals(rel) && this.href.equals(href) && this.templated.equals(templated) && this.properties.equals(properties)));
    }

    @Override
    public int hashCode() {
      return (((29 + this.rel.hashCode()) * 29 + this.href.hashCode()) * 29 + this.templated.hashCode()) * 29 + this.properties.hashCode();
    }
  }

  private static final class Lazy extends Link {
    private volatile Supplier expression;

    private Link evaluation;

    Lazy(Supplier link) {
      this.expression = link;
    }

    private synchronized Link _evaluate() {
      Supplier e = expression;
      if (e != null) {
        evaluation = e.get();
        expression = null;
      }
      return evaluation;
    }

    @Override
    public  R match(Link.Cases cases) {
      return (this.expression == null ? this.evaluation : _evaluate()).match(cases);
    }

    @Override
    public boolean equals(Object o) {
      return (this.expression == null ? this.evaluation : _evaluate()).equals(o);
    }

    @Override
    public int hashCode() {
      return (this.expression == null ? this.evaluation : _evaluate()).hashCode();
    }
  }

  public static class CasesMatchers {
    private static final TotalMatcher_Simple totalMatcher_Simple = new TotalMatcher_Simple();

    private CasesMatchers() {
    }

    public static final class TotalMatcher_Simple {
      TotalMatcher_Simple() {
      }

      public final  TotalMatcher_Full simple(SimpleMapper simple) {
        return new TotalMatcher_Full<>(simple);
      }

      public final  TotalMatcher_Full simple_(R r) {
        return this.simple((rel, href, templated) -> r);
      }

      public final  PartialMatcher full(FullMapper full) {
        return new PartialMatcher<>(null, full);
      }

      public final  PartialMatcher full_(R r) {
        return this.full((rel, href, templated, properties) -> r);
      }
    }

    public static final class TotalMatcher_Full extends PartialMatcher {
      TotalMatcher_Full(SimpleMapper simple) {
        super(simple, null);
      }

      public final Function1 full(FullMapper full) {
        Link.Cases cases = Links.cases(((PartialMatcher) this).simple, full);
        return link -> link.match(cases);
      }

      public final Function1 full_(R r) {
        return this.full((rel, href, templated, properties) -> r);
      }
    }

    public static class PartialMatcher {
      private final SimpleMapper simple;

      private final FullMapper full;

      PartialMatcher(SimpleMapper simple, FullMapper full) {
        this.simple = simple;
        this.full = full;
      }

      public final Function1 otherwise(Supplier otherwise) {
        Link.Cases cases = Links.cases(this.simple != null ? this.simple : (rel, href, templated) -> otherwise.get(),
            this.full != null ? this.full : (rel, href, templated, properties) -> otherwise.get());
        return link -> link.match(cases);
      }

      public final Function1 otherwise_(R r) {
        return this.otherwise(() -> r);
      }

      public final Function1> otherwiseNone() {
        Link.Cases> cases = Links.cases((this.simple != null) ? (rel, href, templated) -> Option.some(this.simple.simple(rel, href, templated))
            : (rel, href, templated) -> Option.none(),
            (this.full != null) ? (rel, href, templated, properties) -> Option.some(this.full.full(rel, href, templated, properties))
            : (rel, href, templated, properties) -> Option.none());
        return link -> link.match(cases);
      }

      public final  Function1> otherwiseLeft(Supplier left) {
        Link.Cases> cases = Links.cases((this.simple != null) ? (rel, href, templated) -> Either.right(this.simple.simple(rel, href, templated))
            : (rel, href, templated) -> Either.left(left.get()),
            (this.full != null) ? (rel, href, templated, properties) -> Either.right(this.full.full(rel, href, templated, properties))
            : (rel, href, templated, properties) -> Either.left(left.get()));
        return link -> link.match(cases);
      }

      public final  Function1> otherwiseLeft_(L left) {
        return this.otherwiseLeft(() -> left);
      }
    }
  }

  public static class CaseOfMatchers {
    private CaseOfMatchers() {
    }

    public static final class TotalMatcher_Simple {
      private final Link _link;

      TotalMatcher_Simple(Link _link) {
        this._link = _link;
      }

      public final  TotalMatcher_Full simple(SimpleMapper simple) {
        return new TotalMatcher_Full<>(this._link, simple);
      }

      public final  TotalMatcher_Full simple_(R r) {
        return this.simple((rel, href, templated) -> r);
      }

      public final  PartialMatcher full(FullMapper full) {
        return new PartialMatcher<>(this._link, null, full);
      }

      public final  PartialMatcher full_(R r) {
        return this.full((rel, href, templated, properties) -> r);
      }
    }

    public static final class TotalMatcher_Full extends PartialMatcher {
      TotalMatcher_Full(Link _link, SimpleMapper simple) {
        super(_link, simple, null);
      }

      public final R full(FullMapper full) {
        Link.Cases cases = Links.cases(((PartialMatcher) this).simple, full);
        return ((PartialMatcher) this)._link.match(cases);
      }

      public final R full_(R r) {
        return this.full((rel, href, templated, properties) -> r);
      }
    }

    public static class PartialMatcher {
      private final Link _link;

      private final SimpleMapper simple;

      private final FullMapper full;

      PartialMatcher(Link _link, SimpleMapper simple, FullMapper full) {
        this._link = _link;
        this.simple = simple;
        this.full = full;
      }

      public final R otherwise(Supplier otherwise) {
        Link.Cases cases = Links.cases(this.simple != null ? this.simple : (rel, href, templated) -> otherwise.get(),
            this.full != null ? this.full : (rel, href, templated, properties) -> otherwise.get());
        return this._link.match(cases);
      }

      public final R otherwise_(R r) {
        return this.otherwise(() -> r);
      }

      public final Option otherwiseNone() {
        Link.Cases> cases = Links.cases((this.simple != null) ? (rel, href, templated) -> Option.some(this.simple.simple(rel, href, templated))
            : (rel, href, templated) -> Option.none(),
            (this.full != null) ? (rel, href, templated, properties) -> Option.some(this.full.full(rel, href, templated, properties))
            : (rel, href, templated, properties) -> Option.none());
        return this._link.match(cases);
      }

      public final  Either otherwiseLeft(Supplier left) {
        Link.Cases> cases = Links.cases((this.simple != null) ? (rel, href, templated) -> Either.right(this.simple.simple(rel, href, templated))
            : (rel, href, templated) -> Either.left(left.get()),
            (this.full != null) ? (rel, href, templated, properties) -> Either.right(this.full.full(rel, href, templated, properties))
            : (rel, href, templated, properties) -> Either.left(left.get()));
        return this._link.match(cases);
      }

      public final  Either otherwiseLeft_(L left) {
        return this.otherwiseLeft(() -> left);
      }
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy