KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > util > Slist


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.util;
30
31 /**
32  * Cons-cell slist of objects
33  */

34 public class Slist {
35   public Slist next;
36   public Object JavaDoc first;
37
38   public Slist rest() { return next; }
39   public Object JavaDoc value() { return first; }
40   /**
41    * Copies a slist
42    */

43   public static Slist copy(Slist slist)
44   {
45     if (slist == null)
46       return null;
47
48     Slist head = new Slist(null, slist.first);
49     Slist ptr = head;
50     for (slist = slist.next; slist != null; slist = slist.next) {
51       ptr.next = new Slist(null, slist.first);
52       ptr = ptr.next;
53     }
54
55     return head;
56   }
57
58   /**
59    * Destructively reverse
60    */

61   public static Slist reverse(Slist slist)
62   {
63     if (slist == null)
64       return null;
65
66     Slist prev = null;
67     Slist next = slist.next;
68     while (next != null) {
69       slist.next = prev;
70
71       prev = slist;
72       slist = next;
73       next = next.next;
74     }
75
76     slist.next = prev;
77
78     return slist;
79   }
80
81   /**
82    * Destructively append
83    */

84   public static Slist append(Slist a, Slist b)
85   {
86     if (a == null)
87       return b;
88
89     Slist ptr = a;
90     for (; ptr.next != null; ptr = ptr.next) {
91     }
92
93     ptr.next = b;
94
95     return a;
96   }
97
98   /**
99    * Remove if ==
100    */

101   public static Slist remove(Slist slist, Object JavaDoc object)
102   {
103     if (slist == null)
104       return null;
105     else if (slist.first == object)
106       return slist.next;
107       
108     for (Slist ptr = slist; ptr.next != null; ptr = ptr.next) {
109       if (ptr.next.first == object) {
110     ptr.next = ptr.next.next;
111     break;
112       }
113     }
114
115     return slist;
116   }
117
118   /**
119    * Create a new cons-cell
120    */

121   public Slist(Slist next, Object JavaDoc first)
122   {
123     this.next = next;
124     this.first = first;
125   }
126 }
127
Popular Tags