KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > util > Queue


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25 package org.snipsnap.util;
26
27 import org.radeox.util.logging.Logger;
28 import org.snipsnap.snip.Snip;
29
30 import java.util.Iterator JavaDoc;
31 import java.util.LinkedList JavaDoc;
32 import java.util.List JavaDoc;
33
34 /**
35  * Queue implementation for Snips.
36  * @author Stephan J. Schmidt
37  * @version $Id: Queue.java 1256 2003-12-11 13:24:57Z leo $
38  */

39 public class Queue {
40   private LinkedList JavaDoc queue;
41   private int size;
42
43   public Queue() {
44     this(100);
45   }
46
47   public Queue(int size) {
48     this.size = size;
49     queue = new LinkedList JavaDoc();
50   }
51
52   public void fill(List JavaDoc list) {
53     queue.clear();
54     queue.addAll(list);
55   }
56
57   public Snip add(Snip snip) {
58     Logger.debug("Adding snip="+snip);
59     Logger.debug("Class="+snip.getClass());
60     Iterator JavaDoc iterator = queue.iterator();
61     while (iterator.hasNext()) {
62       Snip snip1 = (Snip) iterator.next();
63     }
64     // Queue already contains object, so remove it
65
if (queue.contains(snip)) {
66       //Logger.debug("Removing.");
67
queue.remove(snip);
68     }
69
70     // Queue is full, drop last item
71
if (queue.size() == size) {
72       queue.removeLast();
73     }
74     queue.addFirst(snip);
75     return snip;
76   }
77
78   public void remove(Snip snip) {
79     queue.remove(snip);
80   }
81
82   public List JavaDoc get() {
83     return (List JavaDoc) queue;
84   }
85
86   public List JavaDoc get(int count) {
87     count = Math.min(count, queue.size());
88     return (List JavaDoc) queue.subList(0, count);
89   }
90
91 }
92
Popular Tags