KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > graph > query > BufferPipe


1 /*
2   (c) Copyright 2002, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: BufferPipe.java,v 1.7 2005/02/21 11:52:15 andy_seaborne Exp $
5 */

6
7 package com.hp.hpl.jena.graph.query;
8
9 import EDU.oswego.cs.dl.util.concurrent.*;
10 import com.hp.hpl.jena.shared.*;
11
12 import java.util.*;
13
14 /**
15     This class is a pipe between query threads, implemented as a bounded buffer.
16     @author kers
17 */

18 public class BufferPipe implements Pipe
19     {
20     private boolean open = true;
21     private BoundedBuffer buffer = new BoundedBuffer( 5 );
22     private Object JavaDoc pending = null;
23     
24     public static class Finished
25         {
26         protected RuntimeException JavaDoc e;
27         
28         public Finished() {}
29         
30         public Finished( Exception JavaDoc e ) { this.e = new QueryStageException( e ); }
31         
32         public RuntimeException JavaDoc getCause() { return e; }
33         }
34     
35     private static final Finished finished = new Finished();
36      
37     public BufferPipe()
38         { }
39
40     /** put something into the pipe; take care of BoundedBuffer's checked exceptions */
41     private Object JavaDoc fetch()
42         {
43         try { return buffer.take(); }
44         catch (Exception JavaDoc e) { throw new BoundedBufferTakeException( e ); }
45         }
46
47     /** get something from the pipe; take care of BoundedBuffer's checked exceptions */
48     private void putAny( Object JavaDoc d )
49         {
50         try { buffer.put( d ); return; }
51         catch (Exception JavaDoc e) { throw new BoundedBufferPutException( e ); }
52         }
53         
54     public void put( Domain d )
55         { putAny( d ); }
56
57     public void close()
58         { putAny( finished ); }
59     
60     public void close( Exception JavaDoc e )
61         { putAny( new Finished( e ) ); }
62
63     public boolean hasNext()
64         {
65         if (open)
66             {
67             if (pending == null)
68                 {
69                 pending = fetch();
70                 if (pending instanceof Finished)
71                     {
72                     Finished end = (Finished) pending;
73                     RuntimeException JavaDoc cause = end.getCause();
74                     if (cause == null) open = false;
75                     else throw cause;
76                     }
77                 return open;
78                 }
79             else
80                 return true;
81             }
82         else
83             return false;
84         }
85         
86     public Domain get()
87         {
88         if (hasNext() == false) throw new NoSuchElementException();
89         try { return (Domain) pending; } finally { pending = null; }
90         }
91
92     /**
93         Exception to throw if a <code>take</code> throws an exception.
94     */

95     public static class BoundedBufferTakeException extends JenaException
96         { BoundedBufferTakeException( Exception JavaDoc e ) { super( e ); } }
97     
98     /**
99         Exception to throw if a <code>put</code> throws an exception.
100     */

101     public static class BoundedBufferPutException extends JenaException
102         { BoundedBufferPutException( Exception JavaDoc e ) { super( e ); } }
103     }
104 /*
105     (c) Copyright 2002, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
106     All rights reserved.
107
108     Redistribution and use in source and binary forms, with or without
109     modification, are permitted provided that the following conditions
110     are met:
111
112     1. Redistributions of source code must retain the above copyright
113        notice, this list of conditions and the following disclaimer.
114
115     2. Redistributions in binary form must reproduce the above copyright
116        notice, this list of conditions and the following disclaimer in the
117        documentation and/or other materials provided with the distribution.
118
119     3. The name of the author may not be used to endorse or promote products
120        derived from this software without specific prior written permission.
121
122     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
123     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
124     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
125     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
126     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
127     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
128     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
129     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
130     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
131     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
132 */

133
Popular Tags