Permalink
Browse files

Added a few convenience methodsto SmartList.

  • Loading branch information...
1 parent 9b06d28 commit 0c6d701c55fb01e82ae8a2bfa361f4d91a95d7a4 Nicolas Gramlich committed Apr 19, 2012
@@ -44,6 +44,22 @@ public SmartList(final int pCapacity) {
// Methods for/from SuperClass/Interfaces
// ===========================================================
+ public void addFirst(final T pItem) {
+ this.add(0, pItem);
+ }
+
+ public void addLast(final T pItem) {
+ this.add(this.size(), pItem);
+ }
+
+ public T getFirst() throws IndexOutOfBoundsException {
+ return this.get(0);
+ }
+
+ public T getLast() throws IndexOutOfBoundsException {
+ return this.get(this.size() - 1);
+ }
+
public T get(final IMatcher<T> pMatcher) {
final int size = this.size();
for(int i = 0; i < size; i++) {
@@ -55,6 +71,14 @@ public T get(final IMatcher<T> pMatcher) {
return null;
}
+ public T removeFirst() throws IndexOutOfBoundsException {
+ return this.remove(0);
+ }
+
+ public T removeLast() throws IndexOutOfBoundsException {
+ return this.remove(this.size() - 1);
+ }
+
/**
* @param pItem the item to remove.
* @param pParameterCallable to be called with the removed item, if it was removed.
@@ -0,0 +1,22 @@
+package org.andengine.util.level;
+
+import org.andengine.entity.IEntity;
+import org.xml.sax.Attributes;
+
+/**
+ * (c) Zynga 2012
+ *
+ * @author Nicolas Gramlich <ngramlich@zynga.com>
+ * @since 18:38:40 - 18.04.2012
+ */
+public interface IEntityLoader {
+ // ===========================================================
+ // Constants
+ // ===========================================================
+
+ // ===========================================================
+ // Methods
+ // ===========================================================
+
+ public IEntity onLoadEntity(final String pEntityName, final Attributes pAttributes);
+}
@@ -9,6 +9,7 @@
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
+import org.andengine.entity.IEntity;
import org.andengine.util.StreamUtils;
import org.andengine.util.debug.Debug;
import org.xml.sax.Attributes;
@@ -140,16 +141,4 @@ public void loadLevelFromStream(final InputStream pInputStream) throws IOExcepti
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
-
- public static interface IEntityLoader {
- // ===========================================================
- // Constants
- // ===========================================================
-
- // ===========================================================
- // Methods
- // ===========================================================
-
- public void onLoadEntity(final String pEntityName, final Attributes pAttributes);
- }
}
@@ -2,7 +2,8 @@
import java.util.HashMap;
-import org.andengine.util.level.LevelLoader.IEntityLoader;
+import org.andengine.entity.IEntity;
+import org.andengine.util.adt.list.SmartList;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
@@ -26,6 +27,8 @@
private final IEntityLoader mDefaultEntityLoader;
private final HashMap<String, IEntityLoader> mEntityLoaders;
+ private SmartList<IEntity> mParentEntityStack = new SmartList<IEntity>();
+
// ===========================================================
// Constructors
// ===========================================================
@@ -45,16 +48,31 @@ public LevelParser(final IEntityLoader pDefaultEntityLoader, final HashMap<Strin
@Override
public void startElement(final String pUri, final String pLocalName, final String pQualifiedName, final Attributes pAttributes) throws SAXException {
- final IEntityLoader entityLoader = this.mEntityLoaders.get(pLocalName);
+ final String entityName = pLocalName;
+
+ final IEntity parent = (this.mParentEntityStack.isEmpty()) ? null : this.mParentEntityStack.getLast();
+
+ final IEntityLoader entityLoader = this.mEntityLoaders.get(entityName);
+
+ final IEntity entity;
if(entityLoader != null) {
- entityLoader.onLoadEntity(pLocalName, pAttributes);
+ entity = entityLoader.onLoadEntity(entityName, pAttributes);
+ } else if(this.mDefaultEntityLoader != null) {
+ entity = this.mDefaultEntityLoader.onLoadEntity(entityName, pAttributes);
} else {
- if(this.mDefaultEntityLoader != null) {
- this.mDefaultEntityLoader.onLoadEntity(pLocalName, pAttributes);
- } else {
- throw new IllegalArgumentException("Unexpected tag: '" + pLocalName + "'.");
- }
+ throw new IllegalArgumentException("Unexpected tag: '" + entityName + "'.");
+ }
+
+ if(parent != null && entity != null) {
+ parent.attachChild(entity);
}
+
+ this.mParentEntityStack.addLast(entity);
+ }
+
+ @Override
+ public void endElement(final String pUri, final String pLocalName, final String pQualifiedName) throws SAXException {
+ this.mParentEntityStack.removeLast();
}
// ===========================================================

0 comments on commit 0c6d701

Please sign in to comment.