KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opensubsystems > patterns > listdata > util > AsyncDataLoaderRunnable


1 /*
2  * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
3  *
4  * Project: OpenSubsystems
5  *
6  * $Id: AsyncDataLoaderRunnable.java,v 1.3 2007/01/07 06:14:15 bastafidli Exp $
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; version 2 of the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21
22 package org.opensubsystems.patterns.listdata.util;
23
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26
27 import org.opensubsystems.core.data.DataObject;
28
29 /**
30  * New Runnable object which allows to process multiple items at the same time.
31  * The processor of the data has to specify it's version (status). If the
32  * version changes before or while the processing of the data is in progress
33  * the processing will stop.
34  *
35  * @version $Id: AsyncDataLoaderRunnable.java,v 1.3 2007/01/07 06:14:15 bastafidli Exp $
36  * @author Miro Halas
37  * @code.reviewer Miro Halas
38  * @code.reviewed Initial revision
39  */

40 public abstract class AsyncDataLoaderRunnable implements Runnable JavaDoc
41 {
42    // Attributes ///////////////////////////////////////////////////////////////
43

44    /**
45     * Value uniquely identifying the original state of the object by which the
46     * data should be processed. This can identify if the object changes and data
47     * cannot/shouldn't be added anymore.
48     */

49    protected int m_iOriginalObjectVersion;
50    
51    /**
52     * List of data which should be added to the table
53     */

54    protected List JavaDoc m_lstData;
55    
56    // COnstructors /////////////////////////////////////////////////////////////
57

58    /**
59     * Constructor
60     *
61     * @param lstData - list with already loaded data
62     * @param iOriginalObjectVersion - version of the object by which the data
63     * should be processed. If the version of the object at the time when
64     * the data are being processed is different from this version, then
65     * the data will not be processed or the processing of the data stops.
66     */

67    public AsyncDataLoaderRunnable(
68       List JavaDoc lstData,
69       int iOriginalObjectVersion
70    )
71    {
72       m_lstData = lstData;
73       m_iOriginalObjectVersion = iOriginalObjectVersion;
74    }
75
76    // Public methods ///////////////////////////////////////////////////////////
77

78    /**
79     * {@inheritDoc}
80     */

81    public final void run()
82    {
83       Iterator JavaDoc items;
84       DataObject tempData;
85          
86       for (items = m_lstData.iterator();
87            items.hasNext()
88            && (m_iOriginalObjectVersion == getCurrentObjectVersion());)
89       {
90          tempData = (DataObject)items.next();
91          processItem(tempData);
92       }
93    }
94    
95    // Helper methods ///////////////////////////////////////////////////////////
96

97    /**
98     * Process new item from the list.
99     *
100     * This method cannot be abstract so that we can do the in place overriding
101     * of the class.
102     *
103     * @param dataToProcess - data item to process
104     */

105    protected abstract void processItem(
106       final DataObject dataToProcess
107    );
108
109    /**
110     * This method has to be overriden to return the actual value of the version
111     * for object, which is processing the data. If the current version of the
112     * object will not match the version for which this runnable was constructed
113     * then the processing will stop.
114     *
115     * @return int
116     */

117    protected abstract int getCurrentObjectVersion(
118    );
119 }
120
Popular Tags