package org.ehcache.impl.config;
import org.ehcache.config.ResourcePool;
import org.ehcache.config.ResourceType;
import org.ehcache.config.ResourceUnit;
import org.ehcache.config.SizedResourcePool;
import org.ehcache.core.HumanReadable;
public class SizedResourcePoolImpl<P extends SizedResourcePool> extends AbstractResourcePool<P, ResourceType<P>>
implements SizedResourcePool, HumanReadable {
private final long size;
private final ResourceUnit unit;
public SizedResourcePoolImpl(ResourceType<P> type, long size, ResourceUnit unit, boolean persistent) {
super(type, persistent);
if (unit == null) {
throw new NullPointerException("ResourceUnit can not be null");
}
if (size <= 0) {
throw new IllegalArgumentException("Size must be greater than 0");
}
if (!type.isPersistable() && persistent) {
throw new IllegalStateException("Non-persistable resource cannot be configured persistent");
}
this.size = size;
this.unit = unit;
}
@Override
public long getSize() {
return size;
}
@Override
public ResourceUnit getUnit() {
return unit;
}
@Override
public void validateUpdate(final ResourcePool newPool) {
super.validateUpdate(newPool);
SizedResourcePool sizedPool = (SizedResourcePool)newPool;
if (!this.getUnit().getClass().equals(sizedPool.getUnit().getClass())) {
throw new IllegalArgumentException("ResourcePool for " + sizedPool.getType() + " with ResourceUnit '"
+ sizedPool.getUnit() + "' can not replace '" + this.getUnit() + "'");
}
if (sizedPool.getSize() <= 0) {
throw new IllegalArgumentException("ResourcePool for " + sizedPool.getType()
+ " must specify space greater than 0");
}
}
@Override
public String toString() {
return "Pool {" + getSize() + " " + getUnit() + " " + getType() + (isPersistent() ? "(persistent)}" : "}");
}
@Override
public String readableString() {
return getSize() + " " + getUnit() + " " + (isPersistent() ? "(persistent)" : "");
}
}