1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-06-26 15:13:21 +00:00
CC-Tweaked/src/main/java/dan200/computercraft/core/apis/http/ResourceQueue.java

57 lines
1.3 KiB
Java
Raw Normal View History

/*
* This file is part of ComputerCraft - http://www.computercraft.info
* Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
* Send enquiries to dratcliffe@gmail.com
*/
package dan200.computercraft.core.apis.http;
import java.util.ArrayDeque;
import java.util.function.IntSupplier;
import java.util.function.Supplier;
/**
* A {@link ResourceGroup} which will queue items when the group at capacity.
*/
public class ResourceQueue<T extends Resource<T>> extends ResourceGroup<T>
{
private final ArrayDeque<Supplier<T>> pending = new ArrayDeque<>();
public ResourceQueue( IntSupplier limit )
{
super( limit );
}
public ResourceQueue()
{
}
public synchronized void shutdown()
{
super.shutdown();
pending.clear();
}
public synchronized boolean queue( Supplier<T> resource )
{
if( !active ) return false;
if( !super.queue( resource ) ) pending.add( resource );
return true;
}
public synchronized void release( T resource )
{
super.release( resource );
if( !active ) return;
int limit = this.limit.getAsInt();
if( limit <= 0 || resources.size() < limit )
{
Supplier<T> next = pending.poll();
if( next != null ) resources.add( next.get() );
}
}
}