Skip to content

Commit 082d6d7

Browse files
authored
Add $.createLRUCache(capacity) method in lodash module.
1 parent 4d2d769 commit 082d6d7

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@
157157
<option>-keep public class com.github.underscore.lodash.*$Chain { *; }</option>
158158
<option>-keep public class com.github.underscore.lodash.*$ParseException { *; }</option>
159159
<option>-keep public class com.github.underscore.lodash.*$FetchResponse { *; }</option>
160+
<option>-keep public class com.github.underscore.lodash.*$LRUCache { *; }</option>
161+
<option>-keep public class com.github.underscore.lodash.*$Node { *; }</option>
160162
<option>-keepclassmembers class * { *** newArrayList(); *** newLinkedHashSet(); *** newHashSet(java.lang.Iterable); *** newLinkedHashMap(); }</option>
161163
<option>-dontnote com.github.underscore.*$ClassForName</option>
162164
</options>

src/main/java/com/github/underscore/lodash/$.java

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2803,4 +2803,85 @@ public String uncapitalize() {
28032803
public List<String> words() {
28042804
return words(getString().get());
28052805
}
2806+
2807+
public static class LRUCache<K, V> {
2808+
private int capacity;
2809+
private Map<K, Node<K, V>> map = new HashMap<K, Node<K, V>>();
2810+
private Node head;
2811+
private Node end;
2812+
2813+
public LRUCache(int capacity) {
2814+
this.capacity = capacity;
2815+
}
2816+
2817+
public V get(K key) {
2818+
if (map.containsKey(key)) {
2819+
Node<K, V> node = map.get(key);
2820+
remove(node);
2821+
setHead(node);
2822+
return node.value;
2823+
}
2824+
return null;
2825+
}
2826+
2827+
public void remove(Node node) {
2828+
if (node.pre != null) {
2829+
node.pre.next = node.next;
2830+
} else {
2831+
head = node.next;
2832+
}
2833+
if (node.next != null) {
2834+
node.next.pre = node.pre;
2835+
} else {
2836+
end = node.pre;
2837+
}
2838+
}
2839+
2840+
public void setHead(Node node) {
2841+
node.next = head;
2842+
node.pre = null;
2843+
if (head != null) {
2844+
head.pre = node;
2845+
}
2846+
head = node;
2847+
if (end == null) {
2848+
end = head;
2849+
}
2850+
}
2851+
2852+
public void set(K key, V value) {
2853+
if (map.containsKey(key)) {
2854+
Node<K, V> old = map.get(key);
2855+
old.value = value;
2856+
remove(old);
2857+
setHead(old);
2858+
} else {
2859+
Node<K, V> created = new Node<K, V>(key, value);
2860+
if (map.size() >= capacity) {
2861+
map.remove(end.key);
2862+
remove(end);
2863+
setHead(created);
2864+
} else {
2865+
setHead(created);
2866+
}
2867+
map.put(key, created);
2868+
}
2869+
}
2870+
}
2871+
2872+
public static class Node<K, V> {
2873+
private K key;
2874+
private V value;
2875+
private Node pre;
2876+
private Node next;
2877+
2878+
public Node(K key, V value) {
2879+
this.key = key;
2880+
this.value = value;
2881+
}
2882+
}
2883+
2884+
public static <K, V> LRUCache<K, V> createLRUCache(final int capacity) {
2885+
return new LRUCache<K, V>(capacity);
2886+
}
28062887
}

src/test/java/com/github/underscore/lodash/MathTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.junit.Test;
3232
import static java.util.Arrays.asList;
3333
import static org.junit.Assert.assertEquals;
34+
import static org.junit.Assert.assertNull;
3435

3536
/**
3637
* Underscore library unit test.
@@ -194,6 +195,21 @@ public void sumOfInt() {
194195
assertEquals(34, sum);
195196
}
196197

198+
@Test
199+
public void createLRUCache() {
200+
new $.LRUCache<Integer, String>(0);
201+
$.LRUCache<Integer, String> cache = $.createLRUCache(2);
202+
cache.set(0, "Value 0");
203+
assertEquals("Value 0", cache.get(0));
204+
assertNull(cache.get(1));
205+
cache.set(1, "Value 1");
206+
assertEquals("Value 1", cache.get(1));
207+
cache.set(1, "Value 1+");
208+
assertEquals("Value 1+", cache.get(1));
209+
cache.set(2, "Value 2");
210+
assertEquals("Value 2", cache.get(2));
211+
}
212+
197213
@SuppressWarnings("unchecked")
198214
@Test
199215
public void main() {

0 commit comments

Comments
 (0)