Example usage for org.apache.commons.collections15.buffer PriorityBuffer add

List of usage examples for org.apache.commons.collections15.buffer PriorityBuffer add

Introduction

In this page you can find the example usage for org.apache.commons.collections15.buffer PriorityBuffer add.

Prototype

public boolean add(E element) 

Source Link

Document

Adds an element to the buffer.

Usage

From source file:edu.cuny.cat.market.matching.FourHeapShoutEngine.java

/**
 * Insert a shout into a binary heap.//from   w  w w .  j  a v  a  2s  .  co  m
 * 
 * @param heap
 *          The heap to insert into
 * @param shout
 *          The shout to insert
 * 
 */
private static void insertShout(final PriorityBuffer<Shout> heap, final Shout shout)
        throws DuplicateShoutException {
    try {
        heap.add(shout);
    } catch (final IllegalArgumentException e) {
        FourHeapShoutEngine.logger.error(e.toString());
        e.printStackTrace();
        throw new DuplicateShoutException("Duplicate shout: " + shout.toString());
    }
}

From source file:edu.cuny.cat.market.matching.FourHeapShoutEngine.java

/**
 * Unify the shout at the top of the heap with the supplied shout, so that
 * quantity(shout) = quantity(top(heap)). This is achieved by splitting the
 * supplied shout or the shout at the top of the heap.
 * //from  www. ja  va  2  s  .  c o m
 * @param shout
 *          The shout.
 * @param heap
 *          The heap.
 * 
 * @return A reference to the, possibly modified, shout.
 * 
 */
protected static Shout unifyShout(Shout shout, final PriorityBuffer<Shout> heap) {

    final Shout top = heap.get();

    if (shout.getQuantity() > top.getQuantity()) {
        shout = shout.splat(shout.getQuantity() - top.getQuantity());
    } else {
        if (top.getQuantity() > shout.getQuantity()) {
            final Shout remainder = top.split(top.getQuantity() - shout.getQuantity());
            heap.add(remainder);
        }
    }

    return shout;
}

From source file:edu.cuny.cat.market.matching.FourHeapShoutEngine.java

protected int displaceShout(Shout shout, final PriorityBuffer<Shout> from, final PriorityBuffer<Shout> to)
        throws DuplicateShoutException {
    shout = FourHeapShoutEngine.unifyShout(shout, from);
    to.add(from.remove());
    FourHeapShoutEngine.insertShout(from, shout);
    return shout.getQuantity();
}

From source file:edu.cuny.cat.market.matching.FourHeapShoutEngine.java

/**
 * Remove, possibly several, shouts from heap such that quantity(heap) is
 * reduced by the supplied quantity and reinsert the shouts using the standard
 * insertion logic. quantity(heap) is defined as the total quantity of every
 * shout in the heap.//  w  w  w .j a va2s. c  om
 * 
 * @param heap
 *          The heap to remove shouts from.
 * @param quantity
 *          The total quantity to remove.
 */
protected void reinsert(final PriorityBuffer<Shout> heap, int quantity) {

    while (quantity > 0) {

        final Shout top = heap.remove();

        if (top.getQuantity() > quantity) {
            heap.add(top.split(top.getQuantity() - quantity));
        }

        quantity -= top.getQuantity();

        try {
            if (top.isBid()) {
                newBid(top);
            } else {
                newAsk(top);
            }
        } catch (final DuplicateShoutException e) {
            throw new AuctionError("Invalid auction status");
        }
    }
}

From source file:edu.cuny.cat.market.matching.FourHeapShoutEngine.java

public int promoteShout(Shout shout, final PriorityBuffer<Shout> from, final PriorityBuffer<Shout> to,
        final PriorityBuffer<Shout> matched) throws DuplicateShoutException {

    shout = FourHeapShoutEngine.unifyShout(shout, from);
    FourHeapShoutEngine.insertShout(matched, shout);
    to.add(from.remove());
    return shout.getQuantity();
}