| 1 |
/** |
| 2 |
* Copyright (c) 2002-2010 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v2.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v20.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM - Initial API and implementation |
| 10 |
*/ |
| 11 |
package org.eclipse.emf.common.notify.impl; |
| 12 |
|
| 13 |
|
| 14 |
import java.util.Collection; |
| 15 |
|
| 16 |
import org.eclipse.emf.common.notify.Adapter; |
| 17 |
import org.eclipse.emf.common.notify.Notification; |
| 18 |
import org.eclipse.emf.common.notify.Notifier; |
| 19 |
import org.eclipse.emf.common.util.BasicEList; |
| 20 |
import org.eclipse.emf.common.util.ECollections; |
| 21 |
import org.eclipse.emf.common.util.EList; |
| 22 |
|
| 23 |
|
| 24 |
/** |
| 25 |
* An extensible notifier implementation. |
| 26 |
*/ |
| 27 |
public class BasicNotifierImpl implements Notifier |
| 28 |
{ |
| 29 |
/** |
| 30 |
* Creates a blank new instance. |
| 31 |
*/ |
| 32 |
public BasicNotifierImpl() |
| 33 |
{ |
| 34 |
super(); |
| 35 |
} |
| 36 |
|
| 37 |
public static class EAdapterList<E extends Object & Adapter> extends BasicEList<E> |
| 38 |
{ |
| 39 |
private static final long serialVersionUID = 1L; |
| 40 |
|
| 41 |
protected Notifier notifier; |
| 42 |
|
| 43 |
public EAdapterList(Notifier notifier) |
| 44 |
{ |
| 45 |
this.notifier = notifier; |
| 46 |
} |
| 47 |
|
| 48 |
protected boolean safe; |
| 49 |
|
| 50 |
@Override |
| 51 |
protected boolean canContainNull() |
| 52 |
{ |
| 53 |
return false; |
| 54 |
} |
| 55 |
|
| 56 |
@Override |
| 57 |
protected boolean useEquals() |
| 58 |
{ |
| 59 |
return false; |
| 60 |
} |
| 61 |
|
| 62 |
@Override |
| 63 |
protected Object [] newData(int capacity) |
| 64 |
{ |
| 65 |
return new Adapter [capacity]; |
| 66 |
} |
| 67 |
|
| 68 |
@Override |
| 69 |
protected void didAdd(int index, E newObject) |
| 70 |
{ |
| 71 |
newObject.setTarget(notifier); |
| 72 |
} |
| 73 |
|
| 74 |
@Override |
| 75 |
protected void didRemove(int index, E oldObject) |
| 76 |
{ |
| 77 |
E adapter = oldObject; |
| 78 |
if (notifier.eDeliver()) |
| 79 |
{ |
| 80 |
Notification notification = |
| 81 |
new NotificationImpl(Notification.REMOVING_ADAPTER, oldObject, null, index) |
| 82 |
{ |
| 83 |
@Override |
| 84 |
public Object getNotifier() |
| 85 |
{ |
| 86 |
return notifier; |
| 87 |
} |
| 88 |
}; |
| 89 |
adapter.notifyChanged(notification); |
| 90 |
} |
| 91 |
if (adapter instanceof Adapter.Internal) |
| 92 |
{ |
| 93 |
((Adapter.Internal)adapter).unsetTarget(notifier); |
| 94 |
} |
| 95 |
else if (adapter.getTarget() == notifier) |
| 96 |
{ |
| 97 |
adapter.setTarget(null); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
@Override |
| 102 |
public Object [] data() |
| 103 |
{ |
| 104 |
safe = true; |
| 105 |
if (data != null && data.length != size) |
| 106 |
{ |
| 107 |
if (size == 0) |
| 108 |
{ |
| 109 |
data = null; |
| 110 |
} |
| 111 |
else |
| 112 |
{ |
| 113 |
Object [] oldData = data; |
| 114 |
data = newData(size); |
| 115 |
System.arraycopy(oldData, 0, data, 0, size); |
| 116 |
} |
| 117 |
} |
| 118 |
return data; |
| 119 |
} |
| 120 |
|
| 121 |
protected void ensureSafety() |
| 122 |
{ |
| 123 |
if (safe && data != null) |
| 124 |
{ |
| 125 |
Object [] oldData = data; |
| 126 |
data = newData(data.length); |
| 127 |
System.arraycopy(oldData, 0, data, 0, size); |
| 128 |
safe = false; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
@Override |
| 133 |
public boolean add(E object) |
| 134 |
{ |
| 135 |
ensureSafety(); |
| 136 |
return super.add(object); |
| 137 |
} |
| 138 |
|
| 139 |
@Override |
| 140 |
public void add(int index, E object) |
| 141 |
{ |
| 142 |
ensureSafety(); |
| 143 |
super.add(index, object); |
| 144 |
} |
| 145 |
|
| 146 |
@Override |
| 147 |
public boolean addAll(Collection<? extends E> collection) |
| 148 |
{ |
| 149 |
ensureSafety(); |
| 150 |
return super.addAll(collection); |
| 151 |
} |
| 152 |
|
| 153 |
@Override |
| 154 |
public boolean remove(Object object) |
| 155 |
{ |
| 156 |
ensureSafety(); |
| 157 |
return super.remove(object); |
| 158 |
} |
| 159 |
|
| 160 |
@Override |
| 161 |
public E remove(int index) |
| 162 |
{ |
| 163 |
ensureSafety(); |
| 164 |
return super.remove(index); |
| 165 |
} |
| 166 |
|
| 167 |
@Override |
| 168 |
public boolean removeAll(Collection<?> collection) |
| 169 |
{ |
| 170 |
ensureSafety(); |
| 171 |
return super.removeAll(collection); |
| 172 |
} |
| 173 |
|
| 174 |
@Override |
| 175 |
public void clear() |
| 176 |
{ |
| 177 |
ensureSafety(); |
| 178 |
super.clear(); |
| 179 |
} |
| 180 |
|
| 181 |
@Override |
| 182 |
public boolean retainAll(Collection<?> collection) |
| 183 |
{ |
| 184 |
ensureSafety(); |
| 185 |
return super.retainAll(collection); |
| 186 |
} |
| 187 |
|
| 188 |
@Override |
| 189 |
public E set(int index, E object) |
| 190 |
{ |
| 191 |
ensureSafety(); |
| 192 |
return super.set(index, object); |
| 193 |
} |
| 194 |
|
| 195 |
@Override |
| 196 |
public void move(int newPosition, E object) |
| 197 |
{ |
| 198 |
ensureSafety(); |
| 199 |
super.move(newPosition, object); |
| 200 |
} |
| 201 |
|
| 202 |
@Override |
| 203 |
public E move(int newPosition, int oldPosition) |
| 204 |
{ |
| 205 |
ensureSafety(); |
| 206 |
return super.move(newPosition, oldPosition); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
public EList<Adapter> eAdapters() |
| 211 |
{ |
| 212 |
return ECollections.emptyEList(); |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Returns the adapter list, even if it is <code>null</code>. |
| 217 |
* @return the adapter list, even if it is <code>null</code>. |
| 218 |
*/ |
| 219 |
protected BasicEList<Adapter> eBasicAdapters() |
| 220 |
{ |
| 221 |
return null; |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Returns the underlying array of adapters. |
| 226 |
* The length of this array reflects exactly the number of adapters |
| 227 |
* where <code>null</code> represents the lack of any adapters. |
| 228 |
* This array may not be modified by the caller |
| 229 |
* and must be guaranteed not to be modified even if the {@link #eAdapters() list of adapters} is modified. |
| 230 |
* @return the underlying array of adapters. |
| 231 |
*/ |
| 232 |
protected Adapter[] eBasicAdapterArray() |
| 233 |
{ |
| 234 |
BasicEList<Adapter> eBasicAdapters = eBasicAdapters(); |
| 235 |
return eBasicAdapters == null ? null : (Adapter[])eBasicAdapters.data(); |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Returns whether there are any adapters. |
| 240 |
* @return whether there are any adapters. |
| 241 |
*/ |
| 242 |
protected boolean eBasicHasAdapters() |
| 243 |
{ |
| 244 |
BasicEList<Adapter> eBasicAdapters = eBasicAdapters(); |
| 245 |
return eBasicAdapters != null && eBasicAdapters.size() != 0; |
| 246 |
} |
| 247 |
|
| 248 |
/* |
| 249 |
* Javadoc copied from interface. |
| 250 |
*/ |
| 251 |
public boolean eDeliver() |
| 252 |
{ |
| 253 |
return false; |
| 254 |
} |
| 255 |
|
| 256 |
/* |
| 257 |
* Javadoc copied from interface. |
| 258 |
*/ |
| 259 |
public void eSetDeliver(boolean deliver) |
| 260 |
{ |
| 261 |
throw new UnsupportedOperationException(); |
| 262 |
} |
| 263 |
|
| 264 |
/* |
| 265 |
* Javadoc copied from interface. |
| 266 |
*/ |
| 267 |
public void eNotify(Notification notification) |
| 268 |
{ |
| 269 |
Adapter[] eAdapters = eBasicAdapterArray(); |
| 270 |
if (eAdapters != null && eDeliver()) |
| 271 |
{ |
| 272 |
for (int i = 0, size = eAdapters.length; i < size; ++i) |
| 273 |
{ |
| 274 |
eAdapters[i].notifyChanged(notification); |
| 275 |
} |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
* Returns whether {@link #eNotify eNotify} needs to be called. |
| 281 |
* This may return <code>true</code> even when {@link #eDeliver eDeliver} is <code>false</code> |
| 282 |
* or when {@link #eAdapters eAdapters} is empty. |
| 283 |
* @return whether {@link #eNotify eNotify} needs to be called. |
| 284 |
*/ |
| 285 |
public boolean eNotificationRequired() |
| 286 |
{ |
| 287 |
return eBasicHasAdapters() && eDeliver(); |
| 288 |
} |
| 289 |
} |