You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by ma...@apache.org on 2008/08/20 15:09:14 UTC

svn commit: r687312 - in /myfaces/trinidad/trunk_1.2.x/trinidad-api/src/main/java/org/apache/myfaces/trinidad/component: CompositeIterator.java UIXComponentBase.java

Author: matzew
Date: Wed Aug 20 06:09:14 2008
New Revision: 687312

URL: http://svn.apache.org/viewvc?rev=687312&view=rev
Log:
TRINIDAD-1164 - getFacetsAndChildren from UIXComponentBase allocates a lot of memory

Thanks to Blake Sullivan for his patch

Added:
    myfaces/trinidad/trunk_1.2.x/trinidad-api/src/main/java/org/apache/myfaces/trinidad/component/CompositeIterator.java
Modified:
    myfaces/trinidad/trunk_1.2.x/trinidad-api/src/main/java/org/apache/myfaces/trinidad/component/UIXComponentBase.java

Added: myfaces/trinidad/trunk_1.2.x/trinidad-api/src/main/java/org/apache/myfaces/trinidad/component/CompositeIterator.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk_1.2.x/trinidad-api/src/main/java/org/apache/myfaces/trinidad/component/CompositeIterator.java?rev=687312&view=auto
==============================================================================
--- myfaces/trinidad/trunk_1.2.x/trinidad-api/src/main/java/org/apache/myfaces/trinidad/component/CompositeIterator.java (added)
+++ myfaces/trinidad/trunk_1.2.x/trinidad-api/src/main/java/org/apache/myfaces/trinidad/component/CompositeIterator.java Wed Aug 20 06:09:14 2008
@@ -0,0 +1,93 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.apache.myfaces.trinidad.component;
+
+import java.util.Iterator;
+
+/** not thread safe, though it could be as thread-safe as the underlying Iterators at a slight cost to performance and size */
+public final class CompositeIterator<T> implements Iterator<T>
+{
+  public CompositeIterator(Iterator<T> firstIterator, Iterator<T> secondIterator)
+  {
+    if (firstIterator == null)
+      throw new NullPointerException();
+
+    if (secondIterator == null)
+      throw new NullPointerException();
+
+    // determine whether we have another element aggressively
+    _hasNext = firstIterator.hasNext();
+
+    if (_hasNext)
+    {
+      _firstIterator = firstIterator;
+      _secondIterator = secondIterator;
+    }
+    else
+    {
+      // first iterator is empty so shift up the second iterator
+      _firstIterator = secondIterator;
+      _secondIterator = null;
+      _hasNext = secondIterator.hasNext();
+    }
+  }
+
+  public boolean hasNext()
+  {
+    // return cached value
+    return _hasNext;
+  }
+
+  public T next()
+  {
+    // remove is only valid after next(), so assign it here.
+    // This also deals with issue where next() is called on the
+    // last element of the firstIterator and then remove is called
+    // this way the removeIterator correctly lags shifting from the
+    // old _firstIterator to the _secondIterator
+    _removeIterator = _firstIterator;
+
+    T nextValue = _firstIterator.next();
+    
+    _hasNext = _firstIterator.hasNext();
+
+    if (!_hasNext && (_secondIterator != null))
+    {
+      // firsIterator is done, so shift up the secondIterator
+      _firstIterator = _secondIterator;
+      _secondIterator = null;
+      _hasNext = _firstIterator.hasNext();
+    }
+
+    return nextValue;
+  }
+
+  public void remove()
+  {
+    if (_removeIterator == null)
+      throw new IllegalStateException();
+
+    _removeIterator.remove();
+  }
+
+  private Iterator<T> _firstIterator;
+  private Iterator<T> _secondIterator;
+  private Iterator<T> _removeIterator;
+  private boolean _hasNext;
+}

Modified: myfaces/trinidad/trunk_1.2.x/trinidad-api/src/main/java/org/apache/myfaces/trinidad/component/UIXComponentBase.java
URL: http://svn.apache.org/viewvc/myfaces/trinidad/trunk_1.2.x/trinidad-api/src/main/java/org/apache/myfaces/trinidad/component/UIXComponentBase.java?rev=687312&r1=687311&r2=687312&view=diff
==============================================================================
--- myfaces/trinidad/trunk_1.2.x/trinidad-api/src/main/java/org/apache/myfaces/trinidad/component/UIXComponentBase.java (original)
+++ myfaces/trinidad/trunk_1.2.x/trinidad-api/src/main/java/org/apache/myfaces/trinidad/component/UIXComponentBase.java Wed Aug 20 06:09:14 2008
@@ -21,7 +21,6 @@
 import java.io.InputStream;
 import java.io.IOException;
 import java.net.URL;
-import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
@@ -615,8 +614,6 @@
     return _facets.keySet().iterator();
   }
 
-
-  // TODO: Optimize to a compound iterator
   @Override
   public Iterator<UIComponent> getFacetsAndChildren()
   {
@@ -633,25 +630,10 @@
       if (_children == null)
         return _facets.values().iterator();
     }
-
-    ArrayList<UIComponent> childrenAndFacets = new ArrayList<UIComponent>();
-    for(UIComponent facet : _facets.values())
-    {
-      childrenAndFacets.add(facet);
-    }
-
-    for(UIComponent child : _children)
-    {
-      childrenAndFacets.add(child);
-    }
-
-    if (childrenAndFacets.isEmpty())
-      return _EMPTY_UICOMPONENT_ITERATOR;
-
-    return childrenAndFacets.iterator();
+    
+    return new CompositeIterator<UIComponent>(_children.iterator(), _facets.values().iterator());
   }
 
-
   // ------------------------------------------- Event processing methods
 
   @Override