Commit cc5be8d7 authored by Richard Pánek's avatar Richard Pánek
Browse files

Done

parent 6df6ba27
Loading
Loading
Loading
Loading
Loading
+20 −1
Original line number Diff line number Diff line
@@ -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
@@ -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;
        }
    }
}
+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;
    }
}
+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
+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
+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