001 /* 002 * www.ti.bfh.ch 003 * 004 * Copyright 2007, Berne University of Applied Sciences, 005 * School of Engineering and Information Technology 006 * and individual contributors as indicated by the @authors tag. 007 * 008 * This is free software; you can redistribute it and/or modify it under the terms of the 009 * GNU Lesser General Public License as published by the Free Software Foundation; 010 * either version 3 of the License, or (at your option) any later version. 011 * 012 * This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 013 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 014 * See the GNU Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public License along with this software; 017 * if not, see <http://www.gnu.org/licenses/>. 018 * 019 */ 020 package ch.bfh.algo.core.sequence; 021 022 023 024 public class LinkedCursor<E,P extends GenericLinkedPosition<E,P>> implements Observer{ 025 026 private P previous; 027 private P next; 028 029 private ObserverPosition before; 030 private ObserverPosition after; 031 032 public LinkedCursor(P previous, P next){ 033 this.previous=previous; 034 this.next=next; 035 this.attach(); 036 } 037 038 public P getPrevious(){ 039 return this.previous; 040 } 041 042 public P getNext(){ 043 return this.next; 044 } 045 046 public void attach(){ 047 this.before=this.previous.attachAfter(this); 048 this.after=this.next.attachBefore(this); 049 } 050 051 public void detach(){ 052 this.previous.detachAfter(this.before); 053 this.next.detachBefore(this.after); 054 } 055 056 public void moveNext(){ 057 this.detach(); 058 this.previous=this.next; 059 this.next=this.next.getNext(); 060 this.attach(); 061 } 062 063 public void movePrevious(){ 064 this.detach(); 065 this.next=this.previous; 066 this.previous=this.previous.getPrevious(); 067 this.attach(); 068 } 069 070 public void updateBefore(){ 071 this.previous.detachAfter(this.before); 072 this.previous=this.next.getPrevious(); 073 this.before=this.previous.attachAfter(this); 074 } 075 076 public void updateAfter(){ 077 this.next.detachBefore(this.after); 078 this.next=this.previous.getNext(); 079 this.after=this.next.attachBefore(this); 080 } 081 }