OW2 Consortium telosys

Rev

Rev 52 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
13 lguerin 1
package org.objectweb.telosys.common.data;
2
 
3
import java.util.Collection;
4
 
5
import org.objectweb.telosys.common.TelosysException;
6
 
7
/**
8
 * Memory DataSet <br>
9
 * Special DataSet designed to be loaded from memory, using a Collection of arrays of objects<br>
10
 * Use the "load(Object)" method to initialize the DataSet with a collection<br>
11
 * ( for example a LinkedList of Object[] )
12
 *
13
 * @author Laurent GUERIN
14
 *
15
 */
16
public class MemDataSet extends UpdatableDataSet
17
{
52 lguerin 18
	private static final long serialVersionUID = 1L;
19
 
20
	//-----------------------------------------------------------------------------
13 lguerin 21
    //--- CONSTRUCTORS
22
    //-----------------------------------------------------------------------------
23
    /**
24
     * Constructs a new void DataSet
25
     */
26
    public MemDataSet()
27
    {
28
        super();
29
    }
30
 
31
    //-----------------------------------------------------------------------------
32
    //--- PRIVATE/PROTECTED METHODS
33
    //-----------------------------------------------------------------------------
34
 
35
 
36
    //-----------------------------------------------------------------------------
37
    //--- PUBLIC METHODS
38
    //-----------------------------------------------------------------------------
39
 
40
    //-----------------------------------------------------------------------------
41
    /* (non-Javadoc)
42
     * @see org.objectweb.telosys.common.data.DataSet#load()
43
     */
44
    public int load() throws TelosysException
45
    {
46
        //--- Initialize the DataSet ( prepare to load )
47
        initBeforeLoad();
48
        //--- Return the number of rows in the DataSet
49
        return getRowCount();
50
    }
51
 
52
    //-----------------------------------------------------------------------------
53
    /* (non-Javadoc)
54
     * @see org.objectweb.telosys.common.data.DataSet#load(java.lang.Object)
55
     */
56
    public int load(Object obj) throws TelosysException
57
    {
58
        return init(obj);
59
    }
60
 
61
    //-----------------------------------------------------------------------------
62
    /**
138 lguerin 63
     * Initializes the DataSet with the given collection <br>
64
     *
65
     * @param obj a collection of Object[], or Object[][]
13 lguerin 66
     * @return
67
     * @throws TelosysException
68
     */
69
    private int init(Object obj) throws TelosysException
70
    {
71
        if ( obj == null )
72
        {
73
            throw new TelosysException("load(obj) : parameter is null");
74
        }
75
 
76
        //--- Initialize the DataSet ( prepare to load )
77
        initBeforeLoad();
78
 
79
        if ( obj instanceof Collection ) // Collection of "Object[]"
80
        {
138 lguerin 81
            Collection<?> collection = (Collection<?>) obj;
82
            for ( Object oRow : collection )
13 lguerin 83
            {
84
                if ( oRow instanceof Object[] )
85
                {
86
                    Object[] values = (Object[]) oRow ;
87
                    DataRow dr = new DataRow( values.length, values);
88
                    super.addRow(dr);
89
                }
138 lguerin 90
                else {
91
                    throw new TelosysException("Invalid row type (not Object[]) "  );
92
                }
13 lguerin 93
            }
94
        }
138 lguerin 95
        else if ( obj instanceof Object[][] ) // Array of "Object[]"
13 lguerin 96
        {
138 lguerin 97
            Object[][] array = (Object[][]) obj;
98
            for ( Object[] oRow : array )
99
            {
100
                DataRow dr = new DataRow( oRow.length, oRow);
101
                super.addRow(dr);
102
            }
13 lguerin 103
        }
104
        else
105
        {
138 lguerin 106
            throw new TelosysException("Invalid parameter " + obj.getClass().getName() + " ( not a collection or array of Object[] )" );
13 lguerin 107
        }
108
 
109
        //--- Return the number of rows in the DataSet
110
        return getRowCount();
111
    }
112
 
113
    //-----------------------------------------------------------------------------
114
    //-----------------------------------------------------------------------------
115
}