001 package com.croftsoft.core.util.queue; 002 003 import com.croftsoft.core.lang.NullArgumentException; 004 import com.croftsoft.core.lang.lifecycle.Lifecycle; 005 import com.croftsoft.core.util.consumer.Consumer; 006 import com.croftsoft.core.util.loop.Loopable; 007 import com.croftsoft.core.util.loop.Looper; 008 009 /********************************************************************* 010 * Pulls objects out of the queue in a separate thread. 011 * 012 * @version 013 * 2003-05-27 014 * @since 015 * 1998-11-23 016 * @author 017 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 018 *********************************************************************/ 019 020 public final class QueuePuller 021 implements Lifecycle 022 ////////////////////////////////////////////////////////////////////// 023 ////////////////////////////////////////////////////////////////////// 024 { 025 026 private final Queue queue; 027 028 private final Consumer consumer; 029 030 private final Looper looper; 031 032 ////////////////////////////////////////////////////////////////////// 033 ////////////////////////////////////////////////////////////////////// 034 035 public QueuePuller ( 036 Queue queue, 037 Consumer consumer ) 038 ////////////////////////////////////////////////////////////////////// 039 { 040 NullArgumentException.check ( this.queue = queue ); 041 042 NullArgumentException.check ( this.consumer = consumer ); 043 044 looper = new Looper ( 045 new Loopable ( ) 046 { 047 public boolean loop ( ) 048 { 049 return QueuePuller.this.loop ( ); 050 } 051 } ); 052 } 053 054 ////////////////////////////////////////////////////////////////////// 055 ////////////////////////////////////////////////////////////////////// 056 057 public void init ( ) 058 ////////////////////////////////////////////////////////////////////// 059 { 060 looper.init ( ); 061 } 062 063 public void start ( ) 064 ////////////////////////////////////////////////////////////////////// 065 { 066 looper.start ( ); 067 } 068 069 public void stop ( ) 070 ////////////////////////////////////////////////////////////////////// 071 { 072 looper.stop ( ); 073 } 074 075 public void destroy ( ) 076 ////////////////////////////////////////////////////////////////////// 077 { 078 looper.destroy ( ); 079 } 080 081 ////////////////////////////////////////////////////////////////////// 082 ////////////////////////////////////////////////////////////////////// 083 084 public boolean append ( Object o ) 085 ////////////////////////////////////////////////////////////////////// 086 { 087 return queue.append ( o ); 088 } 089 090 public Object replace ( Object o ) 091 ////////////////////////////////////////////////////////////////////// 092 { 093 return queue.replace ( o ); 094 } 095 096 ////////////////////////////////////////////////////////////////////// 097 ////////////////////////////////////////////////////////////////////// 098 099 private boolean loop ( ) 100 ////////////////////////////////////////////////////////////////////// 101 { 102 try 103 { 104 consumer.consume ( queue.pull ( ) ); 105 } 106 catch ( InterruptedException ex ) 107 { 108 } 109 110 return true; 111 } 112 113 ////////////////////////////////////////////////////////////////////// 114 ////////////////////////////////////////////////////////////////////// 115 }