All Downloads are FREE. Search and download functionalities are using the official Maven repository.

Alachisoft.NCache.Common.Net.ObjectProvider Maven / Gradle / Ivy

There is a newer version: 5.3.3
Show newest version
package Alachisoft.NCache.Common.Net;


//C# TO JAVA CONVERTER TODO TASK: Only the namespaces at the beginning of the file can be converted to the Java 'package' for this file:
//ORIGINAL LINE: namespace Alachisoft.NCache.Common
public abstract class ObjectProvider {
    //C# TO JAVA CONVERTER TODO TASK: Java annotations will not correspond to .NET attributes:
    //[CLSCompliant(false)]
    protected int _initialSize = 30;
    //C# TO JAVA CONVERTER TODO TASK: Java annotations will not correspond to .NET attributes:
    //[CLSCompliant(false)]
    protected java.lang.Class _objectType;
    private java.util.HashMap _available = new java.util.HashMap();
    private java.util.HashMap _rented = new java.util.HashMap();
    private java.util.ArrayList _availableRentIds = new java.util.ArrayList();
    private Object _sync = new Object();
    private int _rentid = 1;

    private java.util.ArrayList list = new java.util.ArrayList();

    public ObjectProvider() {
        Initialize();
    }

    public ObjectProvider(int initialSize) {
        _initialSize = initialSize;
        Initialize();
    }

    public final void Initialize() {
        IRentableObject obj = null;

        synchronized (_sync) {
            for (int i = 0; i < _initialSize; i++) {
                obj = CreateObject();
                if (obj != null) {
                    ResetObject(obj);
                    //obj.RentId = _rentid++;
                    //_availableRentIds.Add(obj.RentId);
                    //_available.Add(obj.RentId,obj);
                    list.add(obj);
                }
            }
        }
    }

    public final IRentableObject RentAnObject() {
        IRentableObject obj = null;
        synchronized (_sync) {
            //lock (_rented.SyncRoot)
            //{
            if (_available.size() > 0) {
                obj = (IRentableObject) _available.get(_availableRentIds.get(0));
                _available.remove(obj.getRentId());
                _availableRentIds.remove(obj.getRentId());
                _rented.put(obj.getRentId(), obj);
                //if (_rented.Count > 10)
                //    Trace.error(Name + ".RentAnObject", "rented " + _rented.Count);
                //Trace.error(Name + ".RentAnObject", "RentId : " + obj.RentId + " Total:" + TotalObjects + " Rented:" + RentCount);
            } else {
                obj = (IRentableObject) CreateObject();
                obj.setRentId(_rentid++);
                if (obj != null) {
                    _rented.put(obj.getRentId(), obj);
                }
                //Trace.error(Name + ".RentAnObject", "new instance; RentId : " + obj.RentId + " Total:" + TotalObjects + " Rented:" + RentCount);
            }
            //}
        }

        return obj;
    }

    public final void SubmittObject(IRentableObject obj) {
        synchronized (_sync) {
            //lock (_rented.SyncRoot)
            {
                if (_rented.containsKey(obj.getRentId())) {
                    _rented.remove(obj.getRentId());
                    ResetObject(obj);
                    _available.put(obj.getRentId(), obj);
                    _availableRentIds.add(obj.getRentId());
                    //Trace.error(Name + ".SubmittObject", " RentId: " + obj.RentId + " Total:" + TotalObjects);
                }
            }
        }
    }

    protected abstract IRentableObject CreateObject();

    protected abstract void ResetObject(Object obj);

    public abstract java.lang.Class getObjectType();

    public abstract String getName();

    public final int getTotalObjects() {
        return _rented.size() + _available.size();
    }

    public final int getAvailableCount() {
        return _available.size();
    }

    public final int getRentCount() {
        return _rented.size();
    }

    public final int getInitialSize() {
        return _initialSize;
    }

    public final void setInitialSize(int value) {
        _initialSize = value;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy