Example usage for org.apache.commons.collections Buffer add

List of usage examples for org.apache.commons.collections Buffer add

Introduction

In this page you can find the example usage for org.apache.commons.collections Buffer add.

Prototype

boolean add(E e);

Source Link

Document

Ensures that this collection contains the specified element (optional operation).

Usage

From source file:edu.illinois.enforcemop.examples.apache.collections.TestBoundedBuffer.java

/**@TO-DO How do we handle timed events? */
@Test/*from   ww w . ja  va2  s  .c  om*/
//  @Schedule(name = "addToFullBufferRemoveViaIterator", sequence = "[beforeAdd:afterAdd]@main->beforeRemove@removeThread")
public void testAddToFullBufferRemoveViaIterator() {
    final Buffer bounded = BoundedBuffer.decorate(new UnboundedFifoBuffer(), 1, 500);
    bounded.add("Hello");
    new NonDelayedIteratorRemove(bounded, 100, "removeThread").start();
    /* @Event("beforeAdd")*/
    bounded.add("World");
    /* @Event("afterAdd")*/
    assertEquals(1, bounded.size());
    assertEquals("World", bounded.get());

}

From source file:edu.illinois.enforcemop.examples.apache.collections.TestBoundedBuffer.java

@Test
//  @Schedule(name = "default", sequence = "[beforeAdd:afterAdd]@main->beforeRemove@removeThread")
public void testAddAllToFullBufferRemoveViaIterator() {
    final Buffer bounded = BoundedBuffer.decorate(new UnboundedFifoBuffer(), 2, 500);
    bounded.add("Hello");
    bounded.add("World");
    new NonDelayedIteratorRemove(bounded, 100, 2, "removeThread").start();
    /* @Event("beforeAdd")*/
    bounded.addAll(Arrays.asList(new String[] { "Foo", "Bar" }));
    /* @Event("afterAdd")*/
    assertEquals(2, bounded.size());//from   ww w .  j a v a  2 s.c om
    assertEquals("Foo", bounded.remove());
    assertEquals("Bar", bounded.remove());
}

From source file:edu.illinois.enforcemop.examples.apache.collections.TestBoundedBuffer.java

@Test
// @Schedule(name = "addToFullBufferWithTimeout", sequence = "[beforeAdd:afterAdd]@main->beforeRemove@removeThread,afterRemove@removeThread->afterAdd@main")
public void testAddToFullBufferWithTimeout() {
    final Buffer bounded = BoundedBuffer.decorate(new UnboundedFifoBuffer(), 1, 500);
    bounded.add("Hello");
    new NonDelayedRemove(bounded, 100, "removeThread").start();
    /* @Event("beforeAdd")*/
    bounded.add("World");
    /* @Event("afterAdd")*/
    assertEquals(1, bounded.size());/*from   w w w.j av  a2  s  .  c o m*/
    assertEquals("World", bounded.get());
    try {
        bounded.add("!");
        fail();
    } catch (BufferOverflowException e) {
    }
}

From source file:com.stainlesscode.mediapipeline.demux.EventAwareSimpleDemultiplexer.java

@SuppressWarnings("unchecked")
protected void demultiplexerLoop() throws NoSuchElementException, IllegalStateException, Exception {
    if (LogUtil.isDebugEnabled())
        LogUtil.debug("In demultiplexerLoop()");

    int result = -1;

    if (packet == null) {
        packet = (IPacket) engineRuntime.getPacketPool().borrowObject();

        engineRuntime.getContainerLock().lock();
        try {//from   w ww  .  jav a2 s.  c  o m
            result = engineRuntime.getContainer().readNextPacket(packet);
            if (LogUtil.isDebugEnabled())
                LogUtil.debug(
                        "read packet " + packet.getTimeStamp() + " for stream " + packet.getStreamIndex());
        } finally {
            engineRuntime.getContainerLock().unlock();
        }
    } else {
        result = 0;
    }

    if (result >= 0) {
        if (packet != null) {
            if (LogUtil.isDebugEnabled())
                LogUtil.debug("extracting packet to buffer " + packet.getStreamIndex());
            Buffer destinationBuffer = engineRuntime.getStreamToBufferMap().get(packet.getStreamIndex());

            if (destinationBuffer == null) {
                packet = null;
            } else {
                try {
                    destinationBuffer.add(packet.copyReference());
                    packet = null;
                } catch (BufferOverflowException e) {
                    Thread.yield();
                }
            }
        }
    } else {
        LogUtil.error("result is " + result);
        if (result == -32) {
            // System.out.println("CLIP_END detected, firing event");
            // engineRuntime.getPlayer().fireMediaPlayerEvent(
            // new MediaPlayerEvent(engineRuntime.getPlayer(),
            // MediaPlayerEvent.CLIP_END));
            setMarkedForDeath(true);
        }
        IError error = IError.make(result);
        LogUtil.error("ERROR: " + error.getDescription());
    }
}

