Loading src/main/java/cz/muni/fi/pb162/hw02/impl/DefaultCacheFactory.java +20 −1 Original line number Diff line number Diff line Loading @@ -4,6 +4,8 @@ import cz.muni.fi.pb162.hw02.Cache; import cz.muni.fi.pb162.hw02.CacheFactory; import cz.muni.fi.pb162.hw02.CacheType; import java.io.IOException; public class DefaultCacheFactory<K, V> implements CacheFactory<K, V> { /** * Creates an instance of a cache Loading @@ -14,6 +16,23 @@ public class DefaultCacheFactory<K, V> implements CacheFactory<K, V> { */ @Override public Cache<K, V> create(CacheType type, int size) { try { switch (type) { case LRU: return new LRUCache<>(size); case MRU: return new MRUCache<>(size); case FIFO: return new FIFOCache<>(size); case RANDOM: return new RandomCache<>(size); default: return null; } } catch (IOException e) { System.out.println(e.toString()); return null; } } } src/main/java/cz/muni/fi/pb162/hw02/impl/FIFOCache.java 0 → 100644 +79 −0 Original line number Diff line number Diff line package cz.muni.fi.pb162.hw02.impl; import cz.muni.fi.pb162.hw02.CacheType; import java.io.IOException; import java.util.ArrayList; public class FIFOCache <K, V> extends RegularCache<K, V>{ /** * List for FIFO */ private final ArrayList<Integer> FIFO; /** * Type of cache */ private static final CacheType type = CacheType.FIFO; /** * FIFO cache constructor * * @param maxSize Maximal size of Cache * @throws IOException If maxSize is less than 1 */ public FIFOCache(int maxSize) throws IOException { super(maxSize); this.FIFO = new ArrayList<>(maxSize); } /** * Retrieves object from cache * * @param key key under which the object is stored * @return object stored under given key */ @Override public V get(K key){ int index = this.getIndex(key); if (index == -1) { return null; } return super.get(key); } /** * Puts new object into cache * * @param key key under which the object is stored * @param value object stored into cache */ @Override public void put(K key, V value) { int index = this.getIndex(key); if (index != -1){ this.refreshIndex(key, value, 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)); return; } this.refreshIndex(key, value, this.FIFO.get(0)); this.FIFO.remove(0); this.FIFO.add(this.getIndex(key)); } /** * Returns type of the cache * * @return cache type */ @Override public CacheType getEvictionType() { return type; } } src/main/java/cz/muni/fi/pb162/hw02/impl/LRUCache.java 0 → 100644 +97 −0 Original line number Diff line number Diff line package cz.muni.fi.pb162.hw02.impl; import cz.muni.fi.pb162.hw02.CacheType; import java.io.IOException; import java.util.ArrayList; public class LRUCache <K, V> extends RegularCache<K, V> { /** * List of usages */ private final ArrayList<Integer> usage; /** * Type of cache */ private static final CacheType type = CacheType.LRU; /** * Constructor * * @param maxSize MAx size of cache * @throws IOException if maxSize is less than 1 */ public LRUCache(int maxSize) throws IOException { super(maxSize); this.usage = new ArrayList<>(maxSize); } /** * Retrieves object from cache * * @param key key under which the object is stored * @return object stored under given key */ @Override public V get(K key){ int index = this.getIndex(key); if (index == -1) { return null; } this.usage.set(index, (this.usage.get(index) + 1)); return super.get(key); } /** * Puts new object into cache * * @param key key under which the object is stored * @param value object stored into cache */ @Override public void put(K key, V value) { int index = this.getIndex(key); if (index != -1){ this.refreshIndex(key, value, index); this.usage.set(index, 1); return; } if (this.capacity() > this.size()){ this.add(key, value); index = this.getIndex(key); this.usage.add(index, 1); return; } this.refreshIndex(key, value, this.min()); index = this.getIndex(key); this.usage.set(index, 1); } /** * Returns type of the cache * * @return cache type */ @Override public CacheType getEvictionType() { return type; } /** * Returns index of LRU * * @return index of LRU */ 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) > this.usage.get(i)) { result = i; } } return result; } } No newline at end of file src/main/java/cz/muni/fi/pb162/hw02/impl/MRUCache.java 0 → 100644 +97 −0 Original line number Diff line number Diff line package cz.muni.fi.pb162.hw02.impl; import cz.muni.fi.pb162.hw02.CacheType; import java.io.IOException; import java.util.ArrayList; public class MRUCache <K, V> extends RegularCache<K, V>{ /** * List of usages */ private final ArrayList<Integer> usage; /** * Type of cache */ private static final CacheType type = CacheType.MRU; /** * Constructor * * @param maxSize MAx size of cache * @throws IOException if maxSize is less than 1 */ public MRUCache(int maxSize) throws IOException { super(maxSize); this.usage = new ArrayList<>(maxSize); } /** * Retrieves object from cache * * @param key key under which the object is stored * @return object stored under given key */ @Override public V get(K key){ int index = this.getIndex(key); if (index == -1) { return null; } this.usage.set(index, (this.usage.get(index) + 1)); return super.get(key); } /** * Puts new object into cache * * @param key key under which the object is stored * @param value object stored into cache */ @Override public void put(K key, V value) { int index = this.getIndex(key); if (index != -1){ this.refreshIndex(key, value, index); this.usage.set(index, 1); return; } if (this.capacity() > this.size()){ this.add(key, value); index = this.getIndex(key); this.usage.add(index, 1); return; } this.refreshIndex(key, value, this.min()); index = this.getIndex(key); this.usage.set(index, 1); } /** * Returns type of the cache * * @return cache type */ @Override public CacheType getEvictionType() { return type; } /** * Returns index of MRU * * @return index of MRU */ 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) <= this.usage.get(i)) { result = i; } } return result; } } No newline at end of file src/main/java/cz/muni/fi/pb162/hw02/impl/RandomCache.java 0 → 100644 +69 −0 Original line number Diff line number Diff line package cz.muni.fi.pb162.hw02.impl; import cz.muni.fi.pb162.hw02.CacheType; import java.io.IOException; import java.util.Random; public class RandomCache <K, V> extends RegularCache<K, V> { /** * Type of cache */ private static final CacheType type = CacheType.RANDOM; /** * Constructor * * @param maxSize MAx size of cache * @throws IOException if maxSize is less than 1 */ public RandomCache(int maxSize) throws IOException { super(maxSize); } /** * Retrieves object from cache * * @param key key under which the object is stored * @return object stored under given key */ @Override public V get(K key){ int index = this.getIndex(key); if (index == -1) { return null; } return super.get(key); } /** * Puts new object into cache * * @param key key under which the object is stored * @param value object stored into cache */ @Override public void put(K key, V value) { int index = this.getIndex(key); if (index != -1){ this.refreshIndex(key, value, index); return; } if (this.capacity() > this.size()){ this.add(key, value); return; } this.refreshIndex(key, value, new Random().nextInt(this.capacity())); } /** * Returns type of the cache * * @return cache type */ @Override public CacheType getEvictionType() { return type; } } Loading
src/main/java/cz/muni/fi/pb162/hw02/impl/DefaultCacheFactory.java +20 −1 Original line number Diff line number Diff line Loading @@ -4,6 +4,8 @@ import cz.muni.fi.pb162.hw02.Cache; import cz.muni.fi.pb162.hw02.CacheFactory; import cz.muni.fi.pb162.hw02.CacheType; import java.io.IOException; public class DefaultCacheFactory<K, V> implements CacheFactory<K, V> { /** * Creates an instance of a cache Loading @@ -14,6 +16,23 @@ public class DefaultCacheFactory<K, V> implements CacheFactory<K, V> { */ @Override public Cache<K, V> create(CacheType type, int size) { try { switch (type) { case LRU: return new LRUCache<>(size); case MRU: return new MRUCache<>(size); case FIFO: return new FIFOCache<>(size); case RANDOM: return new RandomCache<>(size); default: return null; } } catch (IOException e) { System.out.println(e.toString()); return null; } } }
src/main/java/cz/muni/fi/pb162/hw02/impl/FIFOCache.java 0 → 100644 +79 −0 Original line number Diff line number Diff line package cz.muni.fi.pb162.hw02.impl; import cz.muni.fi.pb162.hw02.CacheType; import java.io.IOException; import java.util.ArrayList; public class FIFOCache <K, V> extends RegularCache<K, V>{ /** * List for FIFO */ private final ArrayList<Integer> FIFO; /** * Type of cache */ private static final CacheType type = CacheType.FIFO; /** * FIFO cache constructor * * @param maxSize Maximal size of Cache * @throws IOException If maxSize is less than 1 */ public FIFOCache(int maxSize) throws IOException { super(maxSize); this.FIFO = new ArrayList<>(maxSize); } /** * Retrieves object from cache * * @param key key under which the object is stored * @return object stored under given key */ @Override public V get(K key){ int index = this.getIndex(key); if (index == -1) { return null; } return super.get(key); } /** * Puts new object into cache * * @param key key under which the object is stored * @param value object stored into cache */ @Override public void put(K key, V value) { int index = this.getIndex(key); if (index != -1){ this.refreshIndex(key, value, 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)); return; } this.refreshIndex(key, value, this.FIFO.get(0)); this.FIFO.remove(0); this.FIFO.add(this.getIndex(key)); } /** * Returns type of the cache * * @return cache type */ @Override public CacheType getEvictionType() { return type; } }
src/main/java/cz/muni/fi/pb162/hw02/impl/LRUCache.java 0 → 100644 +97 −0 Original line number Diff line number Diff line package cz.muni.fi.pb162.hw02.impl; import cz.muni.fi.pb162.hw02.CacheType; import java.io.IOException; import java.util.ArrayList; public class LRUCache <K, V> extends RegularCache<K, V> { /** * List of usages */ private final ArrayList<Integer> usage; /** * Type of cache */ private static final CacheType type = CacheType.LRU; /** * Constructor * * @param maxSize MAx size of cache * @throws IOException if maxSize is less than 1 */ public LRUCache(int maxSize) throws IOException { super(maxSize); this.usage = new ArrayList<>(maxSize); } /** * Retrieves object from cache * * @param key key under which the object is stored * @return object stored under given key */ @Override public V get(K key){ int index = this.getIndex(key); if (index == -1) { return null; } this.usage.set(index, (this.usage.get(index) + 1)); return super.get(key); } /** * Puts new object into cache * * @param key key under which the object is stored * @param value object stored into cache */ @Override public void put(K key, V value) { int index = this.getIndex(key); if (index != -1){ this.refreshIndex(key, value, index); this.usage.set(index, 1); return; } if (this.capacity() > this.size()){ this.add(key, value); index = this.getIndex(key); this.usage.add(index, 1); return; } this.refreshIndex(key, value, this.min()); index = this.getIndex(key); this.usage.set(index, 1); } /** * Returns type of the cache * * @return cache type */ @Override public CacheType getEvictionType() { return type; } /** * Returns index of LRU * * @return index of LRU */ 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) > this.usage.get(i)) { result = i; } } return result; } } No newline at end of file
src/main/java/cz/muni/fi/pb162/hw02/impl/MRUCache.java 0 → 100644 +97 −0 Original line number Diff line number Diff line package cz.muni.fi.pb162.hw02.impl; import cz.muni.fi.pb162.hw02.CacheType; import java.io.IOException; import java.util.ArrayList; public class MRUCache <K, V> extends RegularCache<K, V>{ /** * List of usages */ private final ArrayList<Integer> usage; /** * Type of cache */ private static final CacheType type = CacheType.MRU; /** * Constructor * * @param maxSize MAx size of cache * @throws IOException if maxSize is less than 1 */ public MRUCache(int maxSize) throws IOException { super(maxSize); this.usage = new ArrayList<>(maxSize); } /** * Retrieves object from cache * * @param key key under which the object is stored * @return object stored under given key */ @Override public V get(K key){ int index = this.getIndex(key); if (index == -1) { return null; } this.usage.set(index, (this.usage.get(index) + 1)); return super.get(key); } /** * Puts new object into cache * * @param key key under which the object is stored * @param value object stored into cache */ @Override public void put(K key, V value) { int index = this.getIndex(key); if (index != -1){ this.refreshIndex(key, value, index); this.usage.set(index, 1); return; } if (this.capacity() > this.size()){ this.add(key, value); index = this.getIndex(key); this.usage.add(index, 1); return; } this.refreshIndex(key, value, this.min()); index = this.getIndex(key); this.usage.set(index, 1); } /** * Returns type of the cache * * @return cache type */ @Override public CacheType getEvictionType() { return type; } /** * Returns index of MRU * * @return index of MRU */ 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) <= this.usage.get(i)) { result = i; } } return result; } } No newline at end of file
src/main/java/cz/muni/fi/pb162/hw02/impl/RandomCache.java 0 → 100644 +69 −0 Original line number Diff line number Diff line package cz.muni.fi.pb162.hw02.impl; import cz.muni.fi.pb162.hw02.CacheType; import java.io.IOException; import java.util.Random; public class RandomCache <K, V> extends RegularCache<K, V> { /** * Type of cache */ private static final CacheType type = CacheType.RANDOM; /** * Constructor * * @param maxSize MAx size of cache * @throws IOException if maxSize is less than 1 */ public RandomCache(int maxSize) throws IOException { super(maxSize); } /** * Retrieves object from cache * * @param key key under which the object is stored * @return object stored under given key */ @Override public V get(K key){ int index = this.getIndex(key); if (index == -1) { return null; } return super.get(key); } /** * Puts new object into cache * * @param key key under which the object is stored * @param value object stored into cache */ @Override public void put(K key, V value) { int index = this.getIndex(key); if (index != -1){ this.refreshIndex(key, value, index); return; } if (this.capacity() > this.size()){ this.add(key, value); return; } this.refreshIndex(key, value, new Random().nextInt(this.capacity())); } /** * Returns type of the cache * * @return cache type */ @Override public CacheType getEvictionType() { return type; } }