KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > retrotranslator > runtime > java > util > _LinkedList


1 /***
2  * Retrotranslator: a Java bytecode transformer that translates Java classes
3  * compiled with JDK 5.0 into classes that can be run on JVM 1.4.
4  *
5  * Copyright (c) 2005 - 2007 Taras Puchko
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the copyright holders nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  */

32 package net.sf.retrotranslator.runtime.java.util;
33
34 import java.util.*;
35
36 /**
37  * @author Taras Puchko
38  */

39 public class _LinkedList {
40
41     public static Iterator descendingIterator(final LinkedList list) {
42         return new Iterator() {
43             private final ListIterator iterator = list.listIterator(list.size());
44
45             public boolean hasNext() {
46                 return iterator.hasPrevious();
47             }
48
49             public Object JavaDoc next() {
50                 return iterator.previous();
51             }
52
53             public void remove() {
54                 iterator.remove();
55             }
56         };
57     }
58
59     public static Object JavaDoc element(LinkedList list) {
60         return list.getFirst();
61     }
62
63     public static boolean offer(LinkedList list, Object JavaDoc o) {
64         return list.add(o);
65     }
66
67     public static boolean offerFirst(LinkedList list, Object JavaDoc o) {
68         list.addFirst(o);
69         return true;
70     }
71
72     public static boolean offerLast(LinkedList list, Object JavaDoc o) {
73         list.addLast(o);
74         return true;
75     }
76
77     public static Object JavaDoc peek(LinkedList list) {
78         return list.isEmpty() ? null : list.getFirst();
79     }
80
81     public static Object JavaDoc peekFirst(LinkedList list) {
82         return list.isEmpty() ? null : list.getFirst();
83     }
84
85     public static Object JavaDoc peekLast(LinkedList list) {
86         return list.isEmpty() ? null : list.getLast();
87     }
88
89     public static Object JavaDoc poll(LinkedList list) {
90         return list.isEmpty() ? null : list.removeFirst();
91     }
92
93     public static Object JavaDoc pollFirst(LinkedList list) {
94         return list.isEmpty() ? null : list.removeFirst();
95     }
96
97     public static Object JavaDoc pollLast(LinkedList list) {
98         return list.isEmpty() ? null : list.removeLast();
99     }
100
101     public static Object JavaDoc pop(LinkedList list) {
102         return list.removeFirst();
103     }
104
105     public static void push(LinkedList list, Object JavaDoc o) {
106         list.addFirst(o);
107     }
108
109     public static Object JavaDoc remove(LinkedList list) {
110         return list.removeFirst();
111     }
112
113     public static boolean removeFirstOccurrence(LinkedList list, Object JavaDoc o) {
114         return list.remove(o);
115     }
116
117     public static boolean removeLastOccurrence(LinkedList list, Object JavaDoc o) {
118         ListIterator iterator = list.listIterator(list.size());
119         if (o != null) {
120             while (iterator.hasPrevious()) {
121                 if (o.equals(iterator.previous())) {
122                     iterator.remove();
123                     return true;
124                 }
125             }
126         } else {
127             while (iterator.hasPrevious()) {
128                 if (iterator.previous() == null) {
129                     iterator.remove();
130                     return true;
131                 }
132             }
133         }
134         return false;
135     }
136
137 }
138
Popular Tags