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

com.simiacryptus.lang.StaticResourcePool Maven / Gradle / Ivy

There is a newer version: 2.1.0
Show newest version
/*
 * Copyright (c) 2019 by Andrew Charneski.
 *
 * The author licenses this file to you 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 com.simiacryptus.lang;

import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;

public class StaticResourcePool {

  @javax.annotation.Nonnull
  private final List all;
  private final java.util.concurrent.LinkedBlockingQueue pool = new java.util.concurrent.LinkedBlockingQueue<>();

  public StaticResourcePool(@javax.annotation.Nonnull final List items) {
    super();
    this.all = Collections.unmodifiableList(new ArrayList<>(items));
    pool.addAll(getAll());
  }

  public void apply(@Nonnull final Consumer f) {
    apply(f, x -> true, false);
  }

  public void apply(@javax.annotation.Nonnull final Consumer f, final Predicate filter, final boolean exclusive) {
    T poll = get(filter, exclusive);
    try {
      f.accept(poll);
    } finally {
      this.pool.add(poll);
    }
  }

  @Nonnull
  private T get(Predicate filter, final boolean exclusive) {
    ArrayList sampled = new ArrayList<>();
    try {
      T poll = this.pool.poll();
      while (null != poll) {
        if (filter.test(poll)) {
          return poll;
        } else {
          sampled.add(poll);
          poll = this.pool.poll();
        }
      }
    } finally {
      pool.addAll(sampled);
    }
    try {
      while (true) {
        final T poll;
        poll = this.pool.poll(5, TimeUnit.MINUTES);
        if (null == poll) throw new RuntimeException("Timeout awaiting item from pool");
        if (exclusive && !filter.test(poll)) {
          this.pool.add(poll);
          Thread.sleep(0);
        } else {
          return poll;
        }
      }
    } catch (@javax.annotation.Nonnull final InterruptedException e) {
      throw new RuntimeException(e);
    }
  }

  @javax.annotation.Nonnull
  public List getAll() {
    return all;
  }

  public  U run(@Nonnull final Function f) {
    return run(f, x -> true, false);
  }

  public  U run(@javax.annotation.Nonnull final Function f, final Predicate filter, final boolean exclusive) {
    if (all.isEmpty()) throw new IllegalStateException();
    T poll = get(filter, exclusive);
    try {
      return f.apply(poll);
    } finally {
      this.pool.add(poll);
    }
  }

  public int size() {
    return getAll().size();
  }
}