|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ObjectQueue.java | 0% | 0% | 0% | 0% |
|
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
3 | * contributor license agreements. See the NOTICE file distributed with | |
4 | * this work for additional information regarding copyright ownership. | |
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
6 | * (the "License"); you may not use this file except in compliance with | |
7 | * the License. You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
16 | * | |
17 | * $Id: ObjectQueue.java 541508 2007-05-25 01:54:12Z vgritsenko $ | |
18 | */ | |
19 | ||
20 | package org.apache.xindice.util; | |
21 | ||
22 | import java.io.Serializable; | |
23 | ||
24 | /** | |
25 | * ObjectQueue is a simple linked list implemention that can be used | |
26 | * for FIFO queuing. The benefit of ObjectQueue over Sun's Queue | |
27 | * implementation is that it is not an extension of ArrayList and doesn't | |
28 | * suffer from ArrayList's resizing performance limitations. | |
29 | * | |
30 | * @version $Revision: 541508 $, $Date: 2007-05-24 18:54:12 -0700 (Thu, 24 May 2007) $ | |
31 | */ | |
32 | public final class ObjectQueue implements Serializable { | |
33 | static final long serialVersionUID = 2573729619402197579L; | |
34 | ||
35 | private ObjectQueueItem first = null; | |
36 | private ObjectQueueItem last = null; | |
37 | private int size = 0; | |
38 | ||
39 | 0 | public synchronized void add(Object value) { |
40 | 0 | size++; |
41 | 0 | ObjectQueueItem q = new ObjectQueueItem(value); |
42 | 0 | if (last != null) { |
43 | 0 | last.next = q; |
44 | } | |
45 | 0 | last = q; |
46 | 0 | if (first == null) { |
47 | 0 | first = q; |
48 | } | |
49 | } | |
50 | ||
51 | 0 | public synchronized Object remove() { |
52 | 0 | ObjectQueueItem q = first; |
53 | 0 | if (q != null) { |
54 | 0 | size--; |
55 | 0 | first = q.next; |
56 | 0 | if (first == null) { |
57 | 0 | last = null; |
58 | } | |
59 | 0 | return q.value; |
60 | } else { | |
61 | 0 | return null; |
62 | } | |
63 | } | |
64 | ||
65 | 0 | public boolean isEmpty() { |
66 | 0 | return (size == 0); |
67 | } | |
68 | ||
69 | 0 | public int size() { |
70 | 0 | return size; |
71 | } | |
72 | ||
73 | 0 | public synchronized Object peek() { |
74 | 0 | return first != null ? first.value |
75 | : null; | |
76 | } | |
77 | ||
78 | 0 | public synchronized void clear() { |
79 | 0 | size = 0; |
80 | 0 | first = null; |
81 | 0 | last = null; |
82 | } | |
83 | ||
84 | /** | |
85 | * ObjectQueueItem | |
86 | */ | |
87 | ||
88 | private class ObjectQueueItem implements Serializable { | |
89 | public Object value; | |
90 | public ObjectQueueItem next; | |
91 | ||
92 | 0 | public ObjectQueueItem(Object value) { |
93 | 0 | this.value = value; |
94 | } | |
95 | } | |
96 | } | |
97 |
|