Loading src/main/java/cz/muni/fi/pb162/hw02/impl/DefaultCacheFactory.java +7 −2 Original line number Diff line number Diff line Loading @@ -6,6 +6,12 @@ import cz.muni.fi.pb162.hw02.CacheType; import java.io.IOException; /** * @author Richard Panek * * @param <K> Key type * @param <V> Value type */ public class DefaultCacheFactory<K, V> implements CacheFactory<K, V> { /** * Creates an instance of a cache Loading @@ -29,8 +35,7 @@ public class DefaultCacheFactory<K, V> implements CacheFactory<K, V> { default: return null; } } catch (IOException e) { } catch (IOException e) { System.out.println(e.toString()); return null; } Loading src/main/java/cz/muni/fi/pb162/hw02/impl/FIFOCache.java +16 −10 Original line number Diff line number Diff line Loading @@ -5,16 +5,22 @@ import cz.muni.fi.pb162.hw02.CacheType; import java.io.IOException; import java.util.ArrayList; /** * @author Richard Panek * * @param <K> Key type * @param <V> Value type */ public class FIFOCache <K, V> extends RegularCache<K, V>{ /** * List for FIFO */ private final ArrayList<Integer> FIFO; private final ArrayList<Integer> fifo; /** * Type of cache */ private static final CacheType type = CacheType.FIFO; private static final CacheType TYPE = CacheType.FIFO; /** * FIFO cache constructor Loading @@ -24,7 +30,7 @@ public class FIFOCache <K, V> extends RegularCache<K, V>{ */ public FIFOCache(int maxSize) throws IOException { super(maxSize); this.FIFO = new ArrayList<>(maxSize); this.fifo = new ArrayList<>(maxSize); } /** Loading Loading @@ -53,18 +59,18 @@ public class FIFOCache <K, V> extends RegularCache<K, V>{ int index = this.getIndex(key); if (index != -1){ this.refreshIndex(key, value, index); this.FIFO.remove((Integer) index); this.FIFO.add(index); this.fifo.remove((Integer) index); this.fifo.add(index); return; } if (this.capacity() > this.size()){ this.add(key, value); this.FIFO.add(this.getIndex(key)); this.fifo.add(this.getIndex(key)); return; } this.refreshIndex(key, value, this.FIFO.get(0)); this.FIFO.remove(0); this.FIFO.add(this.getIndex(key)); this.refreshIndex(key, value, this.fifo.get(0)); this.fifo.remove(0); this.fifo.add(this.getIndex(key)); } /** Loading @@ -74,6 +80,6 @@ public class FIFOCache <K, V> extends RegularCache<K, V>{ */ @Override public CacheType getEvictionType() { return type; return TYPE; } } src/main/java/cz/muni/fi/pb162/hw02/impl/LRUCache.java +10 −3 Original line number Diff line number Diff line Loading @@ -5,6 +5,12 @@ import cz.muni.fi.pb162.hw02.CacheType; import java.io.IOException; import java.util.ArrayList; /** * @author Richard Panek * * @param <K> Key type * @param <V> Value type */ public class LRUCache <K, V> extends RegularCache<K, V> { /** Loading @@ -15,7 +21,7 @@ public class LRUCache <K, V> extends RegularCache<K, V> { /** * Type of cache */ private static final CacheType type = CacheType.LRU; private static final CacheType TYPE = CacheType.LRU; /** * Constructor Loading Loading @@ -76,7 +82,7 @@ public class LRUCache <K, V> extends RegularCache<K, V> { */ @Override public CacheType getEvictionType() { return type; return TYPE; } /** Loading @@ -87,10 +93,11 @@ public class LRUCache <K, V> extends RegularCache<K, V> { private int min() { int result = 0; for (int i = 1; i < this.capacity(); i++) { if (this.usage.get(result) >= 1 && this.usage.get(i) >= 1) if (this.usage.get(result) >= 1 && this.usage.get(i) >= 1) { if (this.usage.get(result) > this.usage.get(i)) { result = i; } } } return result; } Loading src/main/java/cz/muni/fi/pb162/hw02/impl/MRUCache.java +12 −5 Original line number Diff line number Diff line Loading @@ -5,6 +5,12 @@ import cz.muni.fi.pb162.hw02.CacheType; import java.io.IOException; import java.util.ArrayList; /** * @author Richard Panek * * @param <K> Key type * @param <V> Value type */ public class MRUCache <K, V> extends RegularCache<K, V>{ /** Loading @@ -15,7 +21,7 @@ public class MRUCache <K, V> extends RegularCache<K, V>{ /** * Type of cache */ private static final CacheType type = CacheType.MRU; private static final CacheType TYPE = CacheType.MRU; /** * Constructor Loading Loading @@ -64,7 +70,7 @@ public class MRUCache <K, V> extends RegularCache<K, V>{ this.usage.add(index, 1); return; } this.refreshIndex(key, value, this.min()); this.refreshIndex(key, value, this.max()); index = this.getIndex(key); this.usage.set(index, 1); } Loading @@ -76,7 +82,7 @@ public class MRUCache <K, V> extends RegularCache<K, V>{ */ @Override public CacheType getEvictionType() { return type; return TYPE; } /** Loading @@ -84,13 +90,14 @@ public class MRUCache <K, V> extends RegularCache<K, V>{ * * @return index of MRU */ private int min() { private int max() { int result = 0; for (int i = 1; i < this.capacity(); i++) { if (this.usage.get(result) >= 1 && this.usage.get(i) >= 1) if (this.usage.get(result) >= 1 && this.usage.get(i) >= 1) { if (this.usage.get(result) <= this.usage.get(i)) { result = i; } } } return result; } Loading src/main/java/cz/muni/fi/pb162/hw02/impl/RandomCache.java +8 −2 Original line number Diff line number Diff line Loading @@ -5,11 +5,17 @@ import cz.muni.fi.pb162.hw02.CacheType; import java.io.IOException; import java.util.Random; /** * @author Richard Panek * * @param <K> Key type * @param <V> Value type */ public class RandomCache <K, V> extends RegularCache<K, V> { /** * Type of cache */ private static final CacheType type = CacheType.RANDOM; private static final CacheType TYPE = CacheType.RANDOM; /** * Constructor Loading Loading @@ -63,7 +69,7 @@ public class RandomCache <K, V> extends RegularCache<K, V> { */ @Override public CacheType getEvictionType() { return type; return TYPE; } } Loading
src/main/java/cz/muni/fi/pb162/hw02/impl/DefaultCacheFactory.java +7 −2 Original line number Diff line number Diff line Loading @@ -6,6 +6,12 @@ import cz.muni.fi.pb162.hw02.CacheType; import java.io.IOException; /** * @author Richard Panek * * @param <K> Key type * @param <V> Value type */ public class DefaultCacheFactory<K, V> implements CacheFactory<K, V> { /** * Creates an instance of a cache Loading @@ -29,8 +35,7 @@ public class DefaultCacheFactory<K, V> implements CacheFactory<K, V> { default: return null; } } catch (IOException e) { } catch (IOException e) { System.out.println(e.toString()); return null; } Loading
src/main/java/cz/muni/fi/pb162/hw02/impl/FIFOCache.java +16 −10 Original line number Diff line number Diff line Loading @@ -5,16 +5,22 @@ import cz.muni.fi.pb162.hw02.CacheType; import java.io.IOException; import java.util.ArrayList; /** * @author Richard Panek * * @param <K> Key type * @param <V> Value type */ public class FIFOCache <K, V> extends RegularCache<K, V>{ /** * List for FIFO */ private final ArrayList<Integer> FIFO; private final ArrayList<Integer> fifo; /** * Type of cache */ private static final CacheType type = CacheType.FIFO; private static final CacheType TYPE = CacheType.FIFO; /** * FIFO cache constructor Loading @@ -24,7 +30,7 @@ public class FIFOCache <K, V> extends RegularCache<K, V>{ */ public FIFOCache(int maxSize) throws IOException { super(maxSize); this.FIFO = new ArrayList<>(maxSize); this.fifo = new ArrayList<>(maxSize); } /** Loading Loading @@ -53,18 +59,18 @@ public class FIFOCache <K, V> extends RegularCache<K, V>{ int index = this.getIndex(key); if (index != -1){ this.refreshIndex(key, value, index); this.FIFO.remove((Integer) index); this.FIFO.add(index); this.fifo.remove((Integer) index); this.fifo.add(index); return; } if (this.capacity() > this.size()){ this.add(key, value); this.FIFO.add(this.getIndex(key)); this.fifo.add(this.getIndex(key)); return; } this.refreshIndex(key, value, this.FIFO.get(0)); this.FIFO.remove(0); this.FIFO.add(this.getIndex(key)); this.refreshIndex(key, value, this.fifo.get(0)); this.fifo.remove(0); this.fifo.add(this.getIndex(key)); } /** Loading @@ -74,6 +80,6 @@ public class FIFOCache <K, V> extends RegularCache<K, V>{ */ @Override public CacheType getEvictionType() { return type; return TYPE; } }
src/main/java/cz/muni/fi/pb162/hw02/impl/LRUCache.java +10 −3 Original line number Diff line number Diff line Loading @@ -5,6 +5,12 @@ import cz.muni.fi.pb162.hw02.CacheType; import java.io.IOException; import java.util.ArrayList; /** * @author Richard Panek * * @param <K> Key type * @param <V> Value type */ public class LRUCache <K, V> extends RegularCache<K, V> { /** Loading @@ -15,7 +21,7 @@ public class LRUCache <K, V> extends RegularCache<K, V> { /** * Type of cache */ private static final CacheType type = CacheType.LRU; private static final CacheType TYPE = CacheType.LRU; /** * Constructor Loading Loading @@ -76,7 +82,7 @@ public class LRUCache <K, V> extends RegularCache<K, V> { */ @Override public CacheType getEvictionType() { return type; return TYPE; } /** Loading @@ -87,10 +93,11 @@ public class LRUCache <K, V> extends RegularCache<K, V> { private int min() { int result = 0; for (int i = 1; i < this.capacity(); i++) { if (this.usage.get(result) >= 1 && this.usage.get(i) >= 1) if (this.usage.get(result) >= 1 && this.usage.get(i) >= 1) { if (this.usage.get(result) > this.usage.get(i)) { result = i; } } } return result; } Loading
src/main/java/cz/muni/fi/pb162/hw02/impl/MRUCache.java +12 −5 Original line number Diff line number Diff line Loading @@ -5,6 +5,12 @@ import cz.muni.fi.pb162.hw02.CacheType; import java.io.IOException; import java.util.ArrayList; /** * @author Richard Panek * * @param <K> Key type * @param <V> Value type */ public class MRUCache <K, V> extends RegularCache<K, V>{ /** Loading @@ -15,7 +21,7 @@ public class MRUCache <K, V> extends RegularCache<K, V>{ /** * Type of cache */ private static final CacheType type = CacheType.MRU; private static final CacheType TYPE = CacheType.MRU; /** * Constructor Loading Loading @@ -64,7 +70,7 @@ public class MRUCache <K, V> extends RegularCache<K, V>{ this.usage.add(index, 1); return; } this.refreshIndex(key, value, this.min()); this.refreshIndex(key, value, this.max()); index = this.getIndex(key); this.usage.set(index, 1); } Loading @@ -76,7 +82,7 @@ public class MRUCache <K, V> extends RegularCache<K, V>{ */ @Override public CacheType getEvictionType() { return type; return TYPE; } /** Loading @@ -84,13 +90,14 @@ public class MRUCache <K, V> extends RegularCache<K, V>{ * * @return index of MRU */ private int min() { private int max() { int result = 0; for (int i = 1; i < this.capacity(); i++) { if (this.usage.get(result) >= 1 && this.usage.get(i) >= 1) if (this.usage.get(result) >= 1 && this.usage.get(i) >= 1) { if (this.usage.get(result) <= this.usage.get(i)) { result = i; } } } return result; } Loading
src/main/java/cz/muni/fi/pb162/hw02/impl/RandomCache.java +8 −2 Original line number Diff line number Diff line Loading @@ -5,11 +5,17 @@ import cz.muni.fi.pb162.hw02.CacheType; import java.io.IOException; import java.util.Random; /** * @author Richard Panek * * @param <K> Key type * @param <V> Value type */ public class RandomCache <K, V> extends RegularCache<K, V> { /** * Type of cache */ private static final CacheType type = CacheType.RANDOM; private static final CacheType TYPE = CacheType.RANDOM; /** * Constructor Loading Loading @@ -63,7 +69,7 @@ public class RandomCache <K, V> extends RegularCache<K, V> { */ @Override public CacheType getEvictionType() { return type; return TYPE; } }