Package com.mobixess.jodb.core.plugin

Source Code of com.mobixess.jodb.core.plugin.JODBPluginRegistry

/*
Copyright (C) 2007  Mobixess Inc. http://www.java-objects-database.com

This file is part of the JODB (Java Objects Database) open source project.

JODB is free software; you can redistribute it and/or modify it under
the terms of version 2 of the GNU General Public License as published
by the Free Software Foundation.

JODB is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package com.mobixess.jodb.core.plugin;

import java.util.HashMap;
import java.util.Vector;

import com.mobixess.jodb.core.plugin.basePlugins.EnumProcessor;
import com.mobixess.jodb.core.plugin.basePlugins.PrimitiveWrappersProcessor;
import com.mobixess.jodb.core.plugin.basePlugins.StringProcessor;
import com.mobixess.jodb.core.transaction.BaseProcessor;

@SuppressWarnings("unchecked")
public class JODBPluginRegistry {
    private static JODBPluginRegistry _instance = new JODBPluginRegistry();
    private final static int PROCESSORS_CONTAINER_INC = 10;

    private HashMap<Class, IClassProcessor> _processorsForExactMatches = new HashMap<Class, IClassProcessor>();
    private Vector<IClassProcessor> _instanceOfProcessors = new Vector<IClassProcessor>();
    private BaseProcessor _baseProcessor = new BaseProcessor();
    private IClassProcessor[] _processors = new IClassProcessor[PROCESSORS_CONTAINER_INC];
    private int _totalProcessors;
   
    private JODBPluginRegistry() {
        register(new StringProcessor());
        register(new EnumProcessor());
        register(new PrimitiveWrappersProcessor());
    }
   
    public static JODBPluginRegistry getInstance() {
        return _instance;
    }
   
    public void register(IClassProcessor processor){
        if(processor == null){
            throw new IllegalArgumentException("Can't accept null");
        }
        synchronized (_processors) {
            for (int i = _totalProcessors - 1; i >= 0 ; --i) {
                if(_processors[i] == processor){
                    throw new IllegalArgumentException();
                }
            }
            ensureCapacity(_totalProcessors+1);
            _processors[_totalProcessors++] = processor;
        }
       
//        int usage = processor.getUsageCondition();
//        if((usage & IClassProcessor.USAGE_CONDITION_EXACT_CLASS)!=0){
//            _processorsForExactMatches.put(processor.getType(), processor);
//        }
//        if((usage & IClassProcessor.USAGE_CONDITION_INSTANCE_OF_CLASS)!=0){
//            if(_instanceOfProcessors.indexOf(processor)==-1){
//                _instanceOfProcessors.add(processor);
//            }
//        }
    }
   
    private void ensureCapacity(int capacity){
        if(capacity > _processors.length){
            IClassProcessor[] newContainer = new IClassProcessor[capacity+PROCESSORS_CONTAINER_INC];
            System.arraycopy(_processors, 0, newContainer, 0, _processors.length);
            _processors = newContainer;
        }
    }
   
    public void unregister(IClassProcessor processor){
        synchronized (_processors) {
            for (int i = _totalProcessors - 1; i >= 0 ; --i) {
                if(_processors[i] == processor){
                    _processors[i] = null;
                    _totalProcessors--;
                    System.arraycopy(_processors, i+1, _processors, i, _totalProcessors-i);
                    return;
                }
            }
        }
//        _processorsForExactMatches.remove(clazz);
//        for (int i = _instanceOfProcessors.size()-1; i >= 0; --i) {
//            if(_instanceOfProcessors.get(i).getType() == clazz){
//                _instanceOfProcessors.remove(i);
//                return;
//            }
//        }
    }
   
    public IClassProcessor getClassProcessor(Class clazz){
        synchronized (_processors) {
            for (int i = _totalProcessors - 1; i >= 0 ; --i) {
                if(_processors[i].acceptType(clazz)){
                    return _processors[i];
                }
            }
        }
//        IClassProcessor classProcessor = _processorsForExactMatches.get(clazz);
//        if(classProcessor == null){
//            for (int i = _instanceOfProcessors.size()-1; i >= 0; --i) {
//                Class nextType = _instanceOfProcessors.get(i).getType();
//                if(nextType == clazz || nextType.isAssignableFrom(clazz)){
//                    return _instanceOfProcessors.get(i);                   
//                }
//            }
//        }else {
//            return classProcessor;
//        }
        return _baseProcessor;
    }
}
TOP

Related Classes of com.mobixess.jodb.core.plugin.JODBPluginRegistry

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.