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.HashSet;
017: import java.util.Iterator;
018: import java.util.List;
019: import java.util.Set;
020:
021: import org.rapla.entities.User;
022:
023: public class UpdateResult {
024: User user;
025: List operations = new ArrayList();
026:
027: public UpdateResult(User user) {
028: this .user = user;
029: }
030:
031: public void addOperation(final Object operation) {
032: if (operation == null)
033: throw new IllegalStateException("Operation can't be null");
034: operations.add(operation);
035: }
036:
037: public User getUser() {
038: return user;
039: }
040:
041: public Set getRemoveObjects() {
042: return getObject(Remove.class);
043: }
044:
045: public Set getChangeObjects() {
046: return getObject(Change.class);
047: }
048:
049: public Set getAddObjects() {
050: return getObject(Add.class);
051: }
052:
053: public Iterator getOperations(final Class operationClass) {
054: Iterator operationsIt = operations.iterator();
055: if (operationClass == null)
056: throw new IllegalStateException(
057: "OperationClass can't be null");
058:
059: List list = new ArrayList();
060: while (operationsIt.hasNext()) {
061: Object obj = operationsIt.next();
062: if (operationClass.isInstance(obj))
063: list.add(obj);
064: }
065:
066: return list.iterator();
067: }
068:
069: protected Set getObject(final Class operationClass) {
070: Set set = new HashSet();
071: if (operationClass == null)
072: throw new IllegalStateException(
073: "OperationClass can't be null");
074: Iterator it = getOperations(operationClass);
075: while (it.hasNext()) {
076: set.add(((UpdateOperation) it.next()).getCurrent());
077: }
078: return set;
079: }
080:
081: static public class Add implements UpdateOperation {
082: Object currentObj; // the actual represantation of the object
083: Object newObj; // the object in the state when it was addes
084:
085: public Add(Object currentObj, Object newObj) {
086: this .currentObj = currentObj;
087: this .newObj = newObj;
088: }
089:
090: public Object getCurrent() {
091: return currentObj;
092: }
093:
094: public Object getNew() {
095: return newObj;
096: }
097: }
098:
099: static public class Remove implements UpdateOperation {
100: Object currentObj; // the actual represantation of the object
101:
102: public Remove(Object currentObj) {
103: this .currentObj = currentObj;
104: }
105:
106: public Object getCurrent() {
107: return currentObj;
108: }
109: }
110:
111: static public class Change implements UpdateOperation {
112: Object currentObj; // the actual represantation of the object
113: Object newObj; // the object in the state when it was changed
114: Object oldObj; // the object in the state before it was changed
115:
116: public Change(Object currentObj, Object newObj, Object oldObj) {
117: this .currentObj = currentObj;
118: this .newObj = newObj;
119: this .oldObj = oldObj;
120: }
121:
122: public Object getCurrent() {
123: return currentObj;
124: }
125:
126: public Object getNew() {
127: return newObj;
128: }
129:
130: public Object getOld() {
131: return oldObj;
132: }
133: }
134: }
135:
136: interface UpdateOperation {
137: public Object getCurrent();
138: }
|