Shuji Watanabe avatar Shuji Watanabe committed a9b580c

removeでaを削除すると何も起こらない
WegihtContainer で換装

Comments (0)

Files changed (5)

src/main/java/tddbc/lrucache/LruCache.java

 package tddbc.lrucache;
 
 import java.util.HashMap;
-import java.util.LinkedList;
 import java.util.Map;
 
 public class LruCache {
 
     private final Map<String, String> values = new HashMap<String, String>();
-    LinkedList<String> order = new LinkedList<String>();
+    //  LinkedList<String> order = new LinkedList<String>();
+    WeightContainer order = new WeightContainer();
     private int cacheSize = 2;
 
     public LruCache(int cacheSize) {
 
     public void put(String key, String value) {
         values.put(key, value);
-        order.add(key);
+        order.register(key, System.currentTimeMillis());
         compact();
     }
 
     public String get(String key) {
         order.remove(key);
-        order.add(key);
+        order.register(key, System.currentTimeMillis());
         return values.get(key);
     }
 

src/main/java/tddbc/lrucache/WeightContainer.java

     public void remove(String element) {
         list.remove(element);
         Long weight = weights.remove(element);
+        if (weight == null) return;
         LinkedList<String> list = map.get(weight);
         list.remove(element);
         if (list.isEmpty()) {
         Long w = this.map.firstKey();
         LinkedList<String> l = this.map.get(w);
         String e = l.removeFirst();
-        if (l.isEmpty()){
+        if (l.isEmpty()) {
             this.map.remove(w);
         }
         this.weights.remove(e);
         this.list.remove(e);
-        
+
         return e;
     }
 

src/test/java/tddbc/lrucache/LruCacheFunctionalTest.java

 import static org.hamcrest.CoreMatchers.*;
 import static org.junit.Assert.*;
 
+import org.junit.Ignore;
 import org.junit.Test;
 
 public class LruCacheFunctionalTest {

src/test/java/tddbc/lrucache/LruCacheTest.java

 
 import static org.hamcrest.CoreMatchers.*;
 import static org.junit.Assert.*;
-import static org.mockito.Mockito.*;
-
-import java.util.List;
 
 import org.junit.Before;
 import org.junit.Test;
         @Test
         public void orderにabが格納されている() throws Exception {
             // Exercise
-            List<String> acutual = sut.order;
+            WeightContainer acutual = sut.order;
             // Verify
             assertThat(acutual.size(), is(2));
             assertThat(acutual.get(0), is("a"));
         @Test
         public void orderに最初のキーが消えてbcが格納されている() throws Exception {
             // Exercise
-            List<String> acutual = sut.order;
+            WeightContainer acutual = sut.order;
             // Verify
             assertThat(acutual.size(), is(2));
             assertThat(acutual.get(0), is("b"));
         @Test
         public void orderにaが格納されている() throws Exception {
             // Exercise
-            List<String> acutual = sut.order;
+            WeightContainer acutual = sut.order;
             // Verify
             assertThat(acutual.size(), is(1));
             assertThat(acutual.get(0), is("a"));

src/test/java/tddbc/lrucache/WeightContainerTest.java

             // Verify
             assertThat(sut.getWeight("b"), is(2000L));
         }
+        
+        @Test
+        public void removeでaを削除すると何も起こらない() throws Exception {
+            // Exercise
+            sut.remove("a");
+            // Verify
+            assertThat(sut.size(), is(0));
+        }
 
     }
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.