1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.collections4.list;
18
19 import java.util.Arrays;
20 import java.util.LinkedList;
21
22 import org.junit.jupiter.api.Test;
23
24
25
26
27 public class NodeCachingLinkedListTest<E> extends AbstractLinkedListTest<E> {
28
29 public static void compareSpeed() {
30 final NodeCachingLinkedList<Object> ncll = new NodeCachingLinkedList<>();
31 final LinkedList<Object> ll = new LinkedList<>();
32
33 final Object o1 = new Object();
34 final Object o2 = new Object();
35
36 final int loopCount = 4000000;
37
38 System.out.println("Testing relative execution time of commonly-used methods...");
39
40 long startTime = System.currentTimeMillis();
41 for (int x = loopCount; x > 0; x--) {
42
43 ll.addFirst(o1);
44 ll.addLast(o2);
45 ll.removeFirst();
46 ll.removeLast();
47 ll.add(o1);
48 ll.remove(0);
49
50 ll.addFirst(o1);
51 ll.addLast(o2);
52 ll.removeFirst();
53 ll.removeLast();
54 ll.add(o1);
55 ll.remove(0);
56
57 ll.addFirst(o1);
58 ll.addLast(o2);
59 ll.removeFirst();
60 ll.removeLast();
61 ll.add(o1);
62 ll.remove(0);
63 }
64 long endTime = System.currentTimeMillis();
65 System.out.println("Time with LinkedList: " + (endTime - startTime) + " ms");
66
67 startTime = System.currentTimeMillis();
68 for (int x = loopCount; x > 0; x--) {
69 ncll.addFirst(o1);
70 ncll.addLast(o2);
71 ncll.removeFirst();
72 ncll.removeLast();
73 ncll.add(o1);
74 ncll.remove(0);
75
76 ncll.addFirst(o1);
77 ncll.addLast(o2);
78 ncll.removeFirst();
79 ncll.removeLast();
80 ncll.add(o1);
81 ncll.remove(0);
82
83 ncll.addFirst(o1);
84 ncll.addLast(o2);
85 ncll.removeFirst();
86 ncll.removeLast();
87 ncll.add(o1);
88 ncll.remove(0);
89 }
90 endTime = System.currentTimeMillis();
91 System.out.println("Time with NodeCachingLinkedList: " + (endTime - startTime) + " ms");
92
93 }
94
95
96
97
98 @Override
99 public NodeCachingLinkedList<E> getCollection() {
100 return (NodeCachingLinkedList<E>) super.getCollection();
101 }
102
103 @Override
104 public String getCompatibilityVersion() {
105 return "4";
106 }
107
108 @Override
109 public NodeCachingLinkedList<E> makeObject() {
110 return new NodeCachingLinkedList<>();
111 }
112
113
114
115
116
117
118
119
120
121
122 @Test
123 @SuppressWarnings("unchecked")
124 public void testShrinkCache() {
125 if (!isRemoveSupported() || !isAddSupported()) {
126 return;
127 }
128 resetEmpty();
129 final NodeCachingLinkedList<E> list = getCollection();
130
131 list.addAll(Arrays.asList((E[]) new String[] { "1", "2", "3", "4" }));
132 list.removeAllNodes();
133 list.setMaximumCacheSize(2);
134 list.addAll(Arrays.asList((E[]) new String[] { "1", "2", "3", "4" }));
135 checkNodes();
136 list.removeNode(list.getNode(0, false));
137 list.removeNode(list.getNode(0, false));
138 list.removeNode(list.getNode(0, false));
139 checkNodes();
140 list.addAll(Arrays.asList((E[]) new String[] { "1", "2", "3", "4" }));
141 checkNodes();
142 }
143 }