001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.db4ounit.common.reflect.custom;
022:
023: import com.db4o.foundation.*;
024: import com.db4o.reflect.*;
025:
026: public class CustomClassRepository {
027:
028: // fields must be public so test works on less capable runtimes
029: public Hashtable4 _classes;
030: public transient CustomReflector _reflector;
031:
032: public CustomClassRepository() {
033: _classes = new Hashtable4();
034: }
035:
036: public CustomClass forName(String className) {
037: return (CustomClass) _classes.get(className);
038: }
039:
040: public CustomClass defineClass(String className,
041: String[] fieldNames, String[] fieldTypes) {
042:
043: assertNotDefined(className);
044: CustomClass klass = createClass(className, fieldNames,
045: fieldTypes);
046: return defineClass(klass);
047: }
048:
049: private CustomClass createClass(String className,
050: String[] fieldNames, String[] fieldTypes) {
051: return new CustomClass(this , className, fieldNames,
052: resolveTypes(fieldTypes));
053: }
054:
055: private Class[] resolveTypes(String[] typeNames) {
056: Class[] types = new Class[typeNames.length];
057: for (int i = 0; i < types.length; ++i) {
058: types[i] = resolveType(typeNames[i]);
059: }
060: return types;
061: }
062:
063: private Class resolveType(String typeName) {
064: if (typeName.equals("string")) {
065: return java.lang.String.class;
066: }
067: if (typeName.equals("int")) {
068: return java.lang.Integer.class;
069: }
070: throw new IllegalArgumentException("Invalid type '" + typeName
071: + "'");
072: }
073:
074: private CustomClass defineClass(CustomClass klass) {
075: _classes.put(klass.getName(), klass);
076: return klass;
077: }
078:
079: private void assertNotDefined(String className) {
080: if (_classes.containsKey(className)) {
081: throw new IllegalArgumentException("Class '" + className
082: + "' already defined.");
083: }
084: }
085:
086: public void initialize(CustomReflector reflector) {
087: _reflector = reflector;
088: }
089:
090: public ReflectClass forFieldType(Class type) {
091: return _reflector.forFieldType(type);
092: }
093:
094: public String toString() {
095: return "CustomClassRepository(classes: " + _classes.size()
096: + ")";
097: }
098:
099: public Iterator4 iterator() {
100: return _classes.values();
101: }
102: }
|