Code Monkey home page Code Monkey logo

generic-object-pool's People

Contributors

bbottema avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Forkers

regbo fisher08

generic-object-pool's Issues

Jacoco - Maven Issue

Hey there, found your projecto in the Stormpot issues. Looks great. However, it looks like whatever version that's currently deployed depends on a jacoco agent to run. The only way I could get it up and going was to either include the agent in every project, or fork your project and remove the build plugin. Here's my fork:

https://github.com/regbo/generic-object-pool-jacoco-removed

The message I am getting is (can't bring up full stack trace at the moment):

Caused by: java.lang.ClassNotFoundException: org.jacoco.agent.rt.internal_c13123e.Offline+

I'm not familiar with jacoco but apparently there is a way to deploy "releases" and when you don't the above error may occur.

Pool mapping

As mentioned from another issue I came here from Stormpot. I have a resource that takes a while to spin up, and it's best done in parallel. It looks like your project does it all on one thread.

To overcome this I created two wrapper classes below. The "map" methods allow me to take something like a CompletableFuture, and make sure all claims from a pool are mapped. That way I can allocate a future and block elsewhere if need be.

If possible, I think adding a method like this to your GenericObjectPool class would be great:

default <U> Pool<U, X> map(Function<? super X, U> mapper);

Here are my wrappers (both use immutables):

@Value.Immutable
@Value.Style(stagedBuilder = true)
public interface Pool<T, X> {

	GenericObjectPool<X> getGenericObjectPool();

	Function<? super X, T> getMapper();

	default <U> Pool<U, X> map(Function<? super X, U> mapper) {
		return ImmutablePool.<U, X>builder().genericObjectPool(getGenericObjectPool()).mapper(mapper).build();
	}

	/**
	 * @param timeout
	 * @return
	 * @throws InterruptedException
	 * @throws IllegalStateException
	 * @see org.bbottema.genericobjectpool.GenericObjectPool#claim(org.bbottema.genericobjectpool.util.Timeout)
	 */
	@SuppressWarnings("resource")
	@Nullable
	default Poolo<T> claim(Timeout timeout) throws InterruptedException, IllegalStateException {
		var po = getGenericObjectPool().claim(timeout);
		if (po == null)
			return null;
		var poolo = new Poolo<X>() {

			@Override
			public PoolableObject<?> getPoolableObject() {
				return po;
			}

			@Override
			public X getAllocatedObject() {
				return po.getAllocatedObject();
			}
		};
		return poolo.map(getMapper());
	}

	public static <X> ImmutablePool<X, X> build(GenericObjectPool<X> genericObjectPool) {
		return build(genericObjectPool, Function.identity());
	}

	public static <T, X> ImmutablePool<T, X> build(GenericObjectPool<X> genericObjectPool,
			Function<? super X, T> mapper) {
		return ImmutablePool.<T, X>builder().genericObjectPool(genericObjectPool).mapper(mapper).build();
	}

	public static abstract class Abs<T, X> implements Pool<T, X> {

		private final GenericObjectPool<X> genericObjectPool;

		public Abs(GenericObjectPool<X> genericObjectPool) {
			super();
			this.genericObjectPool = Objects.requireNonNull(genericObjectPool);
		}

		@Override
		public GenericObjectPool<X> getGenericObjectPool() {
			return genericObjectPool;
		}
	}

	public static class Impl<X> extends Pool.Abs<X, X> {

		public Impl(GenericObjectPool<X> genericObjectPool) {
			super(genericObjectPool);
		}

		@Override
		public Function<? super X, X> getMapper() {
			return Function.identity();
		}

	}

	// *****************************delegates

And

@Value.Immutable
@Value.Style(stagedBuilder = true)
public interface Poolo<T> extends AutoCloseable {

	PoolableObject<?> getPoolableObject();

	/**
	 * @return
	 * @see org.bbottema.genericobjectpool.PoolableObject#getAllocatedObject()
	 */
	@Nullable
	T getAllocatedObject();

	@SuppressWarnings("unchecked")
	default <U> Poolo<U> map(Function<? super T, U> mapper) {
		Objects.requireNonNull(mapper);
		var allocatedObject = getAllocatedObject();
		var allocatedObjectUpdated = mapper.apply(allocatedObject);
		if (allocatedObject == allocatedObjectUpdated)
			return (Poolo<U>) this;
		return ImmutablePoolo.<U>builder()
				.poolableObject(getPoolableObject())
				.allocatedObject(allocatedObjectUpdated)
				.build();
	}

	@Override
	default void close() {
		this.getPoolableObject().release();
	}

	// **********************delegates

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.