Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* JasperReports - Free Java Reporting Library.
* Copyright (C) 2005 Works, Inc. All rights reserved.
* http://www.works.com
* Copyright (C) 2005 - 2023 Cloud Software Group, Inc. All rights reserved.
* http://www.jaspersoft.com
*
* Unless you have purchased a commercial license agreement from Jaspersoft,
* the following license terms apply:
*
* This program is part of JasperReports.
*
* JasperReports 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.
*
* JasperReports 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 JasperReports. If not, see .
*/
/*
* Licensed to Jaspersoft Corporation under a Contributer Agreement
*/
package net.sf.jasperreports.engine.fill;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map.Entry;
import org.apache.commons.collections4.map.ReferenceMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import net.sf.jasperreports.engine.JRRuntimeException;
import net.sf.jasperreports.engine.JRVirtualizable;
import net.sf.jasperreports.engine.JRVirtualizer;
import net.sf.jasperreports.engine.util.LocalVirtualizationSerializer;
import net.sf.jasperreports.engine.util.VirtualizationSerializer;
/**
* Abstract base for LRU and serialization based virtualizer
*
* @author John Bindel
*/
public abstract class JRAbstractLRUVirtualizer implements JRVirtualizer
{
private static final Log log = LogFactory.getLog(JRAbstractLRUVirtualizer.class);
protected static class CacheReference extends WeakReference
{
private final String id;
public CacheReference(JRVirtualizable o, ReferenceQueue queue)
{
super(o, queue);
id = o.getUID();
}
public String getId()
{
return id;
}
}
/**
* This class keeps track of how many objects are currently in memory, and
* when there are too many, it pushes the last touched one to disk.
*/
protected class Cache
{
private final int maxSize;
private final ReferenceQueue refQueue;
private final LinkedHashMap map;
Cache(int maxSize)
{
this.maxSize = maxSize;
map = new LinkedHashMap<>(16, 0.75f, true);
refQueue = new ReferenceQueue<>();
}
protected JRVirtualizable getMapValue(CacheReference val)
{
JRVirtualizable o;
if (val == null)
{
o = null;
}
else
{
if (val.isEnqueued())
{
o = null;
}
else
{
o = val.get();
}
}
return o;
}
protected CacheReference toMapValue(JRVirtualizable val)
{
return val == null ? null : new CacheReference(val, refQueue);
}
protected void purge()
{
CacheReference ref;
while ((ref = (CacheReference) refQueue.poll()) != null)
{
map.remove(ref.getId());
}
}
public boolean contains(String id)
{
purge();
return map.containsKey(id);
}
public JRVirtualizable get(String id)
{
purge();
return getMapValue(map.get(id));
}
public JRVirtualizable put(String id, JRVirtualizable o)
{
purge();
return getMapValue(map.put(id, toMapValue(o)));
}
public List evictionCandidates()
{
if (map.size() <= maxSize)
{
return Collections.emptyList();
}
int candidateCount = map.size() - maxSize;
List candidates = new ArrayList<>();
Iterator> mapIterator = map.entrySet().iterator();
while (candidates.size() < candidateCount && mapIterator.hasNext())
{
Entry entry = mapIterator.next();
JRVirtualizable value = getMapValue(entry.getValue());
if (value == null)
{
// this entry will get removed by purge()
--candidateCount;
}
else if (isEvictable(value))
{
if (log.isDebugEnabled())
{
log.debug("LRU eviction candidate: " + entry.getKey());
}
candidates.add(value);
}
}
if (candidates.size() < candidateCount)
{
log.debug("The virtualizer is used by more contexts than its in-memory cache size " + maxSize);
}
return candidates;
}
public JRVirtualizable remove(String id)
{
purge();
return getMapValue(map.remove(id));
}
public Iterator idIterator()
{
purge();
final Iterator valsIt = map.values().iterator();
return new Iterator()
{
@Override
public boolean hasNext()
{
return valsIt.hasNext();
}
@Override
public String next()
{
CacheReference ref = valsIt.next();
return ref.getId();
}
@Override
public void remove()
{
valsIt.remove();
}
};
}
}
protected final VirtualizationSerializer serializer;
protected final Cache pagedIn;
protected final ReferenceMap pagedOut;
protected volatile WeakReference lastObjectRef;
protected ReferenceMap lastObjectMap;
protected ReferenceMap