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

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

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

public class StringProcessor implements IClassProcessor< String, byte[]> {

   

    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;
    }
   
    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 void activate(String incompleteInstance, ObjectDataContainer persistentDataContainer, JODBSession session, int activationDepth, boolean delayedActivation) {
        //Strings complete uppon activation
    }

    public String 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 new String(result,"UTF8");
    }

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

    public COMPARE_RESULT compare(String value1, String 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 val1 = new String(value1,"utf8");
        String val2 = composeInstance(null, value2, context.getSession());
        return convertCompareResult(val1.compareTo(val2));
    }

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

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

    public boolean acceptType(Class typeOfObjectToProcess) {
        return typeOfObjectToProcess == String.class;
    }

 

}
TOP

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

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.