001: /*
002: Copyright (C) 2007 Mobixess Inc. http://www.java-objects-database.com
003:
004: This file is part of the JODB (Java Objects Database) open source project.
005:
006: JODB is free software; you can redistribute it and/or modify it under
007: the terms of version 2 of the GNU General Public License as published
008: by the Free Software Foundation.
009:
010: JODB is distributed in the hope that it will be useful, but WITHOUT ANY
011: WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
013: for more details.
014:
015: You should have received a copy of the GNU General Public License along
016: with this program; if not, write to the Free Software Foundation, Inc.,
017: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: */
019: package com.mobixess.jodb.core.plugin.basePlugins;
020:
021: import java.io.IOException;
022: import java.io.UnsupportedEncodingException;
023: import java.lang.reflect.Field;
024:
025: import com.mobixess.jodb.core.JodbIOException;
026: import com.mobixess.jodb.core.JODBConstants.COMPARE_RESULT;
027: import com.mobixess.jodb.core.io.JODBOperationContext;
028: import com.mobixess.jodb.core.io.ObjectDataContainer;
029: import com.mobixess.jodb.core.io.ObjectDataContainer.FieldRecord;
030: import com.mobixess.jodb.core.io.ObjectDataContainer.FieldsIterator;
031: import com.mobixess.jodb.core.plugin.IClassProcessor;
032: import com.mobixess.jodb.core.transaction.JODBSession;
033:
034: public class StringProcessor implements IClassProcessor<String, byte[]> {
035:
036: public boolean equals(byte[] value1, ObjectDataContainer value2,
037: JODBOperationContext context, Field[] fieldsToIgnore)
038: throws IOException {
039: if (!value2.isArray()) {
040: return false;
041: }
042: FieldsIterator fieldsIterator = value2
043: .getActiveFieldsIterator();
044: if (fieldsIterator == null) {
045: return false;
046: }
047: if (fieldsIterator.getRemainingInCurrentCategory() != value1.length) {
048: return false;
049: }
050: FieldRecord record = value2.getRecordCache();
051: for (int i = 0; i < value1.length && fieldsIterator.hasNext(); i++) {
052: fieldsIterator.next(record, context.getBase(), false);
053: if (record._primitiveRawDataBuffer.get(0) != value1[i]) {
054: return false;
055: }
056: }
057: return true;
058: }
059:
060: private COMPARE_RESULT convertCompareResult(int result) {
061: if (result < 0) {
062: return COMPARE_RESULT.SMALLER;
063: } else if (result > 0) {
064: return COMPARE_RESULT.GREATER;
065: } else {
066: return COMPARE_RESULT.EQUAL;
067: }
068: }
069:
070: public void activate(String incompleteInstance,
071: ObjectDataContainer persistentDataContainer,
072: JODBSession session, int activationDepth,
073: boolean delayedActivation) {
074: //Strings complete uppon activation
075: }
076:
077: public String composeInstance(Class type,
078: ObjectDataContainer persistentDataContainer,
079: JODBSession session) throws IOException {
080: if (!persistentDataContainer.isArray()) {
081: throw new IOException();
082: }
083: FieldsIterator fieldsIterator = persistentDataContainer
084: .getActiveFieldsIterator();
085: if (fieldsIterator == null) {
086: return null;
087: }
088: byte[] result = new byte[fieldsIterator
089: .getRemainingInCurrentCategory()];
090: FieldRecord record = persistentDataContainer.getRecordCache();
091: for (int i = 0; i < result.length && fieldsIterator.hasNext(); i++) {
092: fieldsIterator.next(record, session.getBase(), false);
093: result[i] = record._primitiveRawDataBuffer.get(0);
094: }
095: return new String(result, "UTF8");
096: }
097:
098: public byte[] translate(String objectToTranslate)
099: throws JodbIOException {
100: byte[] result;
101: try {
102: result = objectToTranslate.getBytes("UTF8");
103: } catch (UnsupportedEncodingException e) {
104: e.printStackTrace();
105: throw new JodbIOException(e);
106: }
107: return result;
108: }
109:
110: public COMPARE_RESULT compare(String value1, String value2,
111: JODBOperationContext context, Field[] fieldsToIgnore) {
112: return convertCompareResult(value1.compareTo(value2));
113: }
114:
115: public COMPARE_RESULT compare(byte[] value1,
116: ObjectDataContainer value2, JODBOperationContext context,
117: Field[] fieldsToIgnore) throws IOException {
118: //TODO optimize without instantiation
119: String val1 = new String(value1, "utf8");
120: String val2 = composeInstance(null, value2, context
121: .getSession());
122: return convertCompareResult(val1.compareTo(val2));
123: }
124:
125: public boolean equals(String value1, String value2,
126: JODBOperationContext context, Field[] fieldsToIgnore) {
127: return value1.equals(value2);
128: }
129:
130: public Class<String> getType() {
131: return String.class;
132: }
133:
134: }
|