Added generic Cache classes.
This commit is contained in:
parent
c4ba76ed8d
commit
76c07a90cb
3 changed files with 380 additions and 0 deletions
174
src/fr/devinsy/xidyn/utils/cache/Cache.java
vendored
Normal file
174
src/fr/devinsy/xidyn/utils/cache/Cache.java
vendored
Normal file
|
@ -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 <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
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<T>
|
||||
{
|
||||
private CacheStrategy strategy;
|
||||
private Map<Object, CacheItem<T>> map;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public Cache()
|
||||
{
|
||||
this.strategy = new CacheStrategy(Strategy.NONE, 0);
|
||||
this.map = new HashMap<Object, CacheItem<T>>();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public Cache(final int initialCapacity)
|
||||
{
|
||||
this.strategy = new CacheStrategy(Strategy.NONE, 0);
|
||||
this.map = new HashMap<Object, CacheItem<T>>(initialCapacity);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void clear()
|
||||
{
|
||||
this.map.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param key
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public T get(final Object key)
|
||||
{
|
||||
T result;
|
||||
|
||||
CacheItem<T> 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<T> item = this.map.get(key);
|
||||
|
||||
if (item == null)
|
||||
{
|
||||
purge();
|
||||
item = new CacheItem<T>(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;
|
||||
}
|
||||
}
|
||||
|
||||
// ////////////////////////////////////////////////////////////////////////
|
127
src/fr/devinsy/xidyn/utils/cache/CacheItem.java
vendored
Normal file
127
src/fr/devinsy/xidyn/utils/cache/CacheItem.java
vendored
Normal file
|
@ -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 <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
package fr.devinsy.xidyn.utils.cache;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class CacheItem<T>
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
// ////////////////////////////////////////////////////////////////////////
|
79
src/fr/devinsy/xidyn/utils/cache/CacheStrategy.java
vendored
Normal file
79
src/fr/devinsy/xidyn/utils/cache/CacheStrategy.java
vendored
Normal file
|
@ -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 <http://www.gnu.org/licenses/>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
// ////////////////////////////////////////////////////////////////////////
|
Loading…
Reference in a new issue