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

br.com.objectos.way.pojo.plugin.InvalidateMethodContribution Maven / Gradle / Ivy

There is a newer version: 0.6.0
Show newest version
/*
 * Copyright 2016 Objectos, Fábrica de Software LTDA.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package br.com.objectos.way.pojo.plugin;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Supplier;

import br.com.objectos.way.code.MethodInfoOverrideWriter;

import com.squareup.javapoet.CodeBlock;

/**
 * @author [email protected] (Marcio Endo)
 */
public abstract class InvalidateMethodContribution {

  private static final InvalidateMethodContribution EMPTY = new Empty();

  private InvalidateMethodContribution() {
  }

  public static Builder builder() {
    return new Builder();
  }

  public static InvalidateMethodContribution code(CodeBlock code) {
    return new Code(code);
  }

  public static InvalidateMethodContribution empty() {
    return EMPTY;
  }

  public static InvalidateMethodContribution statement(String format, Object... args) {
    Objects.requireNonNull(format);
    return new Statement(format, args);
  }

  abstract void accept(MethodInfoOverrideWriter writer);

  public static class Builder implements BuilderWhen {

    private final List list = new ArrayList<>();

    private Builder() {
    }

    @Override
    public Builder add(Supplier supplier) {
      Objects.requireNonNull(supplier);
      InvalidateMethodContribution contribution = supplier.get();
      Objects.requireNonNull(contribution, "Provided supplier returned null.");
      list.add(contribution);
      return this;
    }

    @Override
    public Builder addStatement(String format, Object... args) {
      Objects.requireNonNull(format);
      list.add(new Statement(format, args));
      return this;
    }

    public InvalidateMethodContribution build() {
      return new ListContribution(list);
    }

    public BuilderWhen when(boolean test) {
      return test ? this : new WhenFalse();
    }

    private class WhenFalse implements BuilderWhen {

      @Override
      public Builder add(Supplier supplier) {
        return Builder.this;
      }

      @Override
      public Builder addStatement(String format, Object... args) {
        return Builder.this;
      }

    }

  }

  public static interface BuilderWhen {

    Builder add(Supplier supplier);

    Builder addStatement(String format, Object... args);

  }

  private static class Code extends InvalidateMethodContribution {

    private final CodeBlock code;

    public Code(CodeBlock code) {
      this.code = code;
    }

    @Override
    void accept(MethodInfoOverrideWriter writer) {
      writer.addCode(code);
    }

  }

  private static class Empty extends InvalidateMethodContribution {

    @Override
    void accept(MethodInfoOverrideWriter writer) {
    }

  }

  private static class ListContribution extends InvalidateMethodContribution {

    private final List list;

    public ListContribution(List list) {
      this.list = list;
    }

    @Override
    void accept(MethodInfoOverrideWriter writer) {
      for (InvalidateMethodContribution contribution : list) {
        contribution.accept(writer);
      }
    }

  }

  private static class Statement extends InvalidateMethodContribution {

    private final String format;
    private final Object[] args;

    public Statement(String format, Object[] args) {
      this.format = format;
      this.args = args;
    }

    @Override
    void accept(MethodInfoOverrideWriter writer) {
      writer.addStatement(format, args);
    }

  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy