Author: julien(a)jboss.com
Date: 2007-12-18 20:01:18 -0500 (Tue, 18 Dec 2007)
New Revision: 9369
Added:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/StructuralObjectHandle.java
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/state/structural/StructuralObject.java
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/state/structural/StructuralStateContext.java
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockModelImpl.java
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockObjectImpl.java
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/StructuralObjectImpl.java
Log:
start to implement refresh of a structural state context
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/state/structural/StructuralObject.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/state/structural/StructuralObject.java 2007-12-19
00:35:40 UTC (rev 9368)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/state/structural/StructuralObject.java 2007-12-19
01:01:18 UTC (rev 9369)
@@ -23,6 +23,8 @@
package org.jboss.portal.presentation.model.state.structural;
import java.util.List;
+import java.util.Set;
+import java.util.Map;
/**
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
@@ -35,6 +37,107 @@
StructuralState getState();
+ /**
+ * A comparison between two structural objects.
+ */
+ public static class Refresh
+ {
+
+ /** . */
+ private final StructuralObject parent;
+
+ /** . */
+ private final StructuralObject object;
+
+ /** . */
+ private final Set<StructuralObject> addedChildren;
+
+ /** . */
+ private final Set<String> removedChildren;
+
+ /** . */
+// private final Set<String> destroyedChildren;
+
+ /** . */
+ private final Map<String, StructuralObject> staleChildren;
+
+ /** . */
+ private final Set<String> validChildren;
+
+ public Refresh(
+ StructuralObject parent,
+ StructuralObject object,
+ Set<StructuralObject> addedChildren,
+ Set<String> removedChildren,
+ Map<String, StructuralObject> staleChildren,
+ Set<String> validChildren/*,
+ Set<String> destroyedChildren*/)
+ {
+ if (addedChildren == null)
+ {
+ throw new IllegalArgumentException();
+ }
+ if (validChildren == null)
+ {
+ throw new IllegalArgumentException();
+ }
+ if (staleChildren == null)
+ {
+ throw new IllegalArgumentException();
+ }
+ if (removedChildren == null)
+ {
+ throw new IllegalArgumentException();
+ }
+// if (destroyedChildren == null)
+// {
+// throw new IllegalArgumentException();
+// }
+ this.parent = parent;
+ this.object = object;
+ this.addedChildren = addedChildren;
+ this.validChildren = validChildren;
+ this.staleChildren = staleChildren;
+ this.removedChildren = removedChildren;
+// this.destroyedChildren = destroyedChildren;
+ }
+
+ public StructuralObject getParent()
+ {
+ return parent;
+ }
+
+ public StructuralObject getObject()
+ {
+ return object;
+ }
+
+// public Set<String> getDestroyedChildren()
+// {
+// return destroyedChildren;
+// }
+
+ public Set<StructuralObject> getAddedChildren()
+ {
+ return addedChildren;
+ }
+
+ public Map<String, StructuralObject> getStaleChildren()
+ {
+ return staleChildren;
+ }
+
+ public Set<String> getValidChildren()
+ {
+ return validChildren;
+ }
+
+ public Set<String> getRemovedChildren()
+ {
+ return removedChildren;
+ }
+ }
+
public static class Change
{
}
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/state/structural/StructuralStateContext.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/state/structural/StructuralStateContext.java 2007-12-19
00:35:40 UTC (rev 9368)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/model/state/structural/StructuralStateContext.java 2007-12-19
01:01:18 UTC (rev 9369)
@@ -85,6 +85,8 @@
*/
void validate(StructuralObject object) throws IllegalArgumentException,
StateException;
+ StructuralObject.Refresh refresh(StructuralObject object) throws
IllegalArgumentException, StateException;
+
/**
* Create an object.
*
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockModelImpl.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockModelImpl.java 2007-12-19
00:35:40 UTC (rev 9368)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockModelImpl.java 2007-12-19
01:01:18 UTC (rev 9369)
@@ -40,6 +40,8 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.HashSet;
+import java.util.Set;
/**
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
@@ -134,6 +136,92 @@
getValidMockObject(object);
}
+ public StructuralObject.Refresh refresh(StructuralObject object) throws
IllegalArgumentException, StateException
+ {
+ MockObjectImpl mockObject = getMockObject(object, false);
+
+ //
+ StructuralObjectImpl previous = (StructuralObjectImpl)object;
+ StructuralObjectImpl current = mockObject.takeSnapshot();
+
+ //
+ StructuralObject parent = null;
+ if (previous.parentHandle != null)
+ {
+ if (!current.parentHandle.equals(previous.handle))
+ {
+ parent = getMockObject(current.parentHandle, true).takeSnapshot();
+ }
+ }
+
+ //
+ Map<String, StructuralObjectHandle> previousChildren = new
HashMap<String, StructuralObjectHandle>();
+ for (StructuralObjectHandle childHandle : previous.childrenHandles)
+ {
+ previousChildren.put(childHandle.id, childHandle);
+ }
+
+ //
+ Map<String, StructuralObjectHandle> currentChildren = new
HashMap<String, StructuralObjectHandle>();
+ for (StructuralObjectHandle childHandle : current.childrenHandles)
+ {
+ currentChildren.put(childHandle.id, childHandle);
+ }
+
+ //
+ Map<String, StructuralObjectHandle> addedChildrenHandleMap = new
HashMap<String, StructuralObjectHandle>(currentChildren);
+ addedChildrenHandleMap.keySet().removeAll(previousChildren.keySet());
+ Set<StructuralObject> addedChildren = new
HashSet<StructuralObject>();
+ for (StructuralObjectHandle handle : addedChildrenHandleMap.values())
+ {
+ StructuralObject snapshot = getMockObject(handle, true).takeSnapshot();
+ addedChildren.add(snapshot);
+ }
+
+ //
+ Map<String, StructuralObjectHandle> removedChildrenHandleMap = new
HashMap<String, StructuralObjectHandle>(previousChildren);
+ removedChildrenHandleMap.keySet().removeAll(currentChildren.keySet());
+ Set<String> removedChildren = new HashSet<String>();
+ for (StructuralObjectHandle handle : removedChildrenHandleMap.values())
+ {
+ removedChildren.add(handle.id);
+ }
+
+ //
+ Map<String, StructuralObjectHandle> commonCurrentChildrenHandleMap = new
HashMap<String, StructuralObjectHandle>(currentChildren);
+ commonCurrentChildrenHandleMap.keySet().retainAll(previousChildren.keySet());
+
+ //
+ Map<String, StructuralObjectHandle> commonPreviousChildrenHandleMap = new
HashMap<String, StructuralObjectHandle>(previousChildren);
+ commonPreviousChildrenHandleMap.keySet().retainAll(currentChildren.keySet());
+
+ //
+ Set<StructuralObjectHandle> validChildrenHandleSet = new
HashSet<StructuralObjectHandle>(commonPreviousChildrenHandleMap.values());
+ validChildrenHandleSet.retainAll(commonCurrentChildrenHandleMap.values());
+ Set<String> validChildren = new HashSet<String>();
+ for (StructuralObjectHandle handle : validChildrenHandleSet)
+ {
+ validChildren.add(handle.id);
+ }
+
+ //
+ Set<StructuralObjectHandle> staleChildrenHandleSet = new
HashSet<StructuralObjectHandle>(commonPreviousChildrenHandleMap.values());
+ staleChildrenHandleSet.removeAll(commonCurrentChildrenHandleMap.values());
+
+ //
+ Map<String, StructuralObject> staleChildrenHandleMap = new
HashMap<String, StructuralObject>();
+ for (StructuralObjectHandle previousHandle : staleChildrenHandleSet)
+ {
+ String id = previousHandle.id;
+ StructuralObjectHandle handle = commonCurrentChildrenHandleMap.get(id);
+ StructuralObject snapshot = getMockObject(handle, true).takeSnapshot();
+ staleChildrenHandleMap.put(id, snapshot);
+ }
+
+ //
+ return new StructuralObject.Refresh(parent, current, addedChildren,
removedChildren, staleChildrenHandleMap, validChildren);
+ }
+
public StructuralObject.Update update(StructuralObject object, Map<String,
String> changes) throws StateChangeVetoException
{
MockObject mockObject = getValidMockObject(object);
@@ -271,6 +359,11 @@
private MockObjectImpl getValidMockObject(StructuralObject object) throws
StateException
{
+ return getMockObject(object, true);
+ }
+
+ private MockObjectImpl getMockObject(StructuralObject object, boolean
validateVersion)
+ {
if (object == null)
{
throw new IllegalArgumentException("No null object accepted");
@@ -280,24 +373,35 @@
StructuralObjectImpl structuralObject = (StructuralObjectImpl)object;
//
- MockObjectImpl mockObject = universe.get(structuralObject.getId());
+ return getMockObject(structuralObject.handle, validateVersion);
+ }
+
+ private MockObjectImpl getMockObject(StructuralObjectHandle handle, boolean
validateVersion)
+ {
+ if (handle == null)
+ {
+ throw new IllegalArgumentException("No null object accepted");
+ }
//
+ MockObjectImpl mockObject = universe.get(handle.id);
+
+ //
if (mockObject == null)
{
throw new NoSuchStateException();
}
- // An invalid mock object should not be in the universe
+ //
if (!mockObject.isValid())
{
- throw new AssertionError();
+ throw new StaleStateException();
}
//
- if (mockObject.getVersion() != structuralObject.version)
+ if (validateVersion && (mockObject.getVersion() != handle.version))
{
- throw new StaleStateException("Version has changed obtained=" +
mockObject.getVersion() + " wanted=" + structuralObject.version);
+ throw new StaleStateException("Version has changed obtained=" +
mockObject.getVersion() + " wanted=" + handle.version);
}
//
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockObjectImpl.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockObjectImpl.java 2007-12-19
00:35:40 UTC (rev 9368)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/MockObjectImpl.java 2007-12-19
01:01:18 UTC (rev 9369)
@@ -22,9 +22,6 @@
******************************************************************************/
package org.jboss.portal.presentation.test.model.state.structural;
-import org.jboss.portal.presentation.model.state.structural.StructuralObject;
-import org.jboss.portal.presentation.model.state.ConstraintViolationException;
-
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
@@ -50,9 +47,6 @@
private final String name;
/** . */
- private final String id;
-
- /** . */
private final Map<String, String> propertyValues;
/** . */
@@ -71,7 +65,7 @@
private boolean valid;
/** The version id. */
- int version;
+ private StructuralObjectHandle handle;
MockObjectImpl(MockModelImpl model, String name, MockObject.Type type, String id)
{
@@ -81,9 +75,8 @@
this.children = new LinkedHashMap<String, MockObjectImpl>();
this.propertyValues = new HashMap<String, String>();
this.propertyBehaviors = new HashMap<String, UpdateBehavior>();
- this.id = id;
this.valid = true;
- this.version = 0;
+ this.handle = new StructuralObjectHandle(id, 0);
}
public MockObject.Type getType()
@@ -103,7 +96,7 @@
public String getId()
{
- return id;
+ return handle.id;
}
public String getPropertyValue(String propertyName)
@@ -165,8 +158,8 @@
//
child.parent = this;
- children.put(child.id, child);
- version++;
+ children.put(child.handle.id, child);
+ handle = handle.nextHandle();
//
return child;
@@ -217,17 +210,30 @@
}
//
- this.version++;
+ handle = handle.nextHandle();
}
public int getVersion()
{
- return version;
+ return handle.version;
}
- public StructuralObject takeSnapshot()
+ public StructuralObjectImpl takeSnapshot()
{
- return new StructuralObjectImpl(id, version, new StructuralStateImpl(type.clazz,
name, new HashMap<String, String>(propertyValues)));
+ StructuralObjectHandle parentHandle = parent == null ? null : parent.handle;
+
+ //
+ List<StructuralObjectHandle> childrenHandles = new
ArrayList<StructuralObjectHandle>();
+ for (MockObjectImpl child : children.values())
+ {
+ childrenHandles.add(child.handle);
+ }
+
+ //
+ StructuralStateImpl state = new StructuralStateImpl(type.clazz, name, new
HashMap<String, String>(propertyValues));
+
+ //
+ return new StructuralObjectImpl(handle, parentHandle, childrenHandles, state);
}
public void move(MockObject destination) throws IllegalStateException, MockException
@@ -265,16 +271,16 @@
}
//
- parent.children.remove(id);
- parent.version++;
+ parent.children.remove(handle.id);
+ parent.handle = parent.handle.nextHandle();
//
- destination.children.put(id, this);
- destination.version++;
+ destination.children.put(handle.id, this);
+ destination.handle = destination.handle.nextHandle();
//
parent = destination;
- version++;
+ handle = handle.nextHandle();
}
public void destroy() throws MockException
@@ -287,8 +293,8 @@
// Detach from parent
if (parent != null)
{
- parent.children.remove(id);
- parent.version++;
+ parent.children.remove(handle.id);
+ parent.handle = parent.handle.nextHandle();
parent = null;
}
Added:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/StructuralObjectHandle.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/StructuralObjectHandle.java
(rev 0)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/StructuralObjectHandle.java 2007-12-19
01:01:18 UTC (rev 9369)
@@ -0,0 +1,81 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2006, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.presentation.test.model.state.structural;
+
+/**
+ * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
+ * @version $Revision: 630 $
+ */
+class StructuralObjectHandle
+{
+
+ /** . */
+ final String id;
+
+ /** . */
+ final int version;
+
+ StructuralObjectHandle(String id, int version)
+ {
+ if (id == null)
+ {
+ throw new IllegalArgumentException();
+ }
+
+ //
+ this.id = id;
+ this.version = version;
+ }
+
+ public int hashCode()
+ {
+ return id.hashCode() * version;
+ }
+
+ public boolean equals(Object o)
+ {
+ if (o == this)
+ {
+ return true;
+ }
+ if (o instanceof StructuralObjectHandle)
+ {
+ StructuralObjectHandle that = (StructuralObjectHandle)o;
+
+ //
+ return id.equals(that.id) && version == that.version;
+ }
+ return false;
+ }
+
+ StructuralObjectHandle nextHandle()
+ {
+ return new StructuralObjectHandle(id, version + 1);
+ }
+
+ public String toString()
+ {
+ return "StructuralObjectHandle[id=" + id + ",version=" +
version + "]";
+ }
+
+}
Modified:
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/StructuralObjectImpl.java
===================================================================
---
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/StructuralObjectImpl.java 2007-12-19
00:35:40 UTC (rev 9368)
+++
branches/presentation/presentation/src/main/org/jboss/portal/presentation/test/model/state/structural/StructuralObjectImpl.java 2007-12-19
01:01:18 UTC (rev 9369)
@@ -25,6 +25,8 @@
import org.jboss.portal.presentation.model.state.structural.StructuralObject;
import org.jboss.portal.presentation.model.state.structural.StructuralState;
+import java.util.List;
+
/**
* @author <a href="mailto:julien@jboss.org">Julien Viet</a>
* @version $Revision: 630 $
@@ -33,24 +35,32 @@
{
/** . */
- final String id;
+ final StructuralObjectHandle handle;
/** . */
- final int version;
+ final StructuralObjectHandle parentHandle;
/** . */
+ final List<StructuralObjectHandle> childrenHandles;
+
+ /** . */
private final StructuralStateImpl state;
- public StructuralObjectImpl(String id, int version, StructuralStateImpl state)
+ public StructuralObjectImpl(
+ StructuralObjectHandle handle,
+ StructuralObjectHandle parentHandle,
+ List<StructuralObjectHandle> childrenHandles,
+ StructuralStateImpl state)
{
- this.id = id;
- this.version = version;
+ this.handle = handle;
+ this.parentHandle = parentHandle;
+ this.childrenHandles = childrenHandles;
this.state = state;
}
public String getId()
{
- return id;
+ return handle.id;
}
public StructuralState getState()
@@ -67,7 +77,7 @@
if (o instanceof StructuralObjectImpl)
{
StructuralObjectImpl that = (StructuralObjectImpl)o;
- return id.equals(that.id) && version == that.version;
+ return handle.equals(that.handle);
}
return false;
}