⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 memorymanager.java

📁 著名IT公司ILog的APS高级排产优化引擎
💻 JAVA
字号:
package com.power.lpsolver.LPSolve;

import java.util.*;
/**
 *
 * <p>Title: PIPE Engine</p>
 * <p>Description: Global Planning Optimization Engine</p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: Paraster, Inc.</p>
 * @author Wei Tan
 * @version 1.0
 */

/**
 * MemoryManager is a central memory manager that manages the initialization
 * and recycle of frequently used objects, such as integer strings for Hashtable
 * keys, elements in a constraint, etc.
 * It is a singular class.
 */
public class MemoryManager
{
    private static final MemoryManager INSTANCE =
                              new MemoryManager();

    /**
     * Sole class constructor.
     */
    private MemoryManager( ) {
		//_integerStrings.setSize( 10000 );
		//_integers.setSize( 10000 );
        allocIntegerBlock();
        allocElemBlock();
    }

    public static MemoryManager getInstance( ) {
        return INSTANCE;
    }

    /**
     * a vector of recycable elements.
     */
	private Vector _elements = new Vector( );

    /**
     * a vector of recycable string representation of integers in sequential
     * order, i.e., _integerStrings[k] = k.
     */
	private Vector _integerStrings = new Vector( );

    /**
     * a vector of integer objects that matches the elements in _integerStrings.
     * Also in sequential order: _integers[k] = k.
     */
	private Vector _integers = new Vector();


    /**
     * Adds an element to the _elements vector.
     * @param elem the element to be added.
     */
	public void addElement( Element elem ) {
		_elements.addElement( elem );
	}

    /**
     * Gets an element. If none available, create a new one, otherwise, returns
     * the first element in the vector _elements and remove it from the vector.
     * @return an element.
     */
	public Element getElement() {
		if( _elements.size() == 0 ) {
            allocElemBlock();
		}

		Element elem = (Element)_elements.remove( 0 );
		return elem;
	}

    private void allocElemBlock() {
        for( int i=0; i<5000; i++ ) {
            _elements.add( new Element( -1, 0 ) );
        }
    }

    private void allocIntegerBlock() {
        for( int i=0; i<5000; i++ ) {
            Integer anInteger = new Integer( i );
            _integers.add( anInteger );
            _integerStrings.add( anInteger.toString() );
        }
    }

    /**
     * Gets the string representation of a given int value. If none available,
     * create one and increase the size of _integerStrings to the given num +
     * 100. Also makes sure that the _integers consistent with _integerStrings.
     * @param num the number for which its string value is sought.
     * @return the string value of num.
     */
	public String getIntegerString( int num ) {
		if( _integerStrings.size() <= num ) {
            for( int i= _integerStrings.size(); i<num+1000; i++ ) {
                Integer anInteger = new Integer( i );
                _integers.add( anInteger );
                _integerStrings.add( anInteger.toString());
            }
		}

		/*if( null == _integerStrings.elementAt( num ) ) {
			Integer anInt = new Integer( num );
			_integerStrings.insertElementAt( anInt.toString(), num );
			_integers.insertElementAt( anInt, num );
		}*/

		return (String) _integerStrings.elementAt( num );
	}

    /**
     * Gets the Integer representation of a given int value. If none available,
     * create one and increase the size of _integers to the given num +
     * 100. Also makes sure that the _integerStrings consistent with _integers.
     * @param num the number for which its Integer value is sought.
     * @return the Integer value of num.
     */
	public Integer getInteger( int num ) {
		if( _integers.size() <= num ) {
            for( int i= _integerStrings.size(); i<num+1000; i++ ) {
                Integer anInteger = new Integer( i );
                _integers.add( anInteger );
                _integerStrings.add( anInteger.toString());
            }
		}

		/*if( null == _integers.elementAt( num ) ) {
			Integer anInt = new Integer( num );
			_integerStrings.insertElementAt( anInt.toString(), num );
			_integers.insertElementAt( anInt, num );
		}*/

		return (Integer) _integers.elementAt( num );
	}

    public void reset() {
        _integers = null;
        _integerStrings = null;
        _elements = null;
        System.gc();
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -