org.jsl.collider.ObjectCache Maven / Gradle / Ivy
/*
* Copyright (C) 2013 Sergey Zubarev, [email protected]
*
* This file is a part of JS-Collider framework.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see .
*/
package org.jsl.collider;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Level;
import java.util.logging.Logger;
public abstract class ObjectCache
{
private final String m_name;
private final ReentrantLock m_lock;
private final TYPE [] m_cache;
private int m_size;
private int m_gets;
private int m_puts;
private int m_miss;
protected abstract TYPE allocateObject();
public ObjectCache( String name, TYPE [] cache )
{
m_name = name;
m_lock = new ReentrantLock();
m_cache = cache;
}
public final boolean put( TYPE obj )
{
m_lock.lock();
try
{
m_puts++;
if (m_size < m_cache.length)
{
final int idx = m_size++;
assert( m_cache[idx] == null );
m_cache[idx] = obj;
return true;
}
}
finally
{
m_lock.unlock();
}
return false;
}
public final TYPE get()
{
m_lock.lock();
try
{
m_gets++;
if (m_size > 0)
{
final int idx = --m_size;
assert( m_cache[idx] != null );
TYPE ret = m_cache[idx];
m_cache[idx] = null;
return ret;
}
m_miss++;
}
finally
{
m_lock.unlock();
}
return allocateObject();
}
public final void clear( Logger logger )
{
for (int idx=0; idx
© 2015 - 2024 Weber Informatics LLC | Privacy Policy