Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: src/com/github/rjeschke/neetutils/dispose/ReferenceList.java

Issue 6606493159784448: New JNI bindings (Closed)
Patch Set: Reuploaded full diff Created March 28, 2014, 3:56 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright (C) 2012 René Jeschke <rene_jeschke@yahoo.de>
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 package com.github.rjeschke.neetutils.dispose;
17
18 /**
19 * Specialized linked list to hold weak references or similar stuff. Adding and
20 * removing is guaranteed O(1).
21 *
22 * @author René Jeschke <rene_jeschke@yhoo.de>
23 */
24 public class ReferenceList<T>
25 {
26 private Node<T> root;
27 private int size;
28
29 /**
30 * Constructor.
31 */
32 public ReferenceList()
33 {
34 // empty
35 }
36
37 /**
38 * Gets the number of items currently in this list.
39 *
40 * @return The size.
41 */
42 public int size()
43 {
44 return this.size;
45 }
46
47 /**
48 * Checks if this list is empty.
49 *
50 * @return <code>true</code> is this list is empty
51 */
52 public boolean isEmpty()
53 {
54 return this.size == 0;
55 }
56
57 /**
58 * Adds a value to this list (at head).
59 *
60 * @param value
61 * Value to add
62 * @return Node reference needed for remove
63 */
64 public Node<T> add(final T value)
65 {
66 final Node<T> node = new Node<T>(value);
67 this.size++;
68 if (this.root != null)
69 {
70 node.next = this.root;
71 this.root.previous = node;
72 }
73 this.root = node;
74
75 return node;
76 }
77
78 /**
79 * Removes the last added Node.
80 *
81 * @return The last added Node or <code>null</code> if this list is empty.
82 */
83 public Node<T> removeLast()
84 {
85 if (this.root == null)
86 {
87 return null;
88 }
89
90 this.size--;
91 final Node<T> node = this.root;
92 if (node.next != null)
93 {
94 node.next.previous = null;
95 }
96 this.root = node.next;
97
98 node.next = node.previous = null;
99 node.inside = false;
100 return node;
101 }
102
103 /**
104 * Removes the given Node from this list. Multiple removes are prevented.
105 *
106 * @param node
107 * The Node to remove
108 */
109 public void remove(final Node<T> node)
110 {
111 if (!node.inside)
112 {
113 return;
114 }
115
116 this.size--;
117 if (node.previous == null)
118 {
119 this.root = node.next;
120 }
121 else
122 {
123 node.previous.next = node.next;
124 if (node.next != null)
125 {
126 node.next.previous = node.previous;
127 }
128 }
129
130 node.next = node.previous = null;
131 node.inside = false;
132 }
133
134 /**
135 *
136 *
137 * @author René Jeschke <rene_jeschke@yhoo.de>
138 */
139 public static class Node<T>
140 {
141 final T value;
142 boolean inside = true;
143 Node<T> previous;
144 Node<T> next;
145
146 public Node(final T value)
147 {
148 this.value = value;
149 }
150
151 public T value()
152 {
153 return this.value;
154 }
155
156 @Override
157 public String toString()
158 {
159 return "Node: " + this.value;
160 }
161 }
162 }
OLDNEW

Powered by Google App Engine
This is Rietveld