View Javadoc

1   /***
2    * MultiplexedProducer.java
3    *
4    * This file is part of the creme library.
5    * The creme library intends to ease the development effort of large
6    * applications by providing easy to use support classes.
7    *
8    * Copyright (C) 2002 Denis Bregeon
9    *
10   * This library is free software; you can redistribute it and/or
11   * modify it under the terms of the GNU Lesser General Public
12   * License as published by the Free Software Foundation; either
13   * version 2.1 of the License, or (at your option) any later version.
14   *
15   * This library is distributed in the hope that it will be useful,
16   * but WITHOUT ANY WARRANTY; without even the implied warranty of
17   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18   * Lesser General Public License for more details.
19   *
20   * You should have received a copy of the GNU Lesser General Public
21   * License along with this library; if not, write to the Free Software
22   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23   *
24   * contact information: dbregeon@sourceforge.net
25   */
26  package org.jcreme.processing;
27  
28  /***
29   * This producer does nothing but providing an existing object. Its main use is
30   * to provide outlets to the ProducerMultiplexer.
31   * 
32   * @author $Author: dbregeon $
33   * @revision $Revision: 1.3 $
34   */
35  public class MultiplexedProducer extends AbstractProducer {
36  	/***
37  	 * The multiplexing source.
38  	 */
39  	private final ProducerMultiplexer source;
40  
41  	/***
42  	 * Creates a new instance of MultiplexedProducer
43  	 * 
44  	 * @param initialProductionQueueSize
45  	 *            the size of the production queue when it is initialized.
46  	 * @param initialValidationQueueSize
47  	 *            the size of the validation queue when it is initialized.
48  	 * @param source
49  	 *            the multiplixer that provides values to this Producer.
50  	 */
51  	public MultiplexedProducer(int initialProductionQueueSize,
52  			int initialValidationQueueSize, ProducerMultiplexer source) {
53  		super(initialProductionQueueSize, initialValidationQueueSize);
54  		this.source = source;
55  	}
56  
57  	/***
58  	 * Produces an item, i.e. marks it as consumable.
59  	 * 
60  	 * @return true if the method actually produced an item, false otherwise.
61  	 * @see Producer#produceItem
62  	 */
63  	public boolean produceItem() {
64  		boolean result = false;
65  		if (this.source != null) {
66  			final Object theItem = this.source.consumeItem(this);
67  			if (theItem != null) {
68  				publishItem(theItem);
69  				result = true;
70  			}
71  		}
72  		return result;
73  	}
74  
75  	/***
76  	 * Validates an item, i.e. marks it as having been consumed. The method then
77  	 * validates the object that enabled to produce the item in the source
78  	 * consumer.
79  	 * 
80  	 * @param theItem
81  	 *            The item to be validated.
82  	 * @return true if theItem was successfully validated.
83  	 * @see Producer#validateItem
84  	 */
85  	public boolean validateItem(final Object theItem) {
86  		boolean result = super.validateItem(theItem);
87  		if (result && (theItem != null)) {
88  			result = this.source.validateItem(theItem);
89  		}
90  		return result;
91  	}
92  
93  }