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

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

There is a newer version: 0.6.0
Show newest version
/*
 * Copyright 2014-2015 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 static java.util.Objects.requireNonNull;

import java.lang.annotation.Annotation;

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

  Configuration configuration;

  private boolean first;

  protected AbstractPlugin() {
  }

  @Override
  public final synchronized void configure(Configuration configuration) {
    if (this.configuration != null) {
      throw new IllegalStateException("Re-entry is not allowed.");
    }

    this.configuration = requireNonNull(configuration, "builder");
    try {
      first = true;
      configure();
    } finally {
      this.configuration = null;
    }
  }

  @Override
  public void onConfigure() {
  }

  @Override
  public void onStart() {
  }

  protected final boolean always() {
    return true;
  }

  protected abstract void configure();

  protected final void execute(ArtifactAction action) {
    first = false;
    configuration.execute(action);
  }

  protected final void execute(BuilderPropertyAction action) {
    first = false;
    configuration.execute(action);
  }

  protected final void execute(MethodAction action) {
    first = false;
    configuration.execute(action);
  }

  protected final void execute(PojoAction action) {
    first = false;
    configuration.execute(action);
  }

  protected final void executeWhen(PojoPredicate condition) {
    if (!first) {
      throw new IllegalStateException("executeWhen() must be the first method called.");
    }
    synchronized (this) {
      configuration = configuration.executeWhen(condition);
      first = false;
    }
  }

  protected final Condition hasAnnotation(Class type) {
    return Condition.hasAnnotation(type);
  }

  protected final Condition hasAnnotationAnnotatedWith(Class type) {
    return Condition.hasAnnotationAnnotatedWith(type);
  }

  @Deprecated
  protected final Condition hasInterface(Class type) {
    return Condition.hasInterface(type);
  }

  protected final Condition hasName(String name) {
    return Condition.hasName(name);
  }

  protected final Condition hasTypeAnnotatedWith(Class type) {
    return Condition.hasTypeAnnotatedWith(type);
  }

  protected final Condition instanceOf(Class type) {
    return Condition.instanceOf(type);
  }

  protected final MethodPredicate method(MethodCondition condition) {
    return new MethodPredicate(condition);
  }

  protected final PojoPredicate pojo(PojoCondition condition) {
    return new PojoPredicate(condition);
  }

  protected final PropertyPredicate property(PropertyCondition condition) {
    return new PropertyPredicate(condition);
  }

  protected final WhenMethod when(MethodPredicate predicate) {
    first = false;
    return configuration.when(predicate);
  }

  protected final WhenPojo when(PojoPredicate predicate) {
    first = false;
    return configuration.when(predicate);
  }

  protected final WhenProperty when(PropertyPredicate predicate) {
    first = false;
    return configuration.when(predicate);
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy