KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > genclass > collection > ListAccessor


1 /**
2  * Copyright (C) 2001-2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.speedo.genclass.collection;
19
20 import org.objectweb.jorm.api.PIndexedElem;
21 import org.objectweb.jorm.api.PExceptionIO;
22 import org.objectweb.speedo.genclass.api.SpeedoGenClassProxy;
23 import org.objectweb.speedo.genclass.GenClassAccessor;
24
25 import java.util.List JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.ListIterator JavaDoc;
28 import java.util.Iterator JavaDoc;
29
30 /**
31  *
32  * @author S.Chassande-Barrioz
33  */

34 public class ListAccessor extends CollectionAccessor implements List JavaDoc{
35     /**
36      * Indicates the number of elements existing on the data support. All
37      * element added after this index must be marked as CREATED.
38      */

39     protected int loadedSize = -1;
40
41     public ListAccessor(SpeedoGenClassProxy jdoProxy) {
42         super(jdoProxy);
43     }
44     public void workingSetClosed() {
45         super.workingSetClosed();
46         //Update the loaded size after the data support flushing
47
loadedSize = elements.size();
48     }
49
50
51     public String JavaDoc toString() {
52         return "ListAccessor: id=" + jdoProxy.jdoGetGenClassId();
53     }
54
55     public PIndexedElem createPIndexedElem(GenClassAccessor gca) {
56         return new ListElem(gca);
57     }
58
59     public void paSetNbElem(int nbelem) {
60         super.paSetNbElem(nbelem);
61         loadedSize = 0;
62     }
63
64     public void paAdd(PIndexedElem elem, Object JavaDoc conn) throws PExceptionIO {
65         super.paAdd(elem, conn);
66         loadedSize++;
67     }
68
69     public boolean add(Object JavaDoc o) {
70         if (super.add(o)) {
71             int idx = elements.size() - 1;
72             ListElem le = (ListElem) elements.get(idx);
73             le.setIndex(new Integer JavaDoc(idx));
74             return true;
75         } else {
76             return false;
77         }
78     }
79
80     public boolean addAll(int i, Collection JavaDoc collection) {
81         if (collection == null) {
82             return false;
83         }
84         int size = elements.size();
85         if (size > i) {
86             Iterator JavaDoc it = collection.iterator();
87             int j = i;
88             while(it.hasNext()) {
89                 add(j, it.next());
90                 j++;
91             }
92             return true;
93         } else {
94             throw new IndexOutOfBoundsException JavaDoc("Try to insert at the position "
95                     + i + " whereas there is only " + size + " elements.");
96         }
97     }
98
99     public Object JavaDoc get(int i) {
100         ListElem le = (ListElem) elements.get(i);
101         if (le.getElemStatus() == PIndexedElem.ELEM_DELETED) {
102             throw new java.lang.ArrayIndexOutOfBoundsException JavaDoc(i);
103         }
104         return le.getElement();
105     }
106
107     public Object JavaDoc set(int i, Object JavaDoc o) {
108         ListElem le = (ListElem) elements.get(i);
109         if (le.getElemStatus() == PIndexedElem.ELEM_DELETED) {
110             throw new java.lang.ArrayIndexOutOfBoundsException JavaDoc(i);
111         }
112         Object JavaDoc res = le.getElement();
113         le.setStatus(PIndexedElem.ELEM_MODIFIED);
114         le.setElement(o);
115         return res;
116     }
117
118     public void add(int i, Object JavaDoc o) {
119         int size = elements.size();
120         if (size > i) {
121             Object JavaDoc toAdd = o;
122             Object JavaDoc tmp = o;
123             int j = i;
124             ListElem le;
125             //insert the new and shift others
126
while(j < size) {
127                 le =(ListElem) elements.get(j);
128                 tmp = le.element;
129                 le.element = toAdd;
130                 le.setStatus((j < loadedSize
131                         ? PIndexedElem.ELEM_MODIFIED
132                         : PIndexedElem.ELEM_CREATED));
133                 toAdd = tmp;
134                 j++;
135             }
136             //create a new entry for the shifted element (or the added if i = size)
137
le = (ListElem) createPIndexedElem();
138             le.setStatus((j < loadedSize
139                     ? PIndexedElem.ELEM_MODIFIED
140                     : PIndexedElem.ELEM_CREATED));
141             le.setIndex(new Integer JavaDoc(size));
142             elements.add(le);
143         } else {
144             throw new IndexOutOfBoundsException JavaDoc("Try to insert at the position "
145                     + i + " whereas there is only " + size + " elements.");
146         }
147     }
148
149     public Object JavaDoc remove(int i) {
150         return null;
151     }
152
153     public int indexOf(Object JavaDoc o) {
154         return 0;
155     }
156
157     public int lastIndexOf(Object JavaDoc o) {
158         return 0;
159     }
160
161     public ListIterator JavaDoc listIterator() {
162         return null;
163     }
164
165     public ListIterator JavaDoc listIterator(int i) {
166         return null;
167     }
168
169     public List JavaDoc subList(int i, int i1) {
170         return null;
171     }
172 }
173
Popular Tags