You are viewing a plain text version of this content. The canonical link for it is here.
Posted to adffaces-commits@incubator.apache.org by ma...@apache.org on 2006/07/25 03:54:52 UTC
svn commit: r425268 [4/7] - in
/incubator/adffaces/branches/matzew-repackaging-trinidad/trinidad/trinidad-api/src:
main/java-templates/org/apache/myfaces/adf/component/
main/java/org/apache/myfaces/adf/change/
main/java/org/apache/myfaces/adf/component...
Modified: incubator/adffaces/branches/matzew-repackaging-trinidad/trinidad/trinidad-api/src/main/java/org/apache/myfaces/adf/model/RowKeySet.java
URL: http://svn.apache.org/viewvc/incubator/adffaces/branches/matzew-repackaging-trinidad/trinidad/trinidad-api/src/main/java/org/apache/myfaces/adf/model/RowKeySet.java?rev=425268&r1=425267&r2=425268&view=diff
==============================================================================
--- incubator/adffaces/branches/matzew-repackaging-trinidad/trinidad/trinidad-api/src/main/java/org/apache/myfaces/adf/model/RowKeySet.java (original)
+++ incubator/adffaces/branches/matzew-repackaging-trinidad/trinidad/trinidad-api/src/main/java/org/apache/myfaces/adf/model/RowKeySet.java Mon Jul 24 20:54:50 2006
@@ -13,192 +13,192 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.myfaces.adf.model;
-
-import java.util.AbstractSet;
-
-/**
- * This Set is a mutable collection
- * of rowKeys.
- * This class is meant to be used with models that have a current rowKey concept.
- * Therefore, the {@link #add()}, {@link #remove()} and {@link #isContained} methods
- * do not need to take the rowKey as an argument, since the rowKey is implied.
- * The implied key is obtained by calling {@link #getCollectionModel.getRowKey()}
- * <P>
- * Because this Set has a reference to the underlying model, operations like
- * {@link #addAll()}, {@link #removeAll()} and {@link #invertAll()} may
- * execute in constant time.
- * <P>
- * Note that the {@link #size()} method on the this Set might be expensive to
- * compute. Use the {@link #getSize()} method on this class for an inexpensive size.
- * @author The Oracle ADF Faces Team
- */
-public abstract class RowKeySet<E> extends AbstractSet<E> implements Cloneable
-{
- public RowKeySet()
- {
- }
-
- /**
- * @deprecated remove asap
- */
- public abstract boolean isContainedByDefault();
-
- /**
- * Changes the underlying CollectionModel being used by this set.
- * The current rowKey (that is used by some of the methods in this class)
- * is obtained from this CollectionModel.
- * <P>
- * Users typically do not need to call this method.
- * This method is called by component writers who need to set the models
- * used by their components on this set.
- */
- public abstract void setCollectionModel(CollectionModel model);
-
- /**
- * Gets the underlying model used by this set.
- * @see #setCollectionModel
- */
- protected abstract CollectionModel getCollectionModel();
-
- /**
- * Adds the given rowKey to this set if it doesn't already exist, removes
- * it otherwise.
- * @return true if the row is now added. false otherwise.
- */
- public boolean invert(E rowKey)
- {
- // doing "add" followed by an optional "remove" is faster than switching on
- // "contains"; the latter does two hashtable lookups all the time,
- // while the former does two hashtable lookups half the time.
-
- if (add(rowKey))
- return true; // the key was not present earlier, so now we're done.
-
- // rowKey was already present, so remove it:
- remove(rowKey);
- return false;
- }
-
- /**
- * Adds the current rowKey to this set if it doesn't already exist; removes
- * it otherwise.
- * @return true if the row is now added. false otherwise.
- */
- public final boolean invert()
- {
- E rowkey = (E) getCollectionModel().getRowKey();
- return invert(rowkey);
- }
-
- /**
- * Checks to see if the current key is contained by this set.
- * @return true if this set contains the current key
- */
- public final boolean isContained()
- {
- Object rowkey = getCollectionModel().getRowKey();
- return contains(rowkey);
- }
-
- /**
- * Adds or removes the current key.
- * @param isContained if true, the current key is added to this set.
- * if false, the current key is removed from this set.
- */
- public final void setContained(boolean isContained)
- {
- if (isContained)
- add();
- else
- remove();
- }
-
- /**
- * Adds the current key to this set.
- * @return true if this set changed. ie: true is returned if this set
- * did not previously contain the current key.
- */
- public final boolean add()
- {
- E rowkey = (E) getCollectionModel().getRowKey();
- return add(rowkey);
- }
-
- /**
- * Removes the current key from this set.
- * @return true if this set changed. ie: true is returned if this set
- * previously contained the current key.
- */
- public final boolean remove()
- {
- Object rowkey = getCollectionModel().getRowKey();
- return remove(rowkey);
- }
-
- /**
- * Gets the number of elements contained by this set. The difference between
- * this method and {@link #size()} is that this method may return -1 if the
- * size is expensive to compute.
- * This implementation simply calls {@link #size()}.
- * @return -1 if the number of elements is expensive to compute.
- */
- public int getSize()
- {
- return size();
- }
-
- /**
- * Adds all the rowKeys in the current collection into this Set.
- * If the underlying model is a List, then all the rowKeys in the List
- * are added to this Set. If the underlying model is a tree, then all the
- * rowKeys in the current subtree are added to this Set.
- */
- public abstract void addAll();
-
- /**
- * Removes all the rowKeys in the current collection from this Set.
- * If the underlying model is a List, then all the rowKeys in the List
- * are removed from this Set. If the underlying model is a tree, then all the
- * rowKeys in the current subtree are removed from this Set.
- * <P>
- * For List models, this method and {@link #clear} behave the same.
- * For tree models, this method only operates on the current subtree, while
- * the {@link #clear} method removes everything from this Set.
- * <P>
- * This implementation simply calls {@link #clear}
- */
- public void removeAll()
- {
- clear();
- }
-
- /**
- * Inverts this Set. Every element that is in this Set is removed, and
- * every element that is not in this Set is added to this Set.
- * <P>
- * For List models, this method operates on the entire List.
- * For tree models, this method only operates on the current subtree.
- */
- public abstract void invertAll();
-
- /**
- * Creates a shallow clone of this set.
- * Keys may be added or removed from the clone without affecting
- * this instance. The keys themselves may not be cloned.
- * This implementation simply calls
- * {@link Object#clone}
- */
- public RowKeySet clone()
- {
- try
- {
- return (RowKeySet) super.clone();
- }
- catch (CloneNotSupportedException e)
- {
- // should not happen:
- throw new UnsupportedOperationException("Could not clone", e);
- }
- }
-}
+package org.apache.myfaces.adf.model;
+
+import java.util.AbstractSet;
+
+/**
+ * This Set is a mutable collection
+ * of rowKeys.
+ * This class is meant to be used with models that have a current rowKey concept.
+ * Therefore, the {@link #add()}, {@link #remove()} and {@link #isContained} methods
+ * do not need to take the rowKey as an argument, since the rowKey is implied.
+ * The implied key is obtained by calling {@link #getCollectionModel.getRowKey()}
+ * <P>
+ * Because this Set has a reference to the underlying model, operations like
+ * {@link #addAll()}, {@link #removeAll()} and {@link #invertAll()} may
+ * execute in constant time.
+ * <P>
+ * Note that the {@link #size()} method on the this Set might be expensive to
+ * compute. Use the {@link #getSize()} method on this class for an inexpensive size.
+ * @author The Oracle ADF Faces Team
+ */
+public abstract class RowKeySet<E> extends AbstractSet<E> implements Cloneable
+{
+ public RowKeySet()
+ {
+ }
+
+ /**
+ * @deprecated remove asap
+ */
+ public abstract boolean isContainedByDefault();
+
+ /**
+ * Changes the underlying CollectionModel being used by this set.
+ * The current rowKey (that is used by some of the methods in this class)
+ * is obtained from this CollectionModel.
+ * <P>
+ * Users typically do not need to call this method.
+ * This method is called by component writers who need to set the models
+ * used by their components on this set.
+ */
+ public abstract void setCollectionModel(CollectionModel model);
+
+ /**
+ * Gets the underlying model used by this set.
+ * @see #setCollectionModel
+ */
+ protected abstract CollectionModel getCollectionModel();
+
+ /**
+ * Adds the given rowKey to this set if it doesn't already exist, removes
+ * it otherwise.
+ * @return true if the row is now added. false otherwise.
+ */
+ public boolean invert(E rowKey)
+ {
+ // doing "add" followed by an optional "remove" is faster than switching on
+ // "contains"; the latter does two hashtable lookups all the time,
+ // while the former does two hashtable lookups half the time.
+
+ if (add(rowKey))
+ return true; // the key was not present earlier, so now we're done.
+
+ // rowKey was already present, so remove it:
+ remove(rowKey);
+ return false;
+ }
+
+ /**
+ * Adds the current rowKey to this set if it doesn't already exist; removes
+ * it otherwise.
+ * @return true if the row is now added. false otherwise.
+ */
+ public final boolean invert()
+ {
+ E rowkey = (E) getCollectionModel().getRowKey();
+ return invert(rowkey);
+ }
+
+ /**
+ * Checks to see if the current key is contained by this set.
+ * @return true if this set contains the current key
+ */
+ public final boolean isContained()
+ {
+ Object rowkey = getCollectionModel().getRowKey();
+ return contains(rowkey);
+ }
+
+ /**
+ * Adds or removes the current key.
+ * @param isContained if true, the current key is added to this set.
+ * if false, the current key is removed from this set.
+ */
+ public final void setContained(boolean isContained)
+ {
+ if (isContained)
+ add();
+ else
+ remove();
+ }
+
+ /**
+ * Adds the current key to this set.
+ * @return true if this set changed. ie: true is returned if this set
+ * did not previously contain the current key.
+ */
+ public final boolean add()
+ {
+ E rowkey = (E) getCollectionModel().getRowKey();
+ return add(rowkey);
+ }
+
+ /**
+ * Removes the current key from this set.
+ * @return true if this set changed. ie: true is returned if this set
+ * previously contained the current key.
+ */
+ public final boolean remove()
+ {
+ Object rowkey = getCollectionModel().getRowKey();
+ return remove(rowkey);
+ }
+
+ /**
+ * Gets the number of elements contained by this set. The difference between
+ * this method and {@link #size()} is that this method may return -1 if the
+ * size is expensive to compute.
+ * This implementation simply calls {@link #size()}.
+ * @return -1 if the number of elements is expensive to compute.
+ */
+ public int getSize()
+ {
+ return size();
+ }
+
+ /**
+ * Adds all the rowKeys in the current collection into this Set.
+ * If the underlying model is a List, then all the rowKeys in the List
+ * are added to this Set. If the underlying model is a tree, then all the
+ * rowKeys in the current subtree are added to this Set.
+ */
+ public abstract void addAll();
+
+ /**
+ * Removes all the rowKeys in the current collection from this Set.
+ * If the underlying model is a List, then all the rowKeys in the List
+ * are removed from this Set. If the underlying model is a tree, then all the
+ * rowKeys in the current subtree are removed from this Set.
+ * <P>
+ * For List models, this method and {@link #clear} behave the same.
+ * For tree models, this method only operates on the current subtree, while
+ * the {@link #clear} method removes everything from this Set.
+ * <P>
+ * This implementation simply calls {@link #clear}
+ */
+ public void removeAll()
+ {
+ clear();
+ }
+
+ /**
+ * Inverts this Set. Every element that is in this Set is removed, and
+ * every element that is not in this Set is added to this Set.
+ * <P>
+ * For List models, this method operates on the entire List.
+ * For tree models, this method only operates on the current subtree.
+ */
+ public abstract void invertAll();
+
+ /**
+ * Creates a shallow clone of this set.
+ * Keys may be added or removed from the clone without affecting
+ * this instance. The keys themselves may not be cloned.
+ * This implementation simply calls
+ * {@link Object#clone}
+ */
+ public RowKeySet clone()
+ {
+ try
+ {
+ return (RowKeySet) super.clone();
+ }
+ catch (CloneNotSupportedException e)
+ {
+ // should not happen:
+ throw new UnsupportedOperationException("Could not clone", e);
+ }
+ }
+}
Propchange: incubator/adffaces/branches/matzew-repackaging-trinidad/trinidad/trinidad-api/src/main/java/org/apache/myfaces/adf/model/RowKeySet.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: incubator/adffaces/branches/matzew-repackaging-trinidad/trinidad/trinidad-api/src/main/java/org/apache/myfaces/adf/model/RowKeySetTreeImpl.java
URL: http://svn.apache.org/viewvc/incubator/adffaces/branches/matzew-repackaging-trinidad/trinidad/trinidad-api/src/main/java/org/apache/myfaces/adf/model/RowKeySetTreeImpl.java?rev=425268&r1=425267&r2=425268&view=diff
==============================================================================
--- incubator/adffaces/branches/matzew-repackaging-trinidad/trinidad/trinidad-api/src/main/java/org/apache/myfaces/adf/model/RowKeySetTreeImpl.java (original)
+++ incubator/adffaces/branches/matzew-repackaging-trinidad/trinidad/trinidad-api/src/main/java/org/apache/myfaces/adf/model/RowKeySetTreeImpl.java Mon Jul 24 20:54:50 2006
@@ -13,658 +13,658 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.myfaces.adf.model;
-import java.io.Serializable;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map.Entry;
-import java.util.NoSuchElementException;
-import java.util.Set;
-
-/**
- * Implements a collection of rowKeys from a TreeModel.
- * The methods on this class are optimized such that it is possible
- * to add/remove all the rowkeys in a subtree in constant time.
- * <P>
- * The generic type E is the type of a rowKey.
- * @author The Oracle ADF Faces Team
- */
-public class RowKeySetTreeImpl<E> extends RowKeySet<E> implements Serializable
-{
- /**
- * Creates a new Set that is initially empty.
- */
- public RowKeySetTreeImpl()
- {
- this(false);
- }
-
- /**
- * Creates a new Set, that may contain every rowKey by default.
- * @param addAll if this is true, every rowKey is initially added to this set.
- */
- public RowKeySetTreeImpl(boolean addAll)
- {
- _root = new Node<E>(addAll);
- }
-
- /**
- * Tests to see if the given rowKey is included in this Set.
- * @return true If the rowKey is included in this Set.
- */
- @Override
- public boolean contains(Object rowKey)
- {
- return _isContained((E) rowKey);
- }
-
- /**
- * @deprecated do not use. this will be removed post Tier 1.
- */
- public boolean isContainedByDefault()
- {
- TreeModel model = getCollectionModel();
- E rowkey = (E) model.getRowKey();
- return new Search().find(rowkey).isDefaultContained;
- }
-
- public Iterator<E> iterator()
- {
- return new PathIterator();
- }
-
- /**
- * Adds the given rowKey to this Set.
- * @return false if the given rowKey was already in this Set.
- * @see #remove(Object)
- * @see #addAll()
- */
- public boolean add(E rowKey)
- {
- return _setContained(rowKey, true);
- }
-
- /**
- * Removes the given rowKey from this Set.
- * @return false if the given rowKey was already not in this Set.
- * @see #add(E)
- * @see #removeAll()
- */
- @Override
- public boolean remove(Object rowKey)
- {
- return _setContained((E) rowKey, false);
- }
-
- /**
- * Adds the current rowKey and all rowKeys beneath the current rowKey to this Set.
- * This method executes in constant time.
- * @see #add(E)
- * @see #removeAll()
- */
- public void addAll()
- {
- _selectAll(true);
- }
-
- /**
- * Removes the current rowKey and all rowKeys beneath the current rowKey to this Set.
- * This method executes in constant time.
- * @see #remove(Object)
- * @see #clear()
- * @see #addAll()
- */
- public void removeAll()
- {
- _selectAll(false);
- }
-
- /**
- * {@inheritDoc}
- * <P>
- * If the parameter is another RowKeySetTreeImpl, this method is
- * optimized to give superior performance and avoid iteration.
- */
- @Override
- public boolean addAll(Collection<? extends E> other)
- {
- if (other instanceof RowKeySetTreeImpl)
- {
- RowKeySetTreeImpl otherset = (RowKeySetTreeImpl) other;
- return _processOperation(this._root, otherset._root, true);
- }
- return super.addAll(other);
- }
-
- /**
- * {@inheritDoc}
- * <P>
- * If the parameter is another RowKeySetTreeImpl, this method is
- * optimized to give superior performance and avoid iteration.
- */
- @Override
- public boolean removeAll(Collection<?> other)
- {
- if (other instanceof RowKeySetTreeImpl)
- {
- RowKeySetTreeImpl otherset = (RowKeySetTreeImpl) other;
- return _processOperation(this._root, otherset._root, false);
- }
- return super.removeAll(other);
- }
-
- private boolean _processOperation(Node<E> set1, Node<E> set2, boolean add)
- {
- /*
- * setXdef = setX.isDefaultContained
- * setXdif = setX.isDifferent
- * asterisks (*) indicate changes.
- *
- * TABLE ---------------------------------------------------
- |-----------Inputs---------|--------Outputs---------------|
- | set1def | set2def | add | removeAll | addAll | set1def |
- | 0 | 0 | 1 | 0 | 1 | 0 |
- | 0 | 1 | 1 | 1 | 1 | 1* |
- | 1 | 0 | 1 | 0 | 0 | 1 |
- | 1 | 1 | 1 | 1 | 0 | 1 |
- | 0 | 0 | 0 | 0 | 0 | 0 |
- | 0 | 1 | 0 | 1 | 0 | 0 |
- | 1 | 0 | 0 | 0 | 1 | 1 |
- | 1 | 1 | 0 | 1 | 1 | 0* |
- |---------------------------------------------------------|
- */
-
- boolean hasChanges = false;
-
- // See TABLE (above) 'removeAll' column:
- // if set2 contains everything, then there is no point hanging on to
- // any set1-deltas that are not in set2, so remove them:
- if (set2.isDefaultContained && set1.keySet().retainAll(set2.keySet()))
- hasChanges = true;
-
- // See TABLE (above) 'addAll' column:
- // this "addAll" flag controls whether to process any set2-deltas that are not
- // already in set1. If set1 has everything by default and we're adding set2,
- // then there is no point processing any set2-deltas not already in set1.
- // Similarly, if set1 has nothing by default and we're removing set2,
- // then there is no point processing any set2-deltas not already in set1.
- // So only process the set2-deltas if we're doing an add (and set1
- // does not contain everything) or we're doing a remove (and set1
- // has everything):
- boolean addAll = add ^ set1.isDefaultContained;
-
- for(Entry<E, Node<E>> en:set2.entrySet())
- {
- E segment = en.getKey();
- Node<E> subset2 = en.getValue();
- Node<E> subset1 = set1.get(segment);
-
- if (subset1 == null)
- {
- if (addAll)
- {
- subset1 = new Node<E>(set1, segment);
- hasChanges = true;
- }
- else
- continue;
- }
- if (_processOperation(subset1, subset2, add))
- hasChanges = true;
- }
-
- // See TABLE (above) 'Outputs/set1Def' column:
- // if set2 contains everything by default, then that will affect
- // the default flag of set1:
- if (set2.isDefaultContained && (set1.isDefaultContained != add))
- {
- set1.isDefaultContained = add;
- // since we toggled the default state, toggle the diff state
- // as well so that we maintain the status for this node:
- set1.isDifferent = !set1.isDifferent;
- hasChanges = true;
- }
-
- // if this node is contained by set2, then depending on the
- // add flag, this node should (not) be contained by set1:
- if ((set2.isDefaultContained ^ set2.isDifferent) &&
- ((set1.isDefaultContained ^ set1.isDifferent) != add))
- {
- set1.isDifferent = !set1.isDifferent;
- hasChanges = true;
- }
-
- return hasChanges;
- }
-
- /**
- * Removes all rowKeys from this Set.
- * This method executes in the same time as
- * {@link HashMap#clear()}
- */
- @Override
- public void clear()
- {
- _root.clear();
- _root.isDefaultContained = _root.isDifferent = false;
- }
-
- /**
- * Gets the number of elements contained by this set.
- * Does not force the underlying model to compute its size.
- * @return -1 if the number of elements is unknown.
- */
- public int getSize()
- {
- return _getSize(null, _root, getCollectionModel(), false);
- }
-
- /**
- * Gets the number of elements in this Set.
- * This might force the underlying model to compute its size.
- * @return a non-negative number.
- */
- public int size()
- {
- return _getSize(null, _root, getCollectionModel(), true);
- }
-
- @Override
- public boolean isEmpty()
- {
- return (getSize() == 0);
- }
-
- /**
- * Sets the TreeModel associated with this Set.
- * @param model This must be of type {@link TreeModel}
- */
- public final void setCollectionModel(CollectionModel model)
- {
- _model = (TreeModel) model;
- }
-
- /**
- * Creates a clone of this Set. RowKeys may be added/removed from the
- * clone without affecting this instance.
- */
- public RowKeySetTreeImpl clone()
- {
- RowKeySetTreeImpl clone = (RowKeySetTreeImpl) super.clone();
- clone._root = _root.clone();
- return clone;
- }
-
- /**
- * @deprecated not implemented.
- */
- public void invertAll()
- {
- // TODO
- throw new UnsupportedOperationException("todo");
- }
-
- /**
- * Gets the TreeModel associated with this set.
- * This TreeModel will be used to get the current rowKey, and also to
- * get parent rowKeys, from child rowKeys.
- * @see TreeModel#getRowKey
- */
- protected TreeModel getCollectionModel()
- {
- return _model;
- }
-
- /**
- * Gets the total number of nodes in the subtree of the given TreeModel.
- *
- * WARNING: this method changes the TreeModel's currency.
- * The caller is responsible for restoring the model currency.
- *
- * @param exclusions any rowKeys present in this Set are excluded from the count.
- */
- private int _getTreeSize(TreeModel model, Set<E> exclusions)
- {
- int sz = 0;
- for(int i=0;true;i++)
- {
- model.setRowIndex(i);
- if (model.isRowAvailable())
- {
- E rowkey = (E) model.getRowKey();
- if (exclusions.contains(rowkey))
- continue;
- sz++;
- if (model.isContainer())
- {
- model.enterContainer();
- Set<E> empty = Collections.emptySet();
- sz += _getTreeSize(model, empty);
- model.exitContainer();
- }
- }
- else
- return sz;
- }
- }
-
- private int _getSize(E rowkey, Node<E> set, TreeModel model, boolean fetchall)
- {
- // special-case the root collection:
- int sz = ((rowkey != null) && (set.isDefaultContained ^ set.isDifferent)) ? 1 : 0;
- if (set.isDefaultContained)
- {
- if (!fetchall)
- return -1;
-
- Object old = model.getRowKey();
- try
- {
- model.setRowKey(rowkey);
- // special-case the root collection:
- if (rowkey == null)
- {
- sz += _getTreeSize(model, set.keySet());
- }
- else if (model.isContainer())
- {
- model.enterContainer();
- sz += _getTreeSize(model, set.keySet());
- }
- }
- finally
- {
- model.setRowKey(old);
- }
- }
-
- for(Entry<E, Node<E>> en:set.entrySet())
- {
- E newrowkey = en.getKey();
- Node<E> subset = en.getValue();
- int size = _getSize(newrowkey, subset, model, fetchall);
- if (size < 0)
- return -1;
- sz+= size;
- }
- return sz;
- }
-
- /**
- * adds or removes all the paths rooted at the current path
- * @param isSelectAll if true does an add-all. else does remove-all.
- */
- private void _selectAll(final boolean isSelectAll)
- {
- Search search = new Search()
- {
- protected boolean create(Node<E> parent, E rowkey)
- {
- // if the parent does not have the correct default, then
- // we need to add entries for the children, since we need
- // to store a delta:
- return (parent.isDefaultContained != isSelectAll);
- }
-
- protected Node<E> found(Node<E> child)
- {
- child.isDefaultContained = isSelectAll;
- child.isDifferent = false;
- child.clear();
- return null;
- }
- };
-
- TreeModel model = getCollectionModel();
- E rowkey = (E) model.getRowKey();
- search.find(rowkey);
- }
-
- private boolean _isContained(E rowkey)
- {
- Search search = new Search()
- {
- protected Node<E> notFound(Node<E> parent, E rowkey)
- {
- return parent.isDefaultContained ? parent : null;
- }
-
- protected Node<E> found(Node<E> child)
- {
- return (child.isDefaultContained ^ child.isDifferent) ? child : null;
- }
- };
-
- return (search.find(rowkey) != null);
- }
-
- /**
- * Adds or removes the given path from this set.
- * @param isContained If true, the current path is added. Otherwise,
- * it is removed.
- * @return true if this Set changed due to this operation.
- */
- private boolean _setContained(E rowkey, final boolean isContained)
- {
- Search search = new Search()
- {
- protected boolean create(Node<E> parent, E rowkey)
- {
- // only need to create child deltas, if the parent's
- // default is wrong:
- return parent.isDefaultContained != isContained;
- }
-
- protected Node<E> notFound(Node<E> parent, E rowkey)
- {
- return null;
- }
- };
-
- Node<E> current = search.find(rowkey);
- if ((current != null) &&
- ((current.isDefaultContained ^ current.isDifferent) != isContained))
- {
- current.isDifferent = !current.isDifferent;
- return true;
- }
- return false;
- }
-
- /**
- * Advances the currency of the given TreeModel to the next node in a
- * depth-first walk of the tree.
- * @param minDepth the minimum depth of the rowkey. use this to
- * walk within a subtree. Use 0 to walk entire tree.
- * @param recurseChildren if true, will walk children.
- * @return true if the currency of the model was successfully advanced to
- * the next rowData.
- */
- private static boolean _advanceToNextItem(
- TreeModel model, int minDepth, boolean recurseChildren)
- {
- assert minDepth >= 0;
-
- if (recurseChildren && model.isRowAvailable() && model.isContainer())
- {
- model.enterContainer();
- model.setRowIndex(-1);
- }
- while(true)
- {
- int ri = model.getRowIndex();
- model.setRowIndex(ri+1);
- if (model.isRowAvailable())
- return true;
-
- int depth = model.getDepth();
- if (depth <= minDepth)
- return false;
-
- model.exitContainer();
- }
- }
-
- private static final class Node<K> extends HashMap<K, Node<K>>
- implements Serializable, Cloneable
- {
- public boolean isDifferent = false;
- public boolean isDefaultContained = false;
-
- public Node(boolean isDefaultContained)
- {
- this.isDefaultContained = isDefaultContained;
- }
-
- public Node(Node<K> parent, K segment)
- {
- this(parent.isDefaultContained);
- parent.put(segment, this);
- }
-
- // clone all the values as well:
- private void _deepClone(Node<K> root)
- {
- for(Entry<K, Node<K>> en:root.entrySet())
- {
- Node<K> original = en.getValue();
- Node<K> clone = original.clone();
- en.setValue(clone);
- }
- }
-
- public Node<K> clone()
- {
- Node<K> clone = (Node<K>) super.clone();
- _deepClone(clone);
- return clone;
- }
- }
-
- private class Search
- {
- public Search()
- {
- }
-
- protected boolean create(Node<E> parent, E rowkey)
- {
- return false;
- }
-
- protected Node<E> notFound(Node<E> parent, E rowkey)
- {
- return parent;
- }
-
- protected Node<E> found(Node<E> result)
- {
- return result;
- }
-
- public Node<E> find(E rowkey)
- {
- Node<E> current = _root;
- if (rowkey != null)
- {
- TreeModel model = getCollectionModel();
- List<E> parentkeys = model.getAllAncestorContainerRowKeys(rowkey);
- List<E> allkeys = new ArrayList<E>(parentkeys.size() + 1);
- allkeys.addAll(parentkeys);
- allkeys.add(rowkey);
- for(E key:allkeys)
- {
- Node<E> next = current.get(key);
- if (next == null)
- {
- if (create(current, key))
- next = new Node<E>(current, key);
- else
- return notFound(current, key);
- }
- current = next;
- }
- }
- return found(current);
- }
- }
-
- private final class PathIterator implements Iterator<E>
- {
- PathIterator()
- {
- _value = _next(); // initialize;
- }
-
- public E next()
- {
- if (!hasNext())
- throw new NoSuchElementException();
- E value = _value;
- _value = _next();
- return value;
- }
-
- public boolean hasNext()
- {
- return (_value != null);
- }
-
- public void remove()
- {
- throw new UnsupportedOperationException();
- }
-
- private boolean _containsSubtree(E rowkey)
- {
- Search search = new Search()
- {
- protected Node<E> notFound(Node<E> parent, E rowkey)
- {
- return parent.isDefaultContained ? parent : null;
- }
- };
- Node<E> current = search.find(rowkey);
- return (current != null) &&
- ((!current.isEmpty()) || current.isDefaultContained);
- }
-
- private E _next()
- {
- TreeModel model = getCollectionModel();
- if (model == null)
- return null;
-
- Object oldPath = model.getRowKey();
- try
- {
- model.setRowKey(_currPath);
- while(true)
- {
- boolean searchChildren = _containsSubtree(_currPath);
- boolean hasMore = _advanceToNextItem(model, 0, searchChildren);
- if (!hasMore)
- return null;
-
- _currPath = (E) model.getRowKey();
- if (contains(_currPath))
- return _currPath;
- }
- } finally
- {
- model.setRowKey(oldPath);
- }
- }
-
- private E _value;
- private E _currPath = null;
- }
-
- private Node<E> _root;
- private transient TreeModel _model = null;
-}
+package org.apache.myfaces.adf.model;
+import java.io.Serializable;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map.Entry;
+import java.util.NoSuchElementException;
+import java.util.Set;
+
+/**
+ * Implements a collection of rowKeys from a TreeModel.
+ * The methods on this class are optimized such that it is possible
+ * to add/remove all the rowkeys in a subtree in constant time.
+ * <P>
+ * The generic type E is the type of a rowKey.
+ * @author The Oracle ADF Faces Team
+ */
+public class RowKeySetTreeImpl<E> extends RowKeySet<E> implements Serializable
+{
+ /**
+ * Creates a new Set that is initially empty.
+ */
+ public RowKeySetTreeImpl()
+ {
+ this(false);
+ }
+
+ /**
+ * Creates a new Set, that may contain every rowKey by default.
+ * @param addAll if this is true, every rowKey is initially added to this set.
+ */
+ public RowKeySetTreeImpl(boolean addAll)
+ {
+ _root = new Node<E>(addAll);
+ }
+
+ /**
+ * Tests to see if the given rowKey is included in this Set.
+ * @return true If the rowKey is included in this Set.
+ */
+ @Override
+ public boolean contains(Object rowKey)
+ {
+ return _isContained((E) rowKey);
+ }
+
+ /**
+ * @deprecated do not use. this will be removed post Tier 1.
+ */
+ public boolean isContainedByDefault()
+ {
+ TreeModel model = getCollectionModel();
+ E rowkey = (E) model.getRowKey();
+ return new Search().find(rowkey).isDefaultContained;
+ }
+
+ public Iterator<E> iterator()
+ {
+ return new PathIterator();
+ }
+
+ /**
+ * Adds the given rowKey to this Set.
+ * @return false if the given rowKey was already in this Set.
+ * @see #remove(Object)
+ * @see #addAll()
+ */
+ public boolean add(E rowKey)
+ {
+ return _setContained(rowKey, true);
+ }
+
+ /**
+ * Removes the given rowKey from this Set.
+ * @return false if the given rowKey was already not in this Set.
+ * @see #add(E)
+ * @see #removeAll()
+ */
+ @Override
+ public boolean remove(Object rowKey)
+ {
+ return _setContained((E) rowKey, false);
+ }
+
+ /**
+ * Adds the current rowKey and all rowKeys beneath the current rowKey to this Set.
+ * This method executes in constant time.
+ * @see #add(E)
+ * @see #removeAll()
+ */
+ public void addAll()
+ {
+ _selectAll(true);
+ }
+
+ /**
+ * Removes the current rowKey and all rowKeys beneath the current rowKey to this Set.
+ * This method executes in constant time.
+ * @see #remove(Object)
+ * @see #clear()
+ * @see #addAll()
+ */
+ public void removeAll()
+ {
+ _selectAll(false);
+ }
+
+ /**
+ * {@inheritDoc}
+ * <P>
+ * If the parameter is another RowKeySetTreeImpl, this method is
+ * optimized to give superior performance and avoid iteration.
+ */
+ @Override
+ public boolean addAll(Collection<? extends E> other)
+ {
+ if (other instanceof RowKeySetTreeImpl)
+ {
+ RowKeySetTreeImpl otherset = (RowKeySetTreeImpl) other;
+ return _processOperation(this._root, otherset._root, true);
+ }
+ return super.addAll(other);
+ }
+
+ /**
+ * {@inheritDoc}
+ * <P>
+ * If the parameter is another RowKeySetTreeImpl, this method is
+ * optimized to give superior performance and avoid iteration.
+ */
+ @Override
+ public boolean removeAll(Collection<?> other)
+ {
+ if (other instanceof RowKeySetTreeImpl)
+ {
+ RowKeySetTreeImpl otherset = (RowKeySetTreeImpl) other;
+ return _processOperation(this._root, otherset._root, false);
+ }
+ return super.removeAll(other);
+ }
+
+ private boolean _processOperation(Node<E> set1, Node<E> set2, boolean add)
+ {
+ /*
+ * setXdef = setX.isDefaultContained
+ * setXdif = setX.isDifferent
+ * asterisks (*) indicate changes.
+ *
+ * TABLE ---------------------------------------------------
+ |-----------Inputs---------|--------Outputs---------------|
+ | set1def | set2def | add | removeAll | addAll | set1def |
+ | 0 | 0 | 1 | 0 | 1 | 0 |
+ | 0 | 1 | 1 | 1 | 1 | 1* |
+ | 1 | 0 | 1 | 0 | 0 | 1 |
+ | 1 | 1 | 1 | 1 | 0 | 1 |
+ | 0 | 0 | 0 | 0 | 0 | 0 |
+ | 0 | 1 | 0 | 1 | 0 | 0 |
+ | 1 | 0 | 0 | 0 | 1 | 1 |
+ | 1 | 1 | 0 | 1 | 1 | 0* |
+ |---------------------------------------------------------|
+ */
+
+ boolean hasChanges = false;
+
+ // See TABLE (above) 'removeAll' column:
+ // if set2 contains everything, then there is no point hanging on to
+ // any set1-deltas that are not in set2, so remove them:
+ if (set2.isDefaultContained && set1.keySet().retainAll(set2.keySet()))
+ hasChanges = true;
+
+ // See TABLE (above) 'addAll' column:
+ // this "addAll" flag controls whether to process any set2-deltas that are not
+ // already in set1. If set1 has everything by default and we're adding set2,
+ // then there is no point processing any set2-deltas not already in set1.
+ // Similarly, if set1 has nothing by default and we're removing set2,
+ // then there is no point processing any set2-deltas not already in set1.
+ // So only process the set2-deltas if we're doing an add (and set1
+ // does not contain everything) or we're doing a remove (and set1
+ // has everything):
+ boolean addAll = add ^ set1.isDefaultContained;
+
+ for(Entry<E, Node<E>> en:set2.entrySet())
+ {
+ E segment = en.getKey();
+ Node<E> subset2 = en.getValue();
+ Node<E> subset1 = set1.get(segment);
+
+ if (subset1 == null)
+ {
+ if (addAll)
+ {
+ subset1 = new Node<E>(set1, segment);
+ hasChanges = true;
+ }
+ else
+ continue;
+ }
+ if (_processOperation(subset1, subset2, add))
+ hasChanges = true;
+ }
+
+ // See TABLE (above) 'Outputs/set1Def' column:
+ // if set2 contains everything by default, then that will affect
+ // the default flag of set1:
+ if (set2.isDefaultContained && (set1.isDefaultContained != add))
+ {
+ set1.isDefaultContained = add;
+ // since we toggled the default state, toggle the diff state
+ // as well so that we maintain the status for this node:
+ set1.isDifferent = !set1.isDifferent;
+ hasChanges = true;
+ }
+
+ // if this node is contained by set2, then depending on the
+ // add flag, this node should (not) be contained by set1:
+ if ((set2.isDefaultContained ^ set2.isDifferent) &&
+ ((set1.isDefaultContained ^ set1.isDifferent) != add))
+ {
+ set1.isDifferent = !set1.isDifferent;
+ hasChanges = true;
+ }
+
+ return hasChanges;
+ }
+
+ /**
+ * Removes all rowKeys from this Set.
+ * This method executes in the same time as
+ * {@link HashMap#clear()}
+ */
+ @Override
+ public void clear()
+ {
+ _root.clear();
+ _root.isDefaultContained = _root.isDifferent = false;
+ }
+
+ /**
+ * Gets the number of elements contained by this set.
+ * Does not force the underlying model to compute its size.
+ * @return -1 if the number of elements is unknown.
+ */
+ public int getSize()
+ {
+ return _getSize(null, _root, getCollectionModel(), false);
+ }
+
+ /**
+ * Gets the number of elements in this Set.
+ * This might force the underlying model to compute its size.
+ * @return a non-negative number.
+ */
+ public int size()
+ {
+ return _getSize(null, _root, getCollectionModel(), true);
+ }
+
+ @Override
+ public boolean isEmpty()
+ {
+ return (getSize() == 0);
+ }
+
+ /**
+ * Sets the TreeModel associated with this Set.
+ * @param model This must be of type {@link TreeModel}
+ */
+ public final void setCollectionModel(CollectionModel model)
+ {
+ _model = (TreeModel) model;
+ }
+
+ /**
+ * Creates a clone of this Set. RowKeys may be added/removed from the
+ * clone without affecting this instance.
+ */
+ public RowKeySetTreeImpl clone()
+ {
+ RowKeySetTreeImpl clone = (RowKeySetTreeImpl) super.clone();
+ clone._root = _root.clone();
+ return clone;
+ }
+
+ /**
+ * @deprecated not implemented.
+ */
+ public void invertAll()
+ {
+ // TODO
+ throw new UnsupportedOperationException("todo");
+ }
+
+ /**
+ * Gets the TreeModel associated with this set.
+ * This TreeModel will be used to get the current rowKey, and also to
+ * get parent rowKeys, from child rowKeys.
+ * @see TreeModel#getRowKey
+ */
+ protected TreeModel getCollectionModel()
+ {
+ return _model;
+ }
+
+ /**
+ * Gets the total number of nodes in the subtree of the given TreeModel.
+ *
+ * WARNING: this method changes the TreeModel's currency.
+ * The caller is responsible for restoring the model currency.
+ *
+ * @param exclusions any rowKeys present in this Set are excluded from the count.
+ */
+ private int _getTreeSize(TreeModel model, Set<E> exclusions)
+ {
+ int sz = 0;
+ for(int i=0;true;i++)
+ {
+ model.setRowIndex(i);
+ if (model.isRowAvailable())
+ {
+ E rowkey = (E) model.getRowKey();
+ if (exclusions.contains(rowkey))
+ continue;
+ sz++;
+ if (model.isContainer())
+ {
+ model.enterContainer();
+ Set<E> empty = Collections.emptySet();
+ sz += _getTreeSize(model, empty);
+ model.exitContainer();
+ }
+ }
+ else
+ return sz;
+ }
+ }
+
+ private int _getSize(E rowkey, Node<E> set, TreeModel model, boolean fetchall)
+ {
+ // special-case the root collection:
+ int sz = ((rowkey != null) && (set.isDefaultContained ^ set.isDifferent)) ? 1 : 0;
+ if (set.isDefaultContained)
+ {
+ if (!fetchall)
+ return -1;
+
+ Object old = model.getRowKey();
+ try
+ {
+ model.setRowKey(rowkey);
+ // special-case the root collection:
+ if (rowkey == null)
+ {
+ sz += _getTreeSize(model, set.keySet());
+ }
+ else if (model.isContainer())
+ {
+ model.enterContainer();
+ sz += _getTreeSize(model, set.keySet());
+ }
+ }
+ finally
+ {
+ model.setRowKey(old);
+ }
+ }
+
+ for(Entry<E, Node<E>> en:set.entrySet())
+ {
+ E newrowkey = en.getKey();
+ Node<E> subset = en.getValue();
+ int size = _getSize(newrowkey, subset, model, fetchall);
+ if (size < 0)
+ return -1;
+ sz+= size;
+ }
+ return sz;
+ }
+
+ /**
+ * adds or removes all the paths rooted at the current path
+ * @param isSelectAll if true does an add-all. else does remove-all.
+ */
+ private void _selectAll(final boolean isSelectAll)
+ {
+ Search search = new Search()
+ {
+ protected boolean create(Node<E> parent, E rowkey)
+ {
+ // if the parent does not have the correct default, then
+ // we need to add entries for the children, since we need
+ // to store a delta:
+ return (parent.isDefaultContained != isSelectAll);
+ }
+
+ protected Node<E> found(Node<E> child)
+ {
+ child.isDefaultContained = isSelectAll;
+ child.isDifferent = false;
+ child.clear();
+ return null;
+ }
+ };
+
+ TreeModel model = getCollectionModel();
+ E rowkey = (E) model.getRowKey();
+ search.find(rowkey);
+ }
+
+ private boolean _isContained(E rowkey)
+ {
+ Search search = new Search()
+ {
+ protected Node<E> notFound(Node<E> parent, E rowkey)
+ {
+ return parent.isDefaultContained ? parent : null;
+ }
+
+ protected Node<E> found(Node<E> child)
+ {
+ return (child.isDefaultContained ^ child.isDifferent) ? child : null;
+ }
+ };
+
+ return (search.find(rowkey) != null);
+ }
+
+ /**
+ * Adds or removes the given path from this set.
+ * @param isContained If true, the current path is added. Otherwise,
+ * it is removed.
+ * @return true if this Set changed due to this operation.
+ */
+ private boolean _setContained(E rowkey, final boolean isContained)
+ {
+ Search search = new Search()
+ {
+ protected boolean create(Node<E> parent, E rowkey)
+ {
+ // only need to create child deltas, if the parent's
+ // default is wrong:
+ return parent.isDefaultContained != isContained;
+ }
+
+ protected Node<E> notFound(Node<E> parent, E rowkey)
+ {
+ return null;
+ }
+ };
+
+ Node<E> current = search.find(rowkey);
+ if ((current != null) &&
+ ((current.isDefaultContained ^ current.isDifferent) != isContained))
+ {
+ current.isDifferent = !current.isDifferent;
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Advances the currency of the given TreeModel to the next node in a
+ * depth-first walk of the tree.
+ * @param minDepth the minimum depth of the rowkey. use this to
+ * walk within a subtree. Use 0 to walk entire tree.
+ * @param recurseChildren if true, will walk children.
+ * @return true if the currency of the model was successfully advanced to
+ * the next rowData.
+ */
+ private static boolean _advanceToNextItem(
+ TreeModel model, int minDepth, boolean recurseChildren)
+ {
+ assert minDepth >= 0;
+
+ if (recurseChildren && model.isRowAvailable() && model.isContainer())
+ {
+ model.enterContainer();
+ model.setRowIndex(-1);
+ }
+ while(true)
+ {
+ int ri = model.getRowIndex();
+ model.setRowIndex(ri+1);
+ if (model.isRowAvailable())
+ return true;
+
+ int depth = model.getDepth();
+ if (depth <= minDepth)
+ return false;
+
+ model.exitContainer();
+ }
+ }
+
+ private static final class Node<K> extends HashMap<K, Node<K>>
+ implements Serializable, Cloneable
+ {
+ public boolean isDifferent = false;
+ public boolean isDefaultContained = false;
+
+ public Node(boolean isDefaultContained)
+ {
+ this.isDefaultContained = isDefaultContained;
+ }
+
+ public Node(Node<K> parent, K segment)
+ {
+ this(parent.isDefaultContained);
+ parent.put(segment, this);
+ }
+
+ // clone all the values as well:
+ private void _deepClone(Node<K> root)
+ {
+ for(Entry<K, Node<K>> en:root.entrySet())
+ {
+ Node<K> original = en.getValue();
+ Node<K> clone = original.clone();
+ en.setValue(clone);
+ }
+ }
+
+ public Node<K> clone()
+ {
+ Node<K> clone = (Node<K>) super.clone();
+ _deepClone(clone);
+ return clone;
+ }
+ }
+
+ private class Search
+ {
+ public Search()
+ {
+ }
+
+ protected boolean create(Node<E> parent, E rowkey)
+ {
+ return false;
+ }
+
+ protected Node<E> notFound(Node<E> parent, E rowkey)
+ {
+ return parent;
+ }
+
+ protected Node<E> found(Node<E> result)
+ {
+ return result;
+ }
+
+ public Node<E> find(E rowkey)
+ {
+ Node<E> current = _root;
+ if (rowkey != null)
+ {
+ TreeModel model = getCollectionModel();
+ List<E> parentkeys = model.getAllAncestorContainerRowKeys(rowkey);
+ List<E> allkeys = new ArrayList<E>(parentkeys.size() + 1);
+ allkeys.addAll(parentkeys);
+ allkeys.add(rowkey);
+ for(E key:allkeys)
+ {
+ Node<E> next = current.get(key);
+ if (next == null)
+ {
+ if (create(current, key))
+ next = new Node<E>(current, key);
+ else
+ return notFound(current, key);
+ }
+ current = next;
+ }
+ }
+ return found(current);
+ }
+ }
+
+ private final class PathIterator implements Iterator<E>
+ {
+ PathIterator()
+ {
+ _value = _next(); // initialize;
+ }
+
+ public E next()
+ {
+ if (!hasNext())
+ throw new NoSuchElementException();
+ E value = _value;
+ _value = _next();
+ return value;
+ }
+
+ public boolean hasNext()
+ {
+ return (_value != null);
+ }
+
+ public void remove()
+ {
+ throw new UnsupportedOperationException();
+ }
+
+ private boolean _containsSubtree(E rowkey)
+ {
+ Search search = new Search()
+ {
+ protected Node<E> notFound(Node<E> parent, E rowkey)
+ {
+ return parent.isDefaultContained ? parent : null;
+ }
+ };
+ Node<E> current = search.find(rowkey);
+ return (current != null) &&
+ ((!current.isEmpty()) || current.isDefaultContained);
+ }
+
+ private E _next()
+ {
+ TreeModel model = getCollectionModel();
+ if (model == null)
+ return null;
+
+ Object oldPath = model.getRowKey();
+ try
+ {
+ model.setRowKey(_currPath);
+ while(true)
+ {
+ boolean searchChildren = _containsSubtree(_currPath);
+ boolean hasMore = _advanceToNextItem(model, 0, searchChildren);
+ if (!hasMore)
+ return null;
+
+ _currPath = (E) model.getRowKey();
+ if (contains(_currPath))
+ return _currPath;
+ }
+ } finally
+ {
+ model.setRowKey(oldPath);
+ }
+ }
+
+ private E _value;
+ private E _currPath = null;
+ }
+
+ private Node<E> _root;
+ private transient TreeModel _model = null;
+}
Propchange: incubator/adffaces/branches/matzew-repackaging-trinidad/trinidad/trinidad-api/src/main/java/org/apache/myfaces/adf/model/RowKeySetTreeImpl.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: incubator/adffaces/branches/matzew-repackaging-trinidad/trinidad/trinidad-api/src/main/java/org/apache/myfaces/adf/model/SortCriterion.java
URL: http://svn.apache.org/viewvc/incubator/adffaces/branches/matzew-repackaging-trinidad/trinidad/trinidad-api/src/main/java/org/apache/myfaces/adf/model/SortCriterion.java?rev=425268&r1=425267&r2=425268&view=diff
==============================================================================
--- incubator/adffaces/branches/matzew-repackaging-trinidad/trinidad/trinidad-api/src/main/java/org/apache/myfaces/adf/model/SortCriterion.java (original)
+++ incubator/adffaces/branches/matzew-repackaging-trinidad/trinidad/trinidad-api/src/main/java/org/apache/myfaces/adf/model/SortCriterion.java Mon Jul 24 20:54:50 2006
@@ -13,70 +13,70 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.myfaces.adf.model;
-import java.io.Serializable;
-
-/**
- * This class pairs together a property and a direction by which a
- * CollectionModel can be sorted.
- * @see CollectionModel#getSortCriteria
- * @author The Oracle ADF Faces Team
- */
-public final class SortCriterion implements Serializable
-{
- public SortCriterion(String property, boolean isAscending)
- {
- if (property == null)
- throw new NullPointerException("property is null");
-
- _property = property;
- _sortOrder = isAscending;
- }
-
- /**
- * Gets the direction in which the property of this class is sorted.
- * @return true if the property identified by this class is sorted in
- * ascending order.
- */
- public boolean isAscending()
- {
- return _sortOrder;
- }
-
- /**
- * Gets the property that is identified by this class. This is the property
- * that must be sorted by. If a collection of beans is being sorted, bean rules
- * will be used to find a suitable getter method that matches this property.
- * The value returned by the getter method will be sorted on.
- * If a collection of Maps is being sorted, this property will be used
- * as the key into each Map to get at the value being sorted.
- */
- public String getProperty()
- {
- return _property;
- }
-
- public boolean equals(Object obj)
- {
- if (this == obj)
- return true;
-
- if (obj instanceof SortCriterion)
- {
- SortCriterion that = (SortCriterion) obj;
- return (this.getProperty().equals(that.getProperty())) &&
- (this.isAscending() == that.isAscending());
- }
-
- return false;
- }
-
- public int hashCode()
- {
- int hc = getProperty().hashCode();
- return isAscending() ? hc : -hc;
- }
-
- private final String _property;
- private final boolean _sortOrder;
+package org.apache.myfaces.adf.model;
+import java.io.Serializable;
+
+/**
+ * This class pairs together a property and a direction by which a
+ * CollectionModel can be sorted.
+ * @see CollectionModel#getSortCriteria
+ * @author The Oracle ADF Faces Team
+ */
+public final class SortCriterion implements Serializable
+{
+ public SortCriterion(String property, boolean isAscending)
+ {
+ if (property == null)
+ throw new NullPointerException("property is null");
+
+ _property = property;
+ _sortOrder = isAscending;
+ }
+
+ /**
+ * Gets the direction in which the property of this class is sorted.
+ * @return true if the property identified by this class is sorted in
+ * ascending order.
+ */
+ public boolean isAscending()
+ {
+ return _sortOrder;
+ }
+
+ /**
+ * Gets the property that is identified by this class. This is the property
+ * that must be sorted by. If a collection of beans is being sorted, bean rules
+ * will be used to find a suitable getter method that matches this property.
+ * The value returned by the getter method will be sorted on.
+ * If a collection of Maps is being sorted, this property will be used
+ * as the key into each Map to get at the value being sorted.
+ */
+ public String getProperty()
+ {
+ return _property;
+ }
+
+ public boolean equals(Object obj)
+ {
+ if (this == obj)
+ return true;
+
+ if (obj instanceof SortCriterion)
+ {
+ SortCriterion that = (SortCriterion) obj;
+ return (this.getProperty().equals(that.getProperty())) &&
+ (this.isAscending() == that.isAscending());
+ }
+
+ return false;
+ }
+
+ public int hashCode()
+ {
+ int hc = getProperty().hashCode();
+ return isAscending() ? hc : -hc;
+ }
+
+ private final String _property;
+ private final boolean _sortOrder;
}
Propchange: incubator/adffaces/branches/matzew-repackaging-trinidad/trinidad/trinidad-api/src/main/java/org/apache/myfaces/adf/model/SortCriterion.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: incubator/adffaces/branches/matzew-repackaging-trinidad/trinidad/trinidad-api/src/main/java/org/apache/myfaces/adf/model/ViewIdPropertyMenuModel.java
URL: http://svn.apache.org/viewvc/incubator/adffaces/branches/matzew-repackaging-trinidad/trinidad/trinidad-api/src/main/java/org/apache/myfaces/adf/model/ViewIdPropertyMenuModel.java?rev=425268&r1=425267&r2=425268&view=diff
==============================================================================
--- incubator/adffaces/branches/matzew-repackaging-trinidad/trinidad/trinidad-api/src/main/java/org/apache/myfaces/adf/model/ViewIdPropertyMenuModel.java (original)
+++ incubator/adffaces/branches/matzew-repackaging-trinidad/trinidad/trinidad-api/src/main/java/org/apache/myfaces/adf/model/ViewIdPropertyMenuModel.java Mon Jul 24 20:54:50 2006
@@ -13,211 +13,211 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.myfaces.adf.model;
-
-import java.beans.IntrospectionException;
-
-import java.util.HashMap;
-import java.util.Map;
-
-import javax.faces.context.FacesContext;
-import javax.faces.el.PropertyResolver;
-
-import org.apache.myfaces.adf.logging.ADFLogger;
-
-
-/**
- * Creates a MenuModel from a TreeModel where nodes in the treeModel contain
- * viewId information.
- * <p>
- * Each node must have either a bean getter method or a Map property
- * that returns a viewId. There are several restrictions on the data:
- * <ul>
- * <li>The nodes in the tree must either be all beans or all maps,
- * but not a mix of beans and maps.
- * <li>The viewId of a node can be null, but if set it must be unique.
- * <li>The tree cannot be mutable.
- * </ul>
- * The getFocusPath method
- * <ul>
- * <li>gets the current viewId by calling
- * FacesContext.getCurrentInstance().getViewRoot().getViewId()
- * <li> compares the current viewId with the viewId's in
- * the nodes of the tree
- * <li>returns the path to the node with the current viewId or null if the current viewId can't be found
- * </ul>
- * <p>
- * Assuming that NavigationTree is a tree of beans with a vieId getter, an
- * example of creating a MenuModel with this class might look like:
- * <pre><code>
- * CollectionModel collectionModel = ModelUtils.toCollectionModel(new NavigationTree());
- * TreeModel treeModel = new ChildPropertyTreeModel(collectionModel, "children");
- * MenuModel menuModel = new ViewIdPropertyMenuModel(treeModel, "viewId");
- * </code></pre>
- *
- * @author The Oracle ADF Faces Team
- * @todo - support for mutable trees?
- */
-public class ViewIdPropertyMenuModel extends BaseMenuModel
-{
- /**
- * No-arg constructor for use with managed-beans.
- * Must call the {@link #setViewIdProperty} and
- * {@link #setWrappedData} methods after constructing this instance.
- */
- public ViewIdPropertyMenuModel()
- {
- super();
- _focusPathMap = new HashMap();
- }
- /**
- *
- * @param instance a treeModel. This object will be passed to
- * {@link ModelUtils#toTreeModel}
- * @param viewIdProperty the property to use to retrieve a viewId
- * from a node in the tree
- * @throws IntrospectionException
- */
- public ViewIdPropertyMenuModel(Object instance, String viewIdProperty)
- throws IntrospectionException
- {
- super(instance);
- _focusPathMap = new HashMap();
- setViewIdProperty(viewIdProperty);
- setWrappedData(instance);
- }
-
- public void setWrappedData(Object data)
- {
- super.setWrappedData(data);
- Object oldPath = getRowKey();
-
- //set the focus path map
- _focusPathMap.clear();
- setRowKey(null);
- FacesContext context = FacesContext.getCurrentInstance();
- _addToMap(context, this, _focusPathMap, getViewIdProperty());
- setRowKey(oldPath);
- }
-
- /**
- * Returns the rowKey to the current viewId.
- * <p>
- *
- * The getFocusRowKey method
- * <ul>
- * <li>gets the current viewId by calling
- * FacesContext.getCurrentInstance().getViewRoot().getViewId()
- * <li> compares the current viewId with the viewId's in
- * the nodes of the tree
- * <li>returns the rowKey to the node with the current viewId or null
- * if the current viewId can't be found
- * </ul>
- *
- * @return the rowKey to the node with the current viewId or null if the current viewId can't be found
- */
-
- public Object getFocusRowKey()
- {
- String currentViewId = getCurrentViewId();
- Object focusPath = _focusPathMap.get(currentViewId);
- return focusPath;
- }
-
- /**
- * Maps the focusPath returned when the viewId is newViewId
- * to the focusPath returned when the viewId is aliasedViewId.
- * This allows view id's not in the treeModel to be mapped
- * to a focusPath.
- * @param newViewId the view id to add a focus path for
- * @param aliasedViewId the view id to use to get the focusPath to use for newViewId
- */
- public void addViewId(
- String newViewId,
- String aliasedViewId
- )
- {
- Object focusPath = _focusPathMap.get(aliasedViewId);
- if (focusPath != null)
- {
- _focusPathMap.put(newViewId, focusPath);
- }
- }
-
- /**
- * Gets the property to use to retrieve a viewId
- * from a node in the tree
- */
- public String getViewIdProperty()
- {
- return _viewIdProperty;
- }
-
- /**
- * Sets the property to use to retrieve a viewId
- * from a node in the tree
- */
- public void setViewIdProperty(String viewIdProperty)
- {
- _viewIdProperty = viewIdProperty;
- }
-
- /**
- * Returns the current viewId.
- * <p>
- *
- *
- * @return the current viewId or null if the current viewId can't be found
- */
-
- protected String getCurrentViewId()
- {
- String currentViewId =
- FacesContext.getCurrentInstance().getViewRoot().getViewId();
-
- return currentViewId;
- }
-
-
- private static void _addToMap(
- FacesContext context,
- TreeModel tree,
- Map focusPathMap,
- String viewIdProperty
- )
- {
- for ( int i = 0; i < tree.getRowCount(); i++)
- {
- tree.setRowIndex(i);
- if (viewIdProperty != null)
- {
- Object focusPath = tree.getRowKey();
- Object data = tree.getRowData();
- PropertyResolver resolver =
- context.getApplication().getPropertyResolver();
- Object viewIdObject = resolver.getValue(data, viewIdProperty);
- focusPathMap.put(viewIdObject, focusPath);
- }
- else
- {
- _LOG.warning("The viewId property in ViewIdPropertyMenuModel is null. The viewId property is needed to find the focus rowKey." );
- }
-
- if (tree.isContainer() && !tree.isContainerEmpty())
- {
- tree.enterContainer();
- _addToMap(context, tree, focusPathMap, viewIdProperty);
- tree.exitContainer();
- }
-
- }
- }
-
-
- private final Map _focusPathMap;
- private String _viewIdProperty = null;
-
-
-
- static private final ADFLogger _LOG = ADFLogger.createADFLogger(ViewIdPropertyMenuModel.class);
-}
+package org.apache.myfaces.adf.model;
+
+import java.beans.IntrospectionException;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.faces.context.FacesContext;
+import javax.faces.el.PropertyResolver;
+
+import org.apache.myfaces.adf.logging.ADFLogger;
+
+
+/**
+ * Creates a MenuModel from a TreeModel where nodes in the treeModel contain
+ * viewId information.
+ * <p>
+ * Each node must have either a bean getter method or a Map property
+ * that returns a viewId. There are several restrictions on the data:
+ * <ul>
+ * <li>The nodes in the tree must either be all beans or all maps,
+ * but not a mix of beans and maps.
+ * <li>The viewId of a node can be null, but if set it must be unique.
+ * <li>The tree cannot be mutable.
+ * </ul>
+ * The getFocusPath method
+ * <ul>
+ * <li>gets the current viewId by calling
+ * FacesContext.getCurrentInstance().getViewRoot().getViewId()
+ * <li> compares the current viewId with the viewId's in
+ * the nodes of the tree
+ * <li>returns the path to the node with the current viewId or null if the current viewId can't be found
+ * </ul>
+ * <p>
+ * Assuming that NavigationTree is a tree of beans with a vieId getter, an
+ * example of creating a MenuModel with this class might look like:
+ * <pre><code>
+ * CollectionModel collectionModel = ModelUtils.toCollectionModel(new NavigationTree());
+ * TreeModel treeModel = new ChildPropertyTreeModel(collectionModel, "children");
+ * MenuModel menuModel = new ViewIdPropertyMenuModel(treeModel, "viewId");
+ * </code></pre>
+ *
+ * @author The Oracle ADF Faces Team
+ * @todo - support for mutable trees?
+ */
+public class ViewIdPropertyMenuModel extends BaseMenuModel
+{
+ /**
+ * No-arg constructor for use with managed-beans.
+ * Must call the {@link #setViewIdProperty} and
+ * {@link #setWrappedData} methods after constructing this instance.
+ */
+ public ViewIdPropertyMenuModel()
+ {
+ super();
+ _focusPathMap = new HashMap();
+ }
+ /**
+ *
+ * @param instance a treeModel. This object will be passed to
+ * {@link ModelUtils#toTreeModel}
+ * @param viewIdProperty the property to use to retrieve a viewId
+ * from a node in the tree
+ * @throws IntrospectionException
+ */
+ public ViewIdPropertyMenuModel(Object instance, String viewIdProperty)
+ throws IntrospectionException
+ {
+ super(instance);
+ _focusPathMap = new HashMap();
+ setViewIdProperty(viewIdProperty);
+ setWrappedData(instance);
+ }
+
+ public void setWrappedData(Object data)
+ {
+ super.setWrappedData(data);
+ Object oldPath = getRowKey();
+
+ //set the focus path map
+ _focusPathMap.clear();
+ setRowKey(null);
+ FacesContext context = FacesContext.getCurrentInstance();
+ _addToMap(context, this, _focusPathMap, getViewIdProperty());
+ setRowKey(oldPath);
+ }
+
+ /**
+ * Returns the rowKey to the current viewId.
+ * <p>
+ *
+ * The getFocusRowKey method
+ * <ul>
+ * <li>gets the current viewId by calling
+ * FacesContext.getCurrentInstance().getViewRoot().getViewId()
+ * <li> compares the current viewId with the viewId's in
+ * the nodes of the tree
+ * <li>returns the rowKey to the node with the current viewId or null
+ * if the current viewId can't be found
+ * </ul>
+ *
+ * @return the rowKey to the node with the current viewId or null if the current viewId can't be found
+ */
+
+ public Object getFocusRowKey()
+ {
+ String currentViewId = getCurrentViewId();
+ Object focusPath = _focusPathMap.get(currentViewId);
+ return focusPath;
+ }
+
+ /**
+ * Maps the focusPath returned when the viewId is newViewId
+ * to the focusPath returned when the viewId is aliasedViewId.
+ * This allows view id's not in the treeModel to be mapped
+ * to a focusPath.
+ * @param newViewId the view id to add a focus path for
+ * @param aliasedViewId the view id to use to get the focusPath to use for newViewId
+ */
+ public void addViewId(
+ String newViewId,
+ String aliasedViewId
+ )
+ {
+ Object focusPath = _focusPathMap.get(aliasedViewId);
+ if (focusPath != null)
+ {
+ _focusPathMap.put(newViewId, focusPath);
+ }
+ }
+
+ /**
+ * Gets the property to use to retrieve a viewId
+ * from a node in the tree
+ */
+ public String getViewIdProperty()
+ {
+ return _viewIdProperty;
+ }
+
+ /**
+ * Sets the property to use to retrieve a viewId
+ * from a node in the tree
+ */
+ public void setViewIdProperty(String viewIdProperty)
+ {
+ _viewIdProperty = viewIdProperty;
+ }
+
+ /**
+ * Returns the current viewId.
+ * <p>
+ *
+ *
+ * @return the current viewId or null if the current viewId can't be found
+ */
+
+ protected String getCurrentViewId()
+ {
+ String currentViewId =
+ FacesContext.getCurrentInstance().getViewRoot().getViewId();
+
+ return currentViewId;
+ }
+
+
+ private static void _addToMap(
+ FacesContext context,
+ TreeModel tree,
+ Map focusPathMap,
+ String viewIdProperty
+ )
+ {
+ for ( int i = 0; i < tree.getRowCount(); i++)
+ {
+ tree.setRowIndex(i);
+ if (viewIdProperty != null)
+ {
+ Object focusPath = tree.getRowKey();
+ Object data = tree.getRowData();
+ PropertyResolver resolver =
+ context.getApplication().getPropertyResolver();
+ Object viewIdObject = resolver.getValue(data, viewIdProperty);
+ focusPathMap.put(viewIdObject, focusPath);
+ }
+ else
+ {
+ _LOG.warning("The viewId property in ViewIdPropertyMenuModel is null. The viewId property is needed to find the focus rowKey." );
+ }
+
+ if (tree.isContainer() && !tree.isContainerEmpty())
+ {
+ tree.enterContainer();
+ _addToMap(context, tree, focusPathMap, viewIdProperty);
+ tree.exitContainer();
+ }
+
+ }
+ }
+
+
+ private final Map _focusPathMap;
+ private String _viewIdProperty = null;
+
+
+
+ static private final ADFLogger _LOG = ADFLogger.createADFLogger(ViewIdPropertyMenuModel.class);
+}
Propchange: incubator/adffaces/branches/matzew-repackaging-trinidad/trinidad/trinidad-api/src/main/java/org/apache/myfaces/adf/model/ViewIdPropertyMenuModel.java
------------------------------------------------------------------------------
svn:eol-style = native