1 /***
2 * DefaultConsumer.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 * Default implementation of the Consumer interface. This basically the only
30 * implementation needed that refers directly to the provider. Other
31 * implementation of the Consumer Interface will probably simply aggregate an
32 * instance of this class.
33 *
34 * @author $Author: dbregeon $
35 * @version $Revision: 1.3 $
36 * @see Consumer
37 * @see Producer
38 */
39 public class DefaultConsumer implements Consumer {
40 /***
41 * The producer from which the consumer will get objects to consume.
42 *
43 * @see Producer
44 */
45 private Producer provider = null;
46
47 /***
48 * Creates new DefaultConsumer
49 *
50 * @param provider
51 * The provider of the DefaultConsumer.
52 * @throws IllegalArgumentException
53 * if provider is null.
54 */
55 public DefaultConsumer(Producer provider) throws IllegalArgumentException {
56 if (provider == null) {
57 throw new IllegalArgumentException("Null is not a valid Producer.");
58 }
59 this.provider = provider;
60 }
61
62 /***
63 * eturns the producer associated with the consumer.
64 *
65 * @return The producer from wich the consumer member will get the objects
66 * to consume.
67 * @see Consumer#getProvider
68 */
69 public Producer getProvider() {
70 return this.provider;
71 }
72
73 /***
74 * Consumes a new item.
75 *
76 * @return The object produced by the consumption of a new item.
77 * @see Consumer#consumeItem
78 */
79 public Object consumeItem() {
80 Object theItem = null;
81 if (this.provider != null) {
82 theItem = this.provider.nextItem();
83 }
84 return theItem;
85 }
86
87 /***
88 * Tells the parent producer that this consumer has finished with this
89 * object.
90 *
91 * @param item
92 * an item that was previously retrieved through the consumeItem
93 * method.
94 * @return true is item was successfully validated.
95 */
96 public boolean validateItem(final Object item) {
97 boolean result = false;
98 if (item != null) {
99 result = this.provider.validateItem(item);
100 }
101 return result;
102 }
103
104 /***
105 * Sets the producer whose objects the consumer will consume.
106 *
107 * @param provider
108 * The producer to be set.
109 * @throws IllegalArgumentException
110 * In case the type of the given producer doesn't match the
111 * expected type.
112 *
113 */
114 public void setProvider(final Producer provider)
115 throws IllegalArgumentException {
116 if (provider == null) {
117 throw new IllegalArgumentException("Null is not a valid Producer.");
118 }
119 this.provider = provider;
120 }
121 }