001: /* ====================================================================
002: The Jicarilla Software License
003:
004: Copyright (c) 2003 Leo Simons.
005: All rights reserved.
006:
007: Permission is hereby granted, free of charge, to any person obtaining
008: a copy of this software and associated documentation files (the
009: "Software"), to deal in the Software without restriction, including
010: without limitation the rights to use, copy, modify, merge, publish,
011: distribute, sublicense, and/or sell copies of the Software, and to
012: permit persons to whom the Software is furnished to do so, subject to
013: the following conditions:
014:
015: The above copyright notice and this permission notice shall be
016: included in all copies or substantial portions of the Software.
017:
018: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
019: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
020: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
021: IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
022: CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
023: TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
024: SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
025: ==================================================================== */
026: package org.jicarilla.plumbing;
027:
028: import EDU.oswego.cs.dl.util.concurrent.Channel;
029: import org.jicarilla.plumbing.Stage;
030: import org.jicarilla.lang.Assert;
031:
032: /**
033: * A basic implementation of a {@link org.jicarilla.plumbing.Stage} backed by a {@link Channel}.
034: *
035: * @author <a href="lsimons at jicarilla dot org">Leo Simons</a>
036: * @version $Id: SimpleStage.java,v 1.2 2004/03/23 13:37:58 lsimons Exp $
037: */
038: public class SimpleStage implements Stage {
039: /** The backing {@link Channel}. */
040: protected Channel m_channel;
041:
042: /**
043: * Create a new instance backed by the provided channel.
044: *
045: * @param channel the channel to use as backend
046: */
047: public SimpleStage(final Channel channel) {
048: Assert.assertNotNull(channel);
049: m_channel = channel;
050: }
051:
052: /**
053: * See {@link Stage#put(Object)}.
054: *
055: * @param o the object to add
056: * @throws InterruptedException if the current thread has been
057: * {@link java.lang.Thread#interrupt()}ed
058: */
059: public void put(final Object o) throws InterruptedException {
060: getChannel().put(o);
061: }
062:
063: /**
064: * See {@link Stage#offer(Object,long)}.
065: *
066: * @param o the object to add
067: * @param l how long to try adding the object before returning
068: * @return true if the object was added, false if it wasn't
069: * @throws InterruptedException if the current thread has been
070: * {@link java.lang.Thread#interrupt()}ed
071: */
072: public boolean offer(final Object o, final long l)
073: throws InterruptedException {
074: return getChannel().offer(o, l);
075: }
076:
077: /**
078: * See {@link Stage#take()}.
079: *
080: * @return the message retrieved
081: * @throws InterruptedException if the current thread has been
082: * {@link java.lang.Thread#interrupt()}ed
083: */
084: public Object take() throws InterruptedException {
085: return getChannel().take();
086: }
087:
088: /**
089: * See {@link org.jicarilla.plumbing.Stage#poll(long)}.
090: *
091: * @param l how long to try and fetch before giving up
092: * @return the message retrieved, or null if none was retrieved within
093: * the specified time interval
094: * @throws InterruptedException if the current thread has been
095: * {@link java.lang.Thread#interrupt()}ed
096: */
097: public Object poll(final long l) throws InterruptedException {
098: return getChannel().poll(l);
099: }
100:
101: /**
102: * See {@link org.jicarilla.plumbing.Stage#peek()}.
103: *
104: * @return the message retrieved
105: */
106: public Object peek() {
107: return getChannel().peek();
108: }
109:
110: /**
111: * Get the backing channel.
112: *
113: * @return the backing channel
114: */
115: protected Channel getChannel() {
116: return m_channel;
117: }
118: }
|