From: <aki...@us...> - 2006-07-02 18:58:11
|
Revision: 229 Author: akirschbaum Date: 2006-07-02 11:57:56 -0700 (Sun, 02 Jul 2006) ViewCVS: http://svn.sourceforge.net/gridarta/?rev=229&view=rev Log Message: ----------- Unify ArchObjectIterator implementation; unify method names in ArchObject. Modified Paths: -------------- trunk/crossfire/src/cfeditor/AutojoinList.java trunk/crossfire/src/cfeditor/CMapTileList.java trunk/crossfire/src/cfeditor/CMapViewBasic.java trunk/crossfire/src/cfeditor/CopyBuffer.java trunk/crossfire/src/cfeditor/ReplaceDialog.java trunk/crossfire/src/cfeditor/arch/ArchObject.java trunk/crossfire/src/cfeditor/arch/ArchObjectIterator.java trunk/crossfire/src/cfeditor/arch/ArchObjectIteratorDeleteArchMapObject.java trunk/crossfire/src/cfeditor/arch/ArchObjectIteratorDeleteMapArch.java trunk/crossfire/src/cfeditor/map/DefaultMapModel.java Modified: trunk/crossfire/src/cfeditor/AutojoinList.java =================================================================== --- trunk/crossfire/src/cfeditor/AutojoinList.java 2006-07-02 12:48:06 UTC (rev 228) +++ trunk/crossfire/src/cfeditor/AutojoinList.java 2006-07-02 18:57:56 UTC (rev 229) @@ -315,7 +315,7 @@ @Nullable private ArchObject findArchOfJoinlist(final MapModel map, final int x, final int y) { ArchObject tmpArch = map.getTopArchObject(x, y); // we look through the arches at the given location (top to bottom): - for (; tmpArch != null; tmpArch = tmpArch.getPrevArch()) { + for (; tmpArch != null; tmpArch = tmpArch.getPrev()) { if (stack.getArch(tmpArch.getNodeNr()).getJoinList() == this) { return tmpArch; // we found an arch } Modified: trunk/crossfire/src/cfeditor/CMapTileList.java =================================================================== --- trunk/crossfire/src/cfeditor/CMapTileList.java 2006-07-02 12:48:06 UTC (rev 228) +++ trunk/crossfire/src/cfeditor/CMapTileList.java 2006-07-02 18:57:56 UTC (rev 229) @@ -317,13 +317,13 @@ ArchObject node = map.getMapModel().getMouseRightPosObject(); // Jump to the end of the list - for (; node != null && node.getNextArch() != null; node = node.getNextArch()) { + for (; node != null && node.getNext() != null; node = node.getNext()) { ; } // Now go through the list backwards and put all arches // on the panel in this order - for (; node != null; node = node.getPrevArch()) { + for (; node != null; node = node.getPrev()) { // add the node if (node.getMyID() == archid) { postSelect = listCounter; Modified: trunk/crossfire/src/cfeditor/CMapViewBasic.java =================================================================== --- trunk/crossfire/src/cfeditor/CMapViewBasic.java 2006-07-02 12:48:06 UTC (rev 228) +++ trunk/crossfire/src/cfeditor/CMapViewBasic.java 2006-07-02 18:57:56 UTC (rev 229) @@ -1628,13 +1628,13 @@ ArchObject tmpArch = mapControl.getBottomArchObject(mapLoc); if (tmpArch != null) { // go to the topmost arch (end of the list) - for (; tmpArch.getNextArch() != null; tmpArch = tmpArch.getNextArch()) { + for (; tmpArch.getNext() != null; tmpArch = tmpArch.getNext()) { ; } // now search backwards for matching view settings for (; tmpArch != null && mainControl.getTileEdit() != 0 && - !mainControl.isTileEdit(tmpArch.getEditType()); tmpArch = tmpArch.getPrevArch()) { + !mainControl.isTileEdit(tmpArch.getEditType()); tmpArch = tmpArch.getPrev()) { ; } if (tmpArch != null) { @@ -1789,13 +1789,13 @@ ArchObject tmpArch = mapControl.getBottomArchObject(temp); if (tmpArch != null) { // go to the topmost arch (end of the list) - for (; tmpArch.getNextArch() != null; tmpArch = tmpArch.getNextArch()) { + for (; tmpArch.getNext() != null; tmpArch = tmpArch.getNext()) { ; } // now search backwards for matching view settings for (; tmpArch != null && mainControl.getTileEdit() != 0 && - !mainControl.isTileEdit(tmpArch.getEditType()); tmpArch = tmpArch.getPrevArch()) { + !mainControl.isTileEdit(tmpArch.getEditType()); tmpArch = tmpArch.getPrev()) { ; } if (tmpArch != null) { Modified: trunk/crossfire/src/cfeditor/CopyBuffer.java =================================================================== --- trunk/crossfire/src/cfeditor/CopyBuffer.java 2006-07-02 12:48:06 UTC (rev 228) +++ trunk/crossfire/src/cfeditor/CopyBuffer.java 2006-07-02 18:57:56 UTC (rev 229) @@ -262,17 +262,17 @@ if (oldHead != null && oldHead.getNodeNr() == arch.getNodeNr()) { // replace old head with our clone: - if (oldHead.getPrevArch() != null) { - oldHead.getPrevArch().setNextArch(clone); - clone.setPrevArch(oldHead.getPrevArch()); + if (oldHead.getPrev() != null) { + oldHead.getPrev().setNext(clone); + clone.setPrev(oldHead.getPrev()); } else { // no previous arch, we must stick it on the grid mapControl.setArchObject(pos, clone); } - if (oldHead.getNextArch() != null) { - oldHead.getNextArch().setPrevArch(clone); - clone.setNextArch(oldHead.getNextArch()); + if (oldHead.getNext() != null) { + oldHead.getNext().setPrev(clone); + clone.setNext(oldHead.getNext()); } clone.setMyID(oldHead.getMyID()); // pass ID to new head @@ -284,8 +284,8 @@ } // delete old head: - oldHead.setNextArch(null); - oldHead.setPrevArch(null); + oldHead.setNext(null); + oldHead.setPrev(null); oldHead.setMapMultiNext(null); oldHead = null; } else { Modified: trunk/crossfire/src/cfeditor/ReplaceDialog.java =================================================================== --- trunk/crossfire/src/cfeditor/ReplaceDialog.java 2006-07-02 12:48:06 UTC (rev 228) +++ trunk/crossfire/src/cfeditor/ReplaceDialog.java 2006-07-02 18:57:56 UTC (rev 229) @@ -334,7 +334,7 @@ (matchCriteria == MATCH_OBJ_NAME && node.getBestName(node.getDefaultArch()).equalsIgnoreCase(matchString)))) { // first, delete the old arch - final ArchObject prevArch = node.getPrevArch(); + final ArchObject prevArch = node.getPrev(); it.remove(); if (replaceArch != null && !deleteOnly) { Modified: trunk/crossfire/src/cfeditor/arch/ArchObject.java =================================================================== --- trunk/crossfire/src/cfeditor/arch/ArchObject.java 2006-07-02 12:48:06 UTC (rev 228) +++ trunk/crossfire/src/cfeditor/arch/ArchObject.java 2006-07-02 18:57:56 UTC (rev 229) @@ -883,11 +883,19 @@ return mapy; } - public void setNextArch(final ArchObject arch) { + /** + * Set Next link. + * @param arch Arch to point next to + */ + public void setNext(final ArchObject arch) { next = arch; } - public void setPrevArch(final ArchObject arch) { + /** + * Set Prev link. + * @param arch Arch to point prev to + */ + public void setPrev(final ArchObject arch) { prev = arch; } @@ -899,11 +907,19 @@ tempptr = temp; } - public ArchObject getNextArch() { + /** + * Get Next link. + * @return next link + */ + public ArchObject getNext() { return next; } - public ArchObject getPrevArch() { + /** + * Get Prev link. + * @return prev link + */ + public ArchObject getPrev() { return prev; } Modified: trunk/crossfire/src/cfeditor/arch/ArchObjectIterator.java =================================================================== --- trunk/crossfire/src/cfeditor/arch/ArchObjectIterator.java 2006-07-02 12:48:06 UTC (rev 228) +++ trunk/crossfire/src/cfeditor/arch/ArchObjectIterator.java 2006-07-02 18:57:56 UTC (rev 229) @@ -1,4 +1,26 @@ -/* $Id: ArchObjectIterator.java,v 1.1 2006/04/06 17:16:03 akirschbaum Exp $ */ +/* + * Daimonin Java Editor. + * Copyright (C) 2000 Michael Toennies + * Copyright (C) 2001 Andreas Vogl + * + * (code based on: Gridder. 2D grid based level editor. (C) 2000 Pasi Keränen) + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program 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 + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + * 02111-1307, USA. + * + */ package cfeditor.arch; @@ -6,39 +28,72 @@ import java.util.NoSuchElementException; /** - * The class ArchObjectIterator implements an {@link Iterator} that - * returns all {@link ArchObject}s in a map location. - * @author Andreas Kirschbaum + * Iterator for ArchObjects. + * This class is only meant for usage during the transition from manually linked lists to encapslued Collections lists in arch handling. + * This Iterator is believed to be safe against concurrent modification. + * Therefore it is not fail-fast, it does not throw ConcurrentModificationException. + * The next link is future-fetched, which means that the invocation of {@link #next()} already prepares the next invocation of {@link #hasNext()} or {@link #next()}. + * This allows to safely perform manipulations on the current object, especially deletion, without loosing traversal. + * @author <a href="mailto:ch...@it...">Christian Hujer</a> */ public class ArchObjectIterator implements Iterator<ArchObject> { - protected ArchObject nextArchObject; +// /** Current ArchObject, used for {@link #remove()}. */ +// private ArchObject current; - protected ArchObject currArchObject = null; + /** Traversal direction. */ + private final boolean forward; - public ArchObjectIterator(final ArchObject archObject) { - nextArchObject = archObject; + /** ArchObject for first iteration step. */ + private ArchObject next; + + /** + * Creates a new instance of ArchObjectIterator (forward iteration). + * @param first first ArchObject to start forward iteration on + */ + public ArchObjectIterator(final ArchObject first) { + this(first, true); } + /** + * Creates a new instance of ArchObjectIterator. + * @param first first ArchObject to start iteration on + * @param forward <code>true</code> for forward iteration, <code>false</code> for backward iteration + */ + ArchObjectIterator(final ArchObject first, final boolean forward) { + next = first; + this.forward = forward; + } + /** {@inheritDoc} */ public boolean hasNext() { - return nextArchObject != null; + return next != null; } /** {@inheritDoc} */ public ArchObject next() { - if (nextArchObject == null) { - throw new NoSuchElementException(); + try { + return next; + } finally { + try { + next = forward ? next.getNext() : next.getPrev(); + } catch (final NullPointerException e) { + //noinspection ThrowFromFinallyBlock + throw new NoSuchElementException(); + } } - - currArchObject = nextArchObject; - nextArchObject = currArchObject.getNextArch(); - return currArchObject; } - /** {@inheritDoc} */ + /** + * {@inheritDoc} + * @todo eventually, multitile-removal should go here. + */ public void remove() { throw new UnsupportedOperationException(); +// ArchObject prev = current.getPrevArch(); +// ArchObject next = current.getNextArch(); +// if (next != null) { next.setPrevArch(prev); } +// if (prev != null) { prev.setPrevArch(next); } } } // class ArchObjectIterator Modified: trunk/crossfire/src/cfeditor/arch/ArchObjectIteratorDeleteArchMapObject.java =================================================================== --- trunk/crossfire/src/cfeditor/arch/ArchObjectIteratorDeleteArchMapObject.java 2006-07-02 12:48:06 UTC (rev 228) +++ trunk/crossfire/src/cfeditor/arch/ArchObjectIteratorDeleteArchMapObject.java 2006-07-02 18:57:56 UTC (rev 229) @@ -26,6 +26,12 @@ } /** {@inheritDoc} */ + public ArchObject next() { + currArchObject = super.next(); + return currArchObject; + } + + /** {@inheritDoc} */ @Override public void remove() { if (currArchObject == null) { throw new IllegalStateException(); @@ -35,4 +41,6 @@ currArchObject = null; } + private ArchObject currArchObject = null; + } // class ArchObjectIteratorDeleteArchMapObject Modified: trunk/crossfire/src/cfeditor/arch/ArchObjectIteratorDeleteMapArch.java =================================================================== --- trunk/crossfire/src/cfeditor/arch/ArchObjectIteratorDeleteMapArch.java 2006-07-02 12:48:06 UTC (rev 228) +++ trunk/crossfire/src/cfeditor/arch/ArchObjectIteratorDeleteMapArch.java 2006-07-02 18:57:56 UTC (rev 229) @@ -49,6 +49,12 @@ } /** {@inheritDoc} */ + public ArchObject next() { + currArchObject = super.next(); + return currArchObject; + } + + /** {@inheritDoc} */ @Override public void remove() { if (currArchObject == null) { throw new IllegalStateException(); @@ -58,4 +64,6 @@ currArchObject = null; } + private ArchObject currArchObject = null; + } // class ArchObjectIteratorDeleteMapArch Modified: trunk/crossfire/src/cfeditor/map/DefaultMapModel.java =================================================================== --- trunk/crossfire/src/cfeditor/map/DefaultMapModel.java 2006-07-02 12:48:06 UTC (rev 228) +++ trunk/crossfire/src/cfeditor/map/DefaultMapModel.java 2006-07-02 18:57:56 UTC (rev 229) @@ -227,8 +227,8 @@ */ public ArchObject getTopArchObject(final Point pos) { ArchObject result = getBottomArchObject(pos); - while (result != null && result.getNextArch() != null) { - result = result.getNextArch(); + while (result != null && result.getNext() != null) { + result = result.getNext(); } return result; } @@ -362,20 +362,20 @@ return; } - final ArchObject arch = prev.getNextArch(); + final ArchObject arch = prev.getNext(); if (arch == null) { return; } - prev.setNextArch(arch.getNextArch()); - arch.setPrevArch(prev.getPrevArch()); - arch.setNextArch(prev); - prev.setPrevArch(arch); - if (prev.getNextArch() != null) { - prev.getNextArch().setPrevArch(prev); + prev.setNext(arch.getNext()); + arch.setPrev(prev.getPrev()); + arch.setNext(prev); + prev.setPrev(arch); + if (prev.getNext() != null) { + prev.getNext().setPrev(prev); } - if (arch.getPrevArch() != null) { - arch.getPrevArch().setNextArch(arch); + if (arch.getPrev() != null) { + arch.getPrev().setNext(arch); } else { mapGrid[arch.getMapX()][arch.getMapY()] = arch; } @@ -402,20 +402,20 @@ return; } - final ArchObject prev = arch.getPrevArch(); + final ArchObject prev = arch.getPrev(); if (prev == null) { return; } - prev.setNextArch(arch.getNextArch()); - arch.setPrevArch(prev.getPrevArch()); - arch.setNextArch(prev); - prev.setPrevArch(arch); - if (prev.getNextArch() != null) { - prev.getNextArch().setPrevArch(prev); + prev.setNext(arch.getNext()); + arch.setPrev(prev.getPrev()); + arch.setNext(prev); + prev.setPrev(arch); + if (prev.getNext() != null) { + prev.getNext().setPrev(prev); } - if (arch.getPrevArch() != null) { - arch.getPrevArch().setNextArch(arch); + if (arch.getPrev() != null) { + arch.getPrev().setNext(arch); } else { mapGrid[arch.getMapX()][arch.getMapY()] = arch; } @@ -492,18 +492,18 @@ } else { if (!insertBelow) { // if we want to insert on top, we need to get last node element - for (int i = 0; node.getNextArch() != null; i++) { - node = node.getNextArch(); + for (int i = 0; node.getNext() != null; i++) { + node = node.getNext(); } - node.setNextArch(newarch); - node.getNextArch().setPrevArch(node); - node.getNextArch().setMapX(mapx); - node.getNextArch().setMapY(mapy); + node.setNext(newarch); + node.getNext().setPrev(node); + node.getNext().setMapX(mapx); + node.getNext().setMapY(mapy); } else { //ArchObject tmp = node; - node.setPrevArch(newarch); - newarch.setNextArch(node); + node.setPrev(newarch); + newarch.setNext(node); mapGrid[mapx][mapy] = newarch; newarch.setMapX(mapx); newarch.setMapY(mapy); @@ -572,14 +572,14 @@ // jump to the end of the list (-> grab "topmost" arch) ArchObject node; for (node = mapGrid[pos.x][pos.y]; - node != null && node.getNextArch() != null; - node = node.getNextArch()) { + node != null && node.getNext() != null; + node = node.getNext()) { ; } // now move the arch down till it meets the insert position - for (; node != null && node.getPrevArch() != null && - (next == null || node.getPrevArch().getMyID() != next.getMyID()); + for (; node != null && node.getPrev() != null && + (next == null || node.getPrev().getMyID() != next.getMyID()); moveTileDown(node, false)) { ; } @@ -650,18 +650,18 @@ } else { if (!insertBelow) { // if we want to insert on top, we need to get last node element - for (int i = 0; node.getNextArch() != null; i++) { - node = node.getNextArch(); + for (int i = 0; node.getNext() != null; i++) { + node = node.getNext(); } - node.setNextArch(arch); - node.getNextArch().setPrevArch(node); - node.getNextArch().setMapX(mapx); - node.getNextArch().setMapY(mapy); + node.setNext(arch); + node.getNext().setPrev(node); + node.getNext().setMapX(mapx); + node.getNext().setMapY(mapy); } else { // insert below, so just put it on first place - node.setPrevArch(arch); - arch.setNextArch(node); + node.setPrev(arch); + arch.setNext(node); mapGrid[mapx][mapy] = arch; arch.setMapX(mapx); arch.setMapY(mapy); @@ -824,8 +824,8 @@ final int mapx = node.getMapX(); final int mapy = node.getMapY(); // second, we remove our arch from the map position chain - final ArchObject prev = node.getPrevArch(); - ArchObject next = node.getNextArch(); + final ArchObject prev = node.getPrev(); + ArchObject next = node.getNext(); if (next == node) { // should not happen, but better we double-check to prevent infinite loop @@ -837,13 +837,13 @@ // if some up (or null), put this on start mapGrid[mapx][mapy] = next; if (next != null) { // if there one up, mark them as starter - next.setPrevArch(null); + next.setPrev(null); } } else { // we chain next to prev - prev.setNextArch(next); + prev.setNext(next); if (next != null) { // we are last... no next - next.setPrevArch(prev); + next.setPrev(prev); } } if (next != null) { @@ -852,8 +852,8 @@ // remove all inventory... node.clearInventory(); - node.setNextArch(null); - node.setPrevArch(null); + node.setNext(null); + node.setPrev(null); final ArchObject temp = node.getMapMultiNext(); // we save our next part (or null=finished) node.setMapMultiHead(null); node.setMapMultiNext(null); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |