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

org.dita.dost.util.Pool Maven / Gradle / Ivy

The newest version!
/*
 * This file is part of the DITA Open Toolkit project.
 *
 * Copyright 2020 Jarno Elovirta
 *
 * See the accompanying LICENSE file for applicable license.
 */

package org.dita.dost.util;

import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.function.Supplier;

/**
 *
 * Simple object pool.
 *
 * @since 3.6
 */
public class Pool {

  private final Queue objects;
  private final Supplier create;

  public Pool(Supplier create) {
    this.create = create;
    this.objects = new ConcurrentLinkedQueue<>();
  }

  public T borrowObject() {
    T t;
    if ((t = objects.poll()) == null) {
      t = create.get();
    }
    return t;
  }

  public void returnObject(T object) {
    this.objects.offer(object);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy