/*
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.lang.reflect.Field;
import java.nio.ByteBuffer;
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;
import com.mobixess.jodb.util.PrimitiveJavaTypesUtil;
import com.mobixess.jodb.util.PrimitiveJavaTypesUtil.PRIMITIVES_ENUMERATION;
public class PrimitiveWrappersProcessor implements IClassProcessor<Comparable, byte[]>{
private static ThreadLocal<ByteBuffer> _threadLocalBuffer = new ThreadLocal<ByteBuffer>(){
@Override
protected ByteBuffer initialValue()
{
return ByteBuffer.allocate(16);//TODO use global constant for size
}
};
public void activate(Comparable incompleteInstance, ObjectDataContainer persistentDataContainer, JODBSession session,
int activationDepth, boolean delayedActivation) throws IOException
{
}
public COMPARE_RESULT compare(Comparable value1, Comparable 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
PRIMITIVES_ENUMERATION enumeratedType = PrimitiveJavaTypesUtil.getEnumeratedType(originalClass.getName());
//TODO pass enumeratedType directly to 'composeInstance'
ByteBuffer buffer = _threadLocalBuffer.get();
buffer.clear();
buffer.put(value1);
buffer.flip();
Comparable val1 = (Comparable) PrimitiveJavaTypesUtil.getAsWrappedPrimitive(enumeratedType, buffer);
Comparable 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;
}
}
@SuppressWarnings("unchecked")
public Comparable composeInstance(Class type, ObjectDataContainer persistentDataContainer, JODBSession session)
throws IOException
{
if(!persistentDataContainer.isArray()){
throw new IOException();
}
FieldsIterator fieldsIterator = persistentDataContainer.getActiveFieldsIterator();
if(fieldsIterator==null){
return null;
}
ByteBuffer byteBuffer = _threadLocalBuffer.get();
byteBuffer.clear();
FieldRecord record = persistentDataContainer.getRecordCache();
for (int i = 0; fieldsIterator.hasNext(); ++i) {
fieldsIterator.next(record, session.getBase(), false);
byteBuffer.put(record._primitiveRawDataBuffer.get(0));
}
byteBuffer.flip();
PRIMITIVES_ENUMERATION enumeratedType = PrimitiveJavaTypesUtil.getEnumeratedType(type.getName());
return (Comparable) PrimitiveJavaTypesUtil.getAsWrappedPrimitive(enumeratedType, byteBuffer);
}
public boolean equals(Comparable value1, Comparable 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;
}
@SuppressWarnings("unchecked")
public byte[] translate(Comparable objectToTranslate) throws IOException {
ByteBuffer buffer = _threadLocalBuffer.get();
buffer.clear();
PrimitiveJavaTypesUtil.primitiveWrapperToByteBuffer(objectToTranslate, buffer);
buffer.flip();
byte result[] = new byte[buffer.remaining()];
buffer.get(result);
return result;
}
public Class<Comparable> getType() {
return Comparable.class;
}
public boolean acceptType(Class typeOfObjectToProcess) {
return PrimitiveJavaTypesUtil.isPrimitiveWrapper(typeOfObjectToProcess);
}
}