objectdbimpl.java
来自「SRI international 发布的OAA框架软件」· Java 代码 · 共 131 行
JAVA
131 行
/* $Id: ObjectDBImpl.java,v 1.3 2003/09/30 01:44:44 giuli Exp $*/
/**************************************************************************
* Copyright 2001, 2002 SRI International. All rights reserved.
*
* The material contained in this file is confidential and proprietary to SRI
* International and may not be reproduced, published, or disclosed to others
* without authorization from SRI International.
*
* DISCLAIMER OF WARRANTIES
*
* SRI International MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
* SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SRI International SHALL NOT BE
* LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
* OR DISTRIBUTING THIS SOFTWARE
**************************************************************************/
package com.sri.sedc.javanetbridge;
import java.util.List;
import java.util.ArrayList;
/**
* Default implementation of ObjectDB.
*/
class ObjectDBImpl implements ObjectDB {
// Note: Maps cannot be used here, because they rely on equals and hashCode.
// Also, indexOf is not used in the lists for the same reason. Object references
// in this database are stored per object, and an id is not valid for more than
// one object.
private List objects = new ArrayList(100);
private List ids = new ArrayList(100);
private List refCounts = new ArrayList(100);
private volatile int objIdCounter = 0;
public ObjectDBImpl() {
}
public synchronized int addObjRef(Object obj) {
int indx = getObjIndx(obj);
int id = -1;
if (indx == -1) {
// Add the object
objIdCounter++;
objects.add(obj);
ids.add(new Integer(id = objIdCounter));
refCounts.add(new Integer(1));
} else {
int refCount = ((Integer)refCounts.get(indx)).intValue();
refCounts.set(indx, new Integer(refCount + 1));
id = ((Integer)ids.get(indx)).intValue();
}
return id;
}
public synchronized void clear() {
objects.clear();
ids.clear();
refCounts.clear();
objIdCounter = 0;
}
public synchronized List getObjects() {
List ret = new ArrayList(objects.size());
ret.addAll(objects);
return ret;
}
public synchronized int releaseObjRef(Object obj) {
return releaseObjRefImpl(getObjIndx(obj));
}
private int releaseObjRefImpl(int indx) {
if (indx >= 0) {
int refCount = ((Integer)refCounts.get(indx)).intValue();
if (refCount == 1) {
// delete the object
objects.remove(indx);
ids.remove(indx);
refCounts.remove(indx);
return 0;
} else {
// decrement the reference count
refCount--;
refCounts.set(indx, new Integer(refCount));
return refCount;
}
}
return -1;
}
public synchronized int releaseObjRef(int id) {
return releaseObjRefImpl(getObjIndx(id));
}
public synchronized Object getObjFromId(int id) throws ObjectNotFoundException {
int indx = getObjIndx(id);
if (indx == -1) {
throw new ObjectNotFoundException("ObjectDB: No object entry exists for id " + id);
}
return objects.get(indx);
}
public synchronized int getIdFromObj(Object obj) throws ObjectNotFoundException {
int indx = getObjIndx(obj);
if (indx == -1) {
throw new ObjectNotFoundException("ObjectDB:getIdFromObj: No object entry exists");
}
return ((Integer)ids.get(indx)).intValue();
}
private int getObjIndx(Object obj) {
for (int i = 0; i < objects.size(); i++) {
if (obj == objects.get(i)) {
return i;
}
}
return -1;
}
private int getObjIndx(int id) {
for (int i = 0; i < ids.size(); i++) {
if (id == ((Integer)ids.get(i)).intValue()) {
return i;
}
}
return -1;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?