From 76c07a90cb239411d9305821df395ac1b38a0fbc Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Thu, 8 Sep 2016 02:25:09 +0200 Subject: [PATCH] Added generic Cache classes. --- src/fr/devinsy/xidyn/utils/cache/Cache.java | 174 ++++++++++++++++++ .../devinsy/xidyn/utils/cache/CacheItem.java | 127 +++++++++++++ .../xidyn/utils/cache/CacheStrategy.java | 79 ++++++++ 3 files changed, 380 insertions(+) create mode 100644 src/fr/devinsy/xidyn/utils/cache/Cache.java create mode 100644 src/fr/devinsy/xidyn/utils/cache/CacheItem.java create mode 100644 src/fr/devinsy/xidyn/utils/cache/CacheStrategy.java diff --git a/src/fr/devinsy/xidyn/utils/cache/Cache.java b/src/fr/devinsy/xidyn/utils/cache/Cache.java new file mode 100644 index 0000000..4405fda --- /dev/null +++ b/src/fr/devinsy/xidyn/utils/cache/Cache.java @@ -0,0 +1,174 @@ +/** + * Copyright (C) 2016 Christian Pierre MOMON + * + * This file is part of Xidyn. + * + * Xidyn is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Xidyn is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Xidyn. If not, see + */ +package fr.devinsy.xidyn.utils.cache; + +import java.util.HashMap; +import java.util.Map; + +import fr.devinsy.xidyn.utils.cache.CacheStrategy.Strategy; + +/** + * + */ +public class Cache +{ + private CacheStrategy strategy; + private Map> map; + + /** + * + */ + public Cache() + { + this.strategy = new CacheStrategy(Strategy.NONE, 0); + this.map = new HashMap>(); + } + + /** + * + */ + public Cache(final int initialCapacity) + { + this.strategy = new CacheStrategy(Strategy.NONE, 0); + this.map = new HashMap>(initialCapacity); + } + + /** + * + */ + public void clear() + { + this.map.clear(); + } + + /** + * + * @param key + * @return + * @throws Exception + */ + public T get(final Object key) + { + T result; + + CacheItem item = this.map.get(key); + + if (item == null) + { + result = null; + } + else + { + result = item.getValue(); + } + + // + return result; + } + + /** + * + * @return + */ + public boolean isEmpty() + { + boolean result; + + result = this.map.isEmpty(); + + // + return result; + } + + /** + * + */ + public void purge() + { + switch (this.strategy.getStrategy()) + { + case NONE: + { + } + break; + + case SIZE: + { + if (this.map.size() > this.strategy.getParameter()) + { + // TODO + } + } + break; + + case TIME: + { + // TODO + } + break; + } + } + + /** + * + * @param source + * @return + */ + public T put(final Object key, final T source) + { + T result; + + if (source != null) + { + CacheItem item = this.map.get(key); + + if (item == null) + { + purge(); + item = new CacheItem(source); + this.map.put(key, item); + } + else + { + item.setValue(source); + } + } + + result = source; + + // + return result; + } + + /** + * + * @return + */ + public int size() + { + int result; + + result = this.map.size(); + + // + return result; + } +} + +// //////////////////////////////////////////////////////////////////////// \ No newline at end of file diff --git a/src/fr/devinsy/xidyn/utils/cache/CacheItem.java b/src/fr/devinsy/xidyn/utils/cache/CacheItem.java new file mode 100644 index 0000000..727d8af --- /dev/null +++ b/src/fr/devinsy/xidyn/utils/cache/CacheItem.java @@ -0,0 +1,127 @@ +/** + * Copyright (C) 2016 Christian Pierre MOMON + * + * This file is part of Xidyn. + * + * Xidyn is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Xidyn is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Xidyn. If not, see + */ +package fr.devinsy.xidyn.utils.cache; + +import java.util.Date; + +/** + * + */ +public class CacheItem +{ + private T value; + private long creationDate; + private long editionDate; + private long accessDate; + + /** + * + * @param source + */ + public CacheItem(final T source) + { + this.value = source; + this.creationDate = time(); + this.editionDate = this.creationDate; + this.accessDate = this.creationDate; + } + + /** + * + * @return + */ + public long accessDate() + { + long result; + + result = this.accessDate; + + // + return result; + } + + /** + * + * @return + */ + public long creationDate() + { + long result; + + result = this.creationDate; + + // + return result; + } + + /** + * + * @return + */ + public long editionDate() + { + long result; + + result = this.editionDate; + + // + return result; + } + + /** + * + * @return + */ + public T getValue() + { + T result; + + result = this.value; + this.accessDate = time(); + + // + return result; + } + + /** + * + * @param source + */ + public void setValue(final T source) + { + this.value = source; + this.editionDate = time(); + } + + /** + * + * @return + */ + private static long time() + { + long result; + + result = new Date().getTime(); + + // + return result; + } +} + +// //////////////////////////////////////////////////////////////////////// \ No newline at end of file diff --git a/src/fr/devinsy/xidyn/utils/cache/CacheStrategy.java b/src/fr/devinsy/xidyn/utils/cache/CacheStrategy.java new file mode 100644 index 0000000..1e8d9f3 --- /dev/null +++ b/src/fr/devinsy/xidyn/utils/cache/CacheStrategy.java @@ -0,0 +1,79 @@ +/** + * Copyright (C)2016 Christian Pierre MOMON + * + * This file is part of Xidyn. + * + * Xidyn is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Xidyn is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Xidyn. If not, see + */ +package fr.devinsy.xidyn.utils.cache; + +/** + * + */ +public class CacheStrategy +{ + public enum Strategy + { + NONE, + SIZE, + TIME + } + + private Strategy strategy; + private long parameter; + + /** + * + * @param strategy + * @param parameter + */ + public CacheStrategy(final Strategy strategy, final long parameter) + { + if (strategy == null) + { + throw new IllegalArgumentException("Null strategy."); + } + else if (parameter < 0) + { + throw new IllegalArgumentException("Negative parameter."); + } + else + { + this.strategy = strategy; + this.parameter = parameter; + } + } + + public long getParameter() + { + return this.parameter; + } + + public Strategy getStrategy() + { + return this.strategy; + } + + public void setParameter(final long parameter) + { + this.parameter = parameter; + } + + public void setStrategy(final Strategy strategy) + { + this.strategy = strategy; + } +} + +// //////////////////////////////////////////////////////////////////////// \ No newline at end of file