001: /*--------------------------------------------------------------------------*
002: | Copyright (C) 2006 Christopher Kohlhaas |
003: | |
004: | This program is free software; you can redistribute it and/or modify |
005: | it under the terms of the GNU General Public License as published by the |
006: | Free Software Foundation. A copy of the license has been included with |
007: | these distribution in the COPYING file, if not go to www.fsf.org |
008: | |
009: | As a special exception, you are granted the permissions to link this |
010: | program with every library, which license fulfills the Open Source |
011: | Definition as published by the Open Source Initiative (OSI). |
012: *--------------------------------------------------------------------------*/
013: package org.rapla.storage;
014:
015: import java.util.ArrayList;
016: import java.util.HashMap;
017: import java.util.List;
018:
019: import org.rapla.entities.storage.RefEntity;
020:
021: public class UpdateEvent implements java.io.Serializable, Cloneable {
022: private static final long serialVersionUID = 1L;
023:
024: HashMap removeSet = new HashMap();
025: HashMap storeSet = new HashMap();
026:
027: ArrayList removeObjects = new ArrayList();
028: ArrayList storeObjects = new ArrayList();
029: Object userId;
030: long repositoryVersion;
031:
032: public UpdateEvent() {
033: }
034:
035: public void setUserId(Object userId) {
036: this .userId = userId;
037: }
038:
039: public Object getUserId() {
040: return userId;
041: }
042:
043: private void addRemove(RefEntity entity) {
044: removeObjects.add(entity);
045: removeSet.put(entity.getId(), entity);
046: }
047:
048: private void addStore(RefEntity entity) {
049: storeObjects.add(entity);
050: storeSet.put(entity.getId(), entity);
051: }
052:
053: public List getRemoveObjects() {
054: return removeObjects;
055: }
056:
057: public List getStoreObjects() {
058: return storeObjects;
059: }
060:
061: /** use this method if you want to avoid adding the same Entity twice.*/
062: public void putStore(RefEntity entity) {
063:
064: if (storeSet.get(entity.getId()) == null)
065: addStore(entity);
066: }
067:
068: /** use this method if you want to avoid adding the same Entity twice.*/
069: public void putRemove(RefEntity entity) {
070: if (removeSet.get(entity.getId()) == null)
071: addRemove(entity);
072: }
073:
074: /** find an entity in the update-event that matches the passed original. Returns null
075: * if no such entity is found. */
076: public RefEntity findEntity(RefEntity original) {
077: RefEntity entity = (RefEntity) storeSet.get(original.getId());
078: if (entity != null)
079: return entity;
080: entity = (RefEntity) removeSet.get(original.getId());
081: if (entity != null)
082: return entity;
083: return null;
084: }
085:
086: public Object clone() {
087: UpdateEvent clone = new UpdateEvent();
088: clone.userId = userId;
089: clone.removeObjects = (ArrayList) removeObjects.clone();
090: clone.storeObjects = (ArrayList) storeObjects.clone();
091: clone.removeSet = (HashMap) removeSet.clone();
092: clone.storeSet = (HashMap) storeSet.clone();
093: return clone;
094: }
095:
096: public long getRepositoryVersion() {
097: return repositoryVersion;
098: }
099:
100: public void setRepositoryVersion(long repositoryVersion) {
101: this.repositoryVersion = repositoryVersion;
102: }
103: }
|