001: /**
002: * Copyright (C) 2001-2004 France Telecom R&D
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */package org.objectweb.speedo.genclass.collection;
018:
019: import org.objectweb.speedo.genclass.GenClass;
020: import org.objectweb.speedo.mim.api.StateItf;
021:
022: import java.util.ArrayList;
023: import java.util.Collection;
024: import java.util.Iterator;
025:
026: /**
027: * This class is an implementation of the java.util.Collection interface.
028: *
029: * @author Eric.Bruneton, S.Chassande-Barrioz
030: */
031: public class CollectionImpl extends GenClass implements Collection {
032:
033: private static final long serialVersionUID = 98713218943L;
034:
035: protected final static int DEFAULT_SIZE = -1;
036:
037: CollectionAccessor accessor;
038:
039: /**
040: * Instanciates and initializes a new collection with an initial size.
041: */
042: public CollectionImpl() {
043: super ();
044: accessor = (CollectionAccessor) speedoCreateState();
045: }
046:
047: //protected BOject
048:
049: // ------------------------------------------------------------------------
050: // IMPLEMENTATION OF THE Collection INTERFACE
051: // ------------------------------------------------------------------------
052:
053: public boolean add(Object o) {
054: boolean result;
055: if (!speedoIsActive) {
056: result = accessor.collection.add(o);
057: } else {
058: CollectionAccessor ca = (CollectionAccessor) speedoGetHome()
059: .writeIntention(this , null, getDataIdentifier(o));
060: result = ca.add(o);
061: }
062: return result;
063: }
064:
065: public boolean addAll(Collection c) {
066: boolean result;
067: if (!speedoIsActive) {
068: result = accessor.collection.addAll(c);
069: } else {
070: CollectionAccessor ca = (CollectionAccessor) speedoGetHome()
071: .writeIntention(this , null, getDataIdentifiers(c));
072: result = ca.addAll(c);
073: }
074: return result;
075: }
076:
077: public void clear() {
078: if (!speedoIsActive) {
079: accessor.collection.clear();
080: } else {
081: CollectionAccessor ca = (CollectionAccessor) speedoGetHome()
082: .writeIntention(this , null);
083: ca.clear();
084: }
085: }
086:
087: public boolean contains(Object o) {
088: if (!speedoIsActive) {
089: return accessor.collection.contains(o);
090: } else {
091: CollectionAccessor ca = (CollectionAccessor) speedoGetHome()
092: .readIntention(this , null);
093: return ca.contains(o);
094: }
095: }
096:
097: public boolean containsAll(Collection c) {
098: if (!speedoIsActive) {
099: return accessor.collection.containsAll(c);
100: } else {
101: CollectionAccessor ca = (CollectionAccessor) speedoGetHome()
102: .readIntention(this , null);
103: return ca.containsAll(c);
104: }
105: }
106:
107: public boolean equals(Object o) {
108: if (!speedoIsActive) {
109: return accessor.collection.equals(o);
110: } else {
111: CollectionAccessor ca = (CollectionAccessor) speedoGetHome()
112: .readIntention(this , null);
113: return ca.equals(o);
114: }
115: }
116:
117: public boolean isEmpty() {
118: if (!speedoIsActive) {
119: return accessor.collection.isEmpty();
120: } else {
121: CollectionAccessor ca = (CollectionAccessor) speedoGetHome()
122: .readIntention(this , null);
123: return ca.isEmpty();
124: }
125: }
126:
127: public Iterator iterator() {
128: if (!speedoIsActive) {
129: return accessor.collection.iterator();
130: } else {
131: CollectionAccessor ca = (CollectionAccessor) speedoGetHome()
132: .readIntention(this , null);
133: return ca.iterator();
134: }
135: }
136:
137: public boolean remove(Object o) {
138: boolean result;
139: if (!speedoIsActive) {
140: result = accessor.collection.remove(o);
141: } else {
142: CollectionAccessor ca = (CollectionAccessor) speedoGetHome()
143: .writeIntention(this , null, getDataIdentifier(o));
144: result = ca.remove(o);
145: }
146: return result;
147: }
148:
149: public boolean removeAll(Collection c) {
150: boolean result;
151: if (!speedoIsActive) {
152: result = accessor.collection.removeAll(c);
153: } else {
154: CollectionAccessor ca = (CollectionAccessor) speedoGetHome()
155: .writeIntention(this , null, getDataIdentifiers(c));
156: result = ca.removeAll(c);
157: }
158: return result;
159: }
160:
161: public boolean retainAll(Collection c) {
162: throw new UnsupportedOperationException();
163: }
164:
165: public int size() {
166: if (!speedoIsActive) {
167: return accessor.collection.size();
168: } else {
169: CollectionAccessor ca = (CollectionAccessor) speedoGetHome()
170: .readIntention(this , null);
171: return ca.size();
172: }
173: }
174:
175: public Object[] toArray() {
176: if (!speedoIsActive) {
177: return accessor.collection.toArray();
178: } else {
179: CollectionAccessor ca = (CollectionAccessor) speedoGetHome()
180: .readIntention(this , null);
181: return ca.toArray();
182: }
183: }
184:
185: public Object[] toArray(Object[] a) {
186: if (!speedoIsActive) {
187: return accessor.collection.toArray(a);
188: } else {
189: CollectionAccessor ca = (CollectionAccessor) speedoGetHome()
190: .readIntention(this , null);
191: return ca.toArray(a);
192: }
193: }
194:
195: // OTHER METHODS //
196: // --------------//
197:
198: public StateItf speedoCreateState() {
199: return new CollectionAccessor(this );
200: }
201:
202: public Object createGenClass() {
203: return new ArrayList();
204: }
205:
206: public StateItf speedoGetReferenceState() {
207: return accessor;
208: }
209:
210: public void speedoSetReferenceState(StateItf refAcc) {
211: accessor = (CollectionAccessor) refAcc;
212: }
213:
214: }
|