Package com.mobixess.jodb.core.plugin.basePlugins

Source Code of com.mobixess.jodb.core.plugin.basePlugins.EnumProcessor

/*
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.basePlugins;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;

import com.mobixess.jodb.core.JodbIOException;
import com.mobixess.jodb.core.JODBConstants.COMPARE_RESULT;
import com.mobixess.jodb.core.io.JODBOperationContext;
import com.mobixess.jodb.core.io.ObjectDataContainer;
import com.mobixess.jodb.core.io.ObjectDataContainer.FieldRecord;
import com.mobixess.jodb.core.io.ObjectDataContainer.FieldsIterator;
import com.mobixess.jodb.core.plugin.IClassProcessor;
import com.mobixess.jodb.core.transaction.JODBSession;

@SuppressWarnings("unchecked")
public class EnumProcessor implements IClassProcessor<Enum, byte[]>{

    public void activate(Enum incompleteInstance, ObjectDataContainer persistentDataContainer, JODBSession session,
            int activationDepth, boolean delayedActivation) throws IOException
    {
    }

    public COMPARE_RESULT compare(Enum value1, Enum value2, JODBOperationContext context, Field[] fieldsToIgnore) {
        return  convertCompareResult(value1.compareTo(value2));
    }

    public COMPARE_RESULT compare(byte[] value1, ObjectDataContainer value2, JODBOperationContext context,
            Field[] fieldsToIgnore, Class originalClass) throws IOException
    {
        //TODO optimize without instantiation
        String name = new String(value1,"utf8");
        Enum val1 =  Enum.valueOf(originalClass, name);//new String(value1,"utf8");
        Enum val2 = composeInstance(originalClass, value2, context.getSession());
        return convertCompareResult(val1.compareTo(val2));
    }
   
    private COMPARE_RESULT convertCompareResult(int result){
        if(result < 0){
            return COMPARE_RESULT.SMALLER;
        }else if (result > 0){
            return COMPARE_RESULT.GREATER;
        }else{
            return COMPARE_RESULT.EQUAL;
        }
    }

    public Enum composeInstance(Class type, ObjectDataContainer persistentDataContainer, JODBSession session)
            throws IOException
    {
        if(!persistentDataContainer.isArray()){
            throw new IOException();
        }
        FieldsIterator fieldsIterator = persistentDataContainer.getActiveFieldsIterator();
        if(fieldsIterator==null){
            return null;
        }
        byte[] result = new byte[fieldsIterator.getRemainingInCurrentCategory()];
        FieldRecord record = persistentDataContainer.getRecordCache();
        for (int i = 0; i < result.length && fieldsIterator.hasNext(); i++) {
            fieldsIterator.next(record, session.getBase(), false);
            result[i] = record._primitiveRawDataBuffer.get(0);
        }
        return Enum.valueOf(type, new String(result,"UTF8"));
    }

    public boolean equals(Enum value1, Enum value2, JODBOperationContext context, Field[] fieldsToIgnore) {
        return value1.equals(value2);
    }

    public boolean equals(byte[] value1, ObjectDataContainer value2, JODBOperationContext context,
            Field[] fieldsToIgnore) throws IOException
    {
        if(!value2.isArray()){
            return false;
        }
        FieldsIterator fieldsIterator = value2.getActiveFieldsIterator();
        if(fieldsIterator==null){
            return false;
        }
        if(fieldsIterator.getRemainingInCurrentCategory() != value1.length){
            return false;
        }
        FieldRecord record = value2.getRecordCache();
        for (int i = 0; i < value1.length && fieldsIterator.hasNext(); i++) {
            fieldsIterator.next(record, context.getBase(), false);
            if(record._primitiveRawDataBuffer.get(0)!=value1[i]){
                return false;
            }
        }
        return true;
    }

    public byte[] translate(Enum objectToTranslate) throws IOException {
        String name = objectToTranslate.name();
        byte[] result;
        try {
            result = name.getBytes("UTF8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            throw new JodbIOException(e);
        }
        return result;
    }

    public Class<Enum> getType() {
        return Enum.class;
    }

    public boolean acceptType(Class typeOfObjectToProcess) {
        return Enum.class.isAssignableFrom(typeOfObjectToProcess);
    }

}
TOP

Related Classes of com.mobixess.jodb.core.plugin.basePlugins.EnumProcessor

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.