Bug 525399 - Enhancements in ElementCache API
diff --git a/org.eclipse.handly.examples.basic.ui/src/org/eclipse/handly/internal/examples/basic/ui/model/FooModelCache.java b/org.eclipse.handly.examples.basic.ui/src/org/eclipse/handly/internal/examples/basic/ui/model/FooModelCache.java
index 43b77ac..fcaf944 100644
--- a/org.eclipse.handly.examples.basic.ui/src/org/eclipse/handly/internal/examples/basic/ui/model/FooModelCache.java
+++ b/org.eclipse.handly.examples.basic.ui/src/org/eclipse/handly/internal/examples/basic/ui/model/FooModelCache.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2014, 2016 1C-Soft LLC and others.
+ * Copyright (c) 2014, 2017 1C-Soft LLC and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -16,6 +16,7 @@
 import org.eclipse.handly.examples.basic.ui.model.IFooModel;
 import org.eclipse.handly.examples.basic.ui.model.IFooProject;
 import org.eclipse.handly.model.IElement;
+import org.eclipse.handly.model.impl.Body;
 import org.eclipse.handly.model.impl.ElementCache;
 import org.eclipse.handly.model.impl.IBodyCache;
 
@@ -80,7 +81,8 @@
         else if (element instanceof IFooProject)
         {
             projectCache.put(element, body);
-            fileCache.ensureSpaceLimit(body, element);
+            fileCache.ensureSpaceLimit(((Body)body).getChildren().length,
+                element);
         }
         else if (element instanceof IFooFile)
             fileCache.put(element, body);
diff --git a/org.eclipse.handly.examples.javamodel/src/org/eclipse/handly/internal/examples/javamodel/JavaModelCache.java b/org.eclipse.handly.examples.javamodel/src/org/eclipse/handly/internal/examples/javamodel/JavaModelCache.java
index 5888142..a3c5759 100644
--- a/org.eclipse.handly.examples.javamodel/src/org/eclipse/handly/internal/examples/javamodel/JavaModelCache.java
+++ b/org.eclipse.handly.examples.javamodel/src/org/eclipse/handly/internal/examples/javamodel/JavaModelCache.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2015, 2016 1C-Soft LLC.
+ * Copyright (c) 2015, 2017 1C-Soft LLC.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -18,6 +18,7 @@
 import org.eclipse.handly.examples.javamodel.IPackageFragment;
 import org.eclipse.handly.examples.javamodel.IPackageFragmentRoot;
 import org.eclipse.handly.model.IElement;
+import org.eclipse.handly.model.impl.Body;
 import org.eclipse.handly.model.impl.ElementCache;
 import org.eclipse.handly.model.impl.IBodyCache;
 
@@ -96,17 +97,20 @@
         else if (element instanceof IJavaProject)
         {
             projectCache.put(element, body);
-            rootCache.ensureSpaceLimit(body, element);
+            rootCache.ensureSpaceLimit(((Body)body).getChildren().length,
+                element);
         }
         else if (element instanceof IPackageFragmentRoot)
         {
             rootCache.put(element, body);
-            pkgCache.ensureSpaceLimit(body, element);
+            pkgCache.ensureSpaceLimit(((Body)body).getChildren().length,
+                element);
         }
         else if (element instanceof IPackageFragment)
         {
             pkgCache.put(element, body);
-            fileCache.ensureSpaceLimit(body, element);
+            fileCache.ensureSpaceLimit(((Body)body).getChildren().length,
+                element);
         }
         else if (element instanceof ICompilationUnit)
             fileCache.put(element, body);
diff --git a/org.eclipse.handly/src/org/eclipse/handly/model/impl/ElementCache.java b/org.eclipse.handly/src/org/eclipse/handly/model/impl/ElementCache.java
index 89f932a..9db3aff 100644
--- a/org.eclipse.handly/src/org/eclipse/handly/model/impl/ElementCache.java
+++ b/org.eclipse.handly/src/org/eclipse/handly/model/impl/ElementCache.java
@@ -59,14 +59,13 @@
     }
 
     /**
-     * Ensures that there is enough room for adding the children of the given
-     * body. If the space limit must be increased, record the parent that
-     * needed this space limit.
+     * Ensures that there is enough room for adding the given number of children.
+     * If the space limit must be increased, record the parent that needed
+     * this space limit.
      */
-    public void ensureSpaceLimit(Object body, IElement parent)
+    public void ensureSpaceLimit(int childCount, IElement parent)
     {
         // ensure the children can be put without closing other elements
-        int childCount = getChildCount(parent, body);
         int spaceNeeded = 1 + (int)((1 + loadFactor) * (childCount + overflow));
         if (spaceLimit < spaceNeeded)
         {
@@ -90,14 +89,6 @@
         }
     }
 
-    /**
-     * Given a body, returns the number of children of the given element.
-     */
-    protected int getChildCount(IElement element, Object body)
-    {
-        return ((IElementImplExtension)element).getChildren_(body).length;
-    }
-
     @Override
     protected boolean close(LruCacheEntry<IElement, Object> entry)
     {