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

com.sirolf2009.util.retry.SimpleRetryStrategy Maven / Gradle / Ivy

The newest version!
package com.sirolf2009.util.retry;

import com.sirolf2009.util.retry.RetryResult;
import com.sirolf2009.util.retry.RetryStrategy;
import java.util.ArrayList;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import org.eclipse.xtend.lib.annotations.Data;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.eclipse.xtext.xbase.lib.ExclusiveRange;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.Pure;
import org.eclipse.xtext.xbase.lib.util.ToStringBuilder;

@Data
@SuppressWarnings("all")
public class SimpleRetryStrategy implements RetryStrategy {
  private final int retryCount;
  
  @Override
  public  RetryResult run(final Supplier supplier) {
    RetryResult _xblockexpression = null;
    {
      final ArrayList errors = new ArrayList(this.retryCount);
      final Function> _function = new Function>() {
        @Override
        public RetryResult apply(final Integer it) {
          try {
            Optional _of = Optional.of(supplier.get());
            return new RetryResult(_of, (it).intValue(), errors);
          } catch (final Throwable _t) {
            if (_t instanceof Exception) {
              final Exception e = (Exception)_t;
              errors.add(e);
              return null;
            } else {
              throw Exceptions.sneakyThrow(_t);
            }
          }
        }
      };
      final Predicate> _function_1 = new Predicate>() {
        @Override
        public boolean test(final RetryResult it) {
          return (it != null);
        }
      };
      Optional> _findFirst = IterableExtensions.toList(new ExclusiveRange(0, this.retryCount, true)).stream().>map(_function).filter(_function_1).findFirst();
      Optional _empty = Optional.empty();
      RetryResult _retryResult = new RetryResult(_empty, this.retryCount, errors);
      _xblockexpression = _findFirst.orElse(_retryResult);
    }
    return _xblockexpression;
  }
  
  public SimpleRetryStrategy(final int retryCount) {
    super();
    this.retryCount = retryCount;
  }
  
  @Override
  @Pure
  public int hashCode() {
    return 31 * 1 + this.retryCount;
  }
  
  @Override
  @Pure
  public boolean equals(final Object obj) {
    if (this == obj)
      return true;
    if (obj == null)
      return false;
    if (getClass() != obj.getClass())
      return false;
    SimpleRetryStrategy other = (SimpleRetryStrategy) obj;
    if (other.retryCount != this.retryCount)
      return false;
    return true;
  }
  
  @Override
  @Pure
  public String toString() {
    ToStringBuilder b = new ToStringBuilder(this);
    b.add("retryCount", this.retryCount);
    return b.toString();
  }
  
  @Pure
  public int getRetryCount() {
    return this.retryCount;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy