Back to index

wims  3.65+svn20090927
ListClass.java
Go to the documentation of this file.
00001 package rene.util.list;
00002 
00009 public class ListClass
00010 {      ListElement First,Last; // Pointer to start and end of list.
00011 
00015        public ListClass ()
00016        {      First=null; Last=null;
00017        }
00018        
00022        public void append (ListElement l)
00023        {      if (Last==null) init(l);
00024               else
00025               {      Last.next(l); l.previous(Last); Last=l;
00026                      l.next(null); l.list(this);
00027               }
00028        }
00029        
00030        public void prepend (ListElement l)
00031        // prepend a node to the list
00032        {      if (First==null) init(l);
00033               else
00034               {      First.previous(l); l.next(First); First=l;
00035                      l.previous(null); l.list(this);
00036               }
00037        }
00038 
00039        /*
00040        @param l ListElement to be inserted.
00041        @param after If null, it works like prepend.
00042        */
00043        public void insert (ListElement l, ListElement after)
00044        {      if (after==Last) append(l);
00045               else if (after==null) prepend(l);
00046               else
00047               {      after.next().previous(l);
00048                      l.next(after.next());
00049                      after.next(l); l.previous(after); l.list(this);
00050               }
00051        }
00052        
00056        public void init (ListElement l)
00057        {      Last=First=l;
00058               l.previous(null); l.next(null);
00059               l.list(this);
00060        }
00061        
00066        public void remove (ListElement l)
00067        {      if (First==l)
00068               {      First=l.next();
00069                      if (First!=null) First.previous(null);
00070                      else Last=null;
00071               }
00072               else if (Last==l)
00073               {      Last=l.previous();
00074                      if (Last!=null) Last.next(null);
00075                      else First=null;
00076               }
00077               else
00078               {      l.previous().next(l.next());
00079                      l.next().previous(l.previous());
00080               }
00081               l.next(null); l.previous(null); l.list(null);
00082        }
00083 
00087        public void removeall ()
00088        {      First=null;
00089               Last=null;
00090        }
00091 
00093        public void removeAfter (ListElement e)
00094        {      e.next(null);
00095               Last=e;
00096        }
00097 
00101        public ListElement first () { return First; }
00102 
00106        public ListElement last () { return Last; }
00107        
00111        public String toString ()
00112        {      ListElement e=First;
00113               String s="";
00114               while (e!=null)
00115               {      s=s+e.content().toString()+", ";
00116                      e=e.next();
00117               }
00118               return s;
00119        }
00120 }
00121