From source file:com.stainlesscode.mediapipeline.demux.SimpleDemultiplexer.java

@Override
public void handlePacket(IPacket packet) throws DemultiplexerException {
    if (LogUtil.isDebugEnabled())
        LogUtil.debug("extracting packet to buffer " + packet.getStreamIndex());

    Buffer destinationBuffer = engineRuntime.getStreamToBufferMap().get(packet.getStreamIndex());

    if (destinationBuffer == null) {
        LogUtil.debug("No destination configured for packet stream " + packet.getStreamIndex());
        returnBorrowed(packet);/*  w  w  w  .  ja  v a2 s . c  o m*/
    } else {
        try {
            destinationBuffer.add(packet.copyReference());
            // packet = null;
        } catch (BufferOverflowException e) {
            System.err.println(e.getMessage());
            return;
        }
    }
}

From source file:com.stainlesscode.mediapipeline.demux.MultispeedDemultiplexer.java

@SuppressWarnings("unchecked")
public void run() {
    while (!isMarkedForDeath()) {
        if (engineRuntime.isPaused())
            continue;

        if (LogUtil.isDebugEnabled())
            LogUtil.debug("In demultiplexerLoop()");

        int result = -1;

        if (packet == null) {
            try {
                packet = (IPacket) engineRuntime.getPacketPool().borrowObject();
            } catch (NoSuchElementException e) {
                LogUtil.error("Unable to borrow packet for decode");
                continue;
            } catch (IllegalStateException e) {
                LogUtil.error("Unable to borrow packet for decode");
                continue;
            } catch (Exception e) {
                LogUtil.error("Unable to borrow packet for decode");
                continue;
            }//from  ww  w .j av  a2  s.  co m

            engineRuntime.getContainerLock().lock();
            try {
                result = engineRuntime.getContainer().readNextPacket(packet);
                if (LogUtil.isDebugEnabled())
                    LogUtil.debug(
                            "read packet " + packet.getTimeStamp() + " for stream " + packet.getStreamIndex());
            } finally {
                engineRuntime.getContainerLock().unlock();
            }
        } else {
            result = 0;
        }

        if (result >= 0) {
            if (packet != null) {
                if (LogUtil.isDebugEnabled())
                    LogUtil.debug("extracting packet to buffer " + packet.getStreamIndex());
                Buffer destinationBuffer = engineRuntime.getStreamToBufferMap().get(packet.getStreamIndex());

                int modulo = 0;
                speed = new Double(engineRuntime.getPlaySpeed()).intValue();
                if (speed > 0) {
                    modulo = (multispeedPacketCounter++) % speed;
                    if (LogUtil.isDebugEnabled()) {
                        LogUtil.debug("speed factor is " + speed);
                        LogUtil.debug("modulo is " + modulo);
                    }
                }

                if (destinationBuffer != null && modulo == 0) {
                    try {
                        destinationBuffer.add(packet.copyReference());
                        packet = null;
                    } catch (BufferOverflowException e) {
                        return;
                    }
                } else {
                    packet = null;
                    return;
                }
            }
        } else {
            LogUtil.error("result is " + result);
            if (result == -32) {
                // System.out.println("CLIP_END detected, firing event");
                // engineRuntime.getPlayer().fireMediaPlayerEvent(
                // new MediaPlayerEvent(engineRuntime.getPlayer(),
                // MediaPlayerEvent.CLIP_END));
                setMarkedForDeath(true);
            }
            IError error = IError.make(result);
            LogUtil.error("ERROR: " + error.getDescription());
        }
    }

    LogUtil.info("thread shutting down gracefully");
}

