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