• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.cooliris.media;
18 
19 public final class DirectLinkedList<E> {
20     private Entry<E> mHead;
21     private Entry<E> mTail;
22     private int mSize = 0;
23 
24     public static final class Entry<E> {
Entry(E value)25         Entry(E value) {
26             this.value = value;
27         }
28 
29         public final E value;
30         public Entry<E> previous = null;
31         public Entry<E> next = null;
32         public boolean inserted = false;
33     }
34 
DirectLinkedList()35     public DirectLinkedList() {
36     }
37 
isEmpty()38     public boolean isEmpty() {
39         return mSize == 0;
40     }
41 
size()42     public int size() {
43         return mSize;
44     }
45 
add(Entry<E> entry)46     public void add(Entry<E> entry) {
47         // Requires that entry not be inserted in a list.
48         final Entry<E> tail = mTail;
49         if (tail != null) {
50             tail.next = entry;
51             entry.previous = tail;
52         } else {
53             mHead = entry;
54         }
55         mTail = entry;
56         entry.inserted = true;
57         ++mSize;
58     }
59 
remove(Entry<E> entry)60     public Entry<E> remove(Entry<E> entry) {
61         // Requires that entry be inserted into this list.
62         final Entry<E> previous = entry.previous;
63         final Entry<E> next = entry.next;
64         if (next != null) {
65             next.previous = previous;
66             entry.next = null;
67         } else {
68             mTail = previous;
69         }
70         if (previous != null) {
71             previous.next = next;
72             entry.previous = null;
73         } else {
74             mHead = next;
75         }
76         entry.inserted = false;
77         --mSize;
78         if (mSize < 0)
79             mSize = 0;
80         return next;
81     }
82 
getHead()83     public Entry<E> getHead() {
84         return mHead;
85     }
86 
getTail()87     public Entry<E> getTail() {
88         return mTail;
89     }
90 
clear()91     public void clear() {
92         mHead = null;
93         mTail = null;
94         mSize = 0;
95     }
96 }
97