From source file:edu.illinois.imunit.examples.apache.collections.TestBlockingBuffer.java

/**@TO-DO  Can't specify thread is ended, and or relationship between schedules*/
@Test/*  w  w  w  . j a v  a  2s. c  o  m*/
@Schedules({
        @Schedule(name = "BlockedRemoveWithAdd", value = "[beforeRemove: afterRemove]@readThread1->beforeFirstAdd@main,"
                + "[beforeRemove: afterRemove]@readThread2->beforeFirstAdd@main") })
public void testBlockedRemoveWithAdd() throws InterruptedException {
    Buffer blockingBuffer = BlockingBuffer.decorate(new MyBuffer());
    Object obj = new Object();

    // run methods will remove and compare -- must wait for add
    Thread thread1 = new ReadThread(blockingBuffer, obj, null, "remove", "BlockedRemoveWithAdd", "readThread1");
    Thread thread2 = new ReadThread(blockingBuffer, obj, null, "remove", "BlockedRemoveWithAdd", "readThread2");
    thread1.start();
    thread2.start();

    // give hungry read threads ample time to hang
    try {
        // Thread.sleep(100);
    } catch (Exception e) {
        e.printStackTrace();
    }

    fireEvent("beforeFirstAdd");
    blockingBuffer.add(obj);

    // allow notified threads to complete 
    try {
        // Thread.sleep(100);
    } catch (Exception e) {
        e.printStackTrace();
    }

    // There should be one thread waiting.
    //assertTrue( "BlockedRemoveWithAdd", thread1.isAlive() ^ thread2.isAlive() );
    fireEvent("beforeSecondAdd");
    blockingBuffer.add(obj);

    // allow notified thread to complete 
    try {
        // Thread.sleep(100);
    } catch (Exception e) {
        e.printStackTrace();
    }

    // There should not be any threads waiting.
    thread1.join();
    thread2.join();
    //assertFalse( "BlockedRemoveWithAdd", thread1.isAlive() || thread2.isAlive() );
    //if( thread1.isAlive() || thread2.isAlive() ) {
    //fail( "Live thread(s) when both should be dead." );
    //}
}

From source file:edu.illinois.imunit.examples.apache.collections.TestBlockingBuffer.java

/**
 * Tests {@link BlockingBuffer#get()} in combination with {@link BlockingBuffer#add(Object)} using multiple read
 * threads./*from   ww  w.  ja  va2  s. c o  m*/
 * <p/>
 * Two read threads should block on an empty buffer until one object is added then both threads should complete.
 */
@Test
@Schedules({ @Schedule(name = "BlockedGetWithAdd", value = "[beforeGet:afterGet]@readThread1->beforeAdd@main,"
        + "[beforeGet:afterGet]@readThread2->beforeAdd@main," + "afterGet@readThread1->afterAdd@main,"
        + "afterGet@readThread2->afterAdd@main") })
public void testBlockedGetWithAdd() throws InterruptedException {
    Buffer blockingBuffer = BlockingBuffer.decorate(new MyBuffer());
    Object obj = new Object();

    // run methods will get and compare -- must wait for add
    Thread thread1 = new ReadThread(blockingBuffer, obj, "BlockedGetWithAdd", "readThread1");
    Thread thread2 = new ReadThread(blockingBuffer, obj, "BlockedGetWithAdd", "readThread2");
    thread1.start();
    thread2.start();

    // give hungry read threads ample time to hang
    try {
        // Thread.sleep(100);
    } catch (Exception e) {
        e.printStackTrace();
    }

    // notifyAll should allow both read threads to complete
    fireEvent("beforeAdd");
    blockingBuffer.add(obj);

    // allow notified threads to complete 
    // Thread.sleep(100);
    fireEvent("afterAdd");

    // There should not be any threads waiting.
    // if(thread1.isAlive() || thread2.isAlive()) {
    //  fail("Live thread(s) when both should be dead.");
    // }
}

