RelocateFn.java

mudlibs/tinylib/src/net/sourceforge/pain/tinylib/logic/fn/RelocateFn.java · 2.0 KB

From the archive pain_0_45.tgz

Download the archive   « Back to the archive contents

package net.sourceforge.pain.tinylib.logic.fn;

import net.sourceforge.pain.tinylib.data.type.*;


/**
 * PAiN  Date: 14.04.2003  Time: 0:50:45
 */
public final class RelocateFn {

    public static void relocate(Located obj, Space dst) {
        removeFromSpace(obj);		// removing obj from src space
        addToSpace(dst, obj);		// adding obj to dst space
    }

    public static void removeFromSpace(Located obj) {
        final Space space = obj.getLocation();
        final Located prev = obj.getPrevInSpace();
        final Located next = obj.getNextInSpace();
        if (prev != obj) { // backward iteration has no nulls! see Located.java
            final Located firstInSpace = space.getFirstInSpace();
            if (next != null) {
                next.setPrevInSpace(prev);
            } else { // obj is last, and obj was prev for first
                firstInSpace.setPrevInSpace(prev);
            }
            if (obj != firstInSpace) { //last in list should have next = null
                prev.setNextInSpace(next);
            } else {
                space.setFirstInSpace(next);
            }

        } else { // there only one obj in space
            space.setFirstInSpace(null);
        }
        obj.setLocation(null);
        obj.setNextInSpace(null);
        obj.setPrevInSpace(null);
    }

    /**
     * used for new created objects
     */
    public static void addToSpace(Space space, Located obj) {
        Located first = space.getFirstInSpace();
        if (first != null) {
            final Located prev = first.getPrevInSpace(); // last in list
            prev.setNextInSpace(obj);
            obj.setPrevInSpace(prev);
        } else {
            space.setFirstInSpace(obj);
            first = obj;
        }
        first.setPrevInSpace(obj);  //backward list should be without nulls!
        obj.setNextInSpace(null); // forward list finished on null!
        obj.setLocation(space);
    }


}

Site Stats

Snippets: 1791
Codebases: 571