objectpropertyspool.java

来自「创建中间企业对象层」· Java 代码 · 共 91 行

JAVA
91
字号

 
package com.eshaper.appbuilder.objectbase.property;

import java.io.*;
import java.util.*;

public class ObjectPropertysPool
{
	static private ObjectPropertysPool instance; // 唯一实例
	
	/**
	* 返回唯一实例.如果是第一次调用此方法,则创建实例
	*
	* @return DBConnectionManager 唯一实例
	*/
	static synchronized public ObjectPropertysPool getInstance()
	{
		if (instance == null) instance = new ObjectPropertysPool();
		return instance;
	}

	/**
	* 建构函数私有以防止其它对象创建本类实例
	*/
	private ObjectPropertysPool()
	{
	}

	public int getCount()
	{
		return pool.size();
	}
	
	public ObjectPropertys get(String name) throws Exception
	{
		ObjectPropertys propertys = null;
		for(int index=0;index<pool.size();index++)
		{
			try
			{
				propertys = (ObjectPropertys)pool.get(index);
				if(propertys.getObjectName().equalsIgnoreCase(name))
					return propertys;
			}
			catch (Exception e)
			{
				pool.remove(index);
				continue;
			}
		}
		propertys = add(name);
		return propertys;
	}
	
	public ObjectPropertys get(int index)
	{
		if(index<0 || index>=pool.size()) return null;
		try
		{
			return (ObjectPropertys)pool.get(index);
		}
		catch (Exception e)
		{
			pool.remove(index);
		}
		return null;
	}

	private ObjectPropertys add(String name) throws Exception
	{
		String filepath = "D:\\work\\test\\file\\"+name+".tdf";
		ObjectPropertys propertys = new ObjectPropertys();
		propertys.setObjectName(name);
		propertys.loadFromFile(filepath);
		pool.add(propertys);	
		return propertys;
	}

	private ArrayList pool = new ArrayList();
	
	public static void main(String args[]) throws Exception
	{
		System.out.println("Test-------");	
		ObjectPropertysPool pool = new ObjectPropertysPool();
		System.out.println("First count = " + pool.getCount());
		ObjectPropertys fields = pool.get("Users");
		System.out.println("Last count = " + pool.getCount());
		System.out.println("fields's field count = " + fields.getCount());
	}	
}

⌨️ 快捷键说明

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