From source file:edu.illinois.enforcemop.examples.apache.collections.TestBlockingBuffer.java

/**@TO-DO  Can't specify thread is ended, and or relationship between schedules*/
@Test// w  w  w . j  a  va2  s  . c o m
// @Schedules({
//   @Schedule(name = "BlockedRemoveWithAdd", sequence = "[beforeRemove: afterRemove]@readThread1->beforeFirstAdd@main," + 
//       "[beforeRemove: afterRemove]@readThread2->beforeFirstAdd@main") })
public void testBlockedRemoveWithAdd() throws InterruptedException {
    Buffer blockingBuffer = BlockingBuffer.decorate(new MyBuffer());
    Object obj = new Object();

    // run methods will remove and compare -- must wait for add
    Thread thread1 = new ReadThread(blockingBuffer, obj, null, "remove", "BlockedRemoveWithAdd", "readThread1");
    Thread thread2 = new ReadThread(blockingBuffer, obj, null, "remove", "BlockedRemoveWithAdd", "readThread2");
    thread1.start();
    thread2.start();

    // give hungry read threads ample time to hang
    try {
        Thread.sleep(100);
    } catch (Exception e) {
        e.printStackTrace();
    }

    /* @Event("beforeFirstAdd")*/
    blockingBuffer.add(obj);

    // allow notified threads to complete 
    try {
        Thread.sleep(100);
    } catch (Exception e) {
        e.printStackTrace();
    }

    // There should be one thread waiting.
    //assertTrue( "BlockedRemoveWithAdd", thread1.isAlive() ^ thread2.isAlive() );
    /* @Event("beforeSecondAdd")*/
    blockingBuffer.add(obj);

    // allow notified thread to complete 
    try {
        Thread.sleep(100);
    } catch (Exception e) {
        e.printStackTrace();
    }

    // There should not be any threads waiting.
    thread1.join();
    thread2.join();
    //assertFalse( "BlockedRemoveWithAdd", thread1.isAlive() || thread2.isAlive() );
    //if( thread1.isAlive() || thread2.isAlive() ) {
    //fail( "Live thread(s) when both should be dead." );
    //}
}

From source file:edu.illinois.enforcemop.examples.apache.collections.TestBlockingBuffer.java

/**
 * Tests {@link BlockingBuffer#get()} in combination with {@link BlockingBuffer#add(Object)} using multiple read
 * threads./*from  www . j av a 2 s. c  o m*/
 * <p/>
 * Two read threads should block on an empty buffer until one object is added then both threads should complete.
 */
@Test
// @Schedules({
//   @Schedule(name = "BlockedGetWithAdd", sequence = "[beforeGet:afterGet]@readThread1->beforeAdd@main," +
//       "[beforeGet:afterGet]@readThread2->beforeAdd@main," + 
//       "afterGet@readThread1->afterAdd@main," +"afterGet@readThread2->afterAdd@main") })
public void testBlockedGetWithAdd() throws InterruptedException {
    Buffer blockingBuffer = BlockingBuffer.decorate(new MyBuffer());
    Object obj = new Object();

    // run methods will get and compare -- must wait for add
    Thread thread1 = new ReadThread(blockingBuffer, obj, "BlockedGetWithAdd", "readThread1");
    Thread thread2 = new ReadThread(blockingBuffer, obj, "BlockedGetWithAdd", "readThread2");
    thread1.start();
    thread2.start();

    // Give hungry read threads ample time to hang
    try {
        Thread.sleep(100);
    } catch (Exception e) {
        e.printStackTrace();
    }

    // notifyAll should allow both read threads to complete
    /* @Event("beforeAdd")*/
    blockingBuffer.add(obj);

    // allow notified threads to complete 
    Thread.sleep(100);
    /* @Event("afterAdd")*/

    // There should not be any threads waiting.
    // if(thread1.isAlive() || thread2.isAlive()) {
    //  fail("Live thread(s) when both should be dead.");
    // }
}