/*
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.query;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.ListIterator;
import java.util.Vector;
import com.mobixess.jodb.core.io.IOTicket;
import com.mobixess.jodb.core.transaction.JODBSession;
import com.mobixess.jodb.core.transaction.TransactionUtils;
import com.mobixess.jodb.query.api.ObjectSet;
import com.mobixess.jodb.util.WrGlobalQueuedResorceCleaner;
import com.mobixess.jodb.util.WrGlobalQueuedResorceCleaner.IWrCleanableResource;
public abstract class JODBQueryList implements ObjectSet, Iterable {
JODBSession _session;
Vector<IOTicket> _ticketCache;
/**
* @param offsets
*/
public JODBQueryList( JODBSession session) {
super();
_session = session;
}
public boolean add(Object o) {
throw new UnsupportedOperationException();
}
public int lastIndexOf(Object o) {
for (int i = size()-1; i >= 0; i--) {
Object next = get(i);
if(o == next || o.equals(next)){
return i;
}
}
return -1;
}
public int indexOf(Object o) {
int size = size();
for (int i = 0; i < size; i++) {
Object next = get(i);
if(next == null && o!=null){
continue;
}
if(o == next || o.equals(next)){
return i;
}
}
return -1;
}
public void add(int index, Object element) {
throw new UnsupportedOperationException();
}
public boolean addAll(Collection c) {
throw new UnsupportedOperationException();
//return false;
}
public boolean addAll(int index, Collection c) {
throw new UnsupportedOperationException();
//return false;
}
public void clear() {
throw new UnsupportedOperationException();
//
}
public boolean contains(Object o) {
throw new UnsupportedOperationException();
//return false;
}
public boolean containsAll(Collection c) {
throw new UnsupportedOperationException();
//return false;
}
public ListIterator listIterator() {
throw new UnsupportedOperationException();
//return null;
}
public ListIterator listIterator(int index) {
throw new UnsupportedOperationException();
//return null;
}
public boolean remove(Object o) {
throw new UnsupportedOperationException();
//return false;
}
public Object remove(int index) {
throw new UnsupportedOperationException();
//return null;
}
public boolean removeAll(Collection c) {
throw new UnsupportedOperationException();
//return false;
}
public boolean retainAll(Collection c) {
throw new UnsupportedOperationException();
//return false;
}
public Object set(int index, Object element) {
throw new UnsupportedOperationException();
//return null;
}
public List subList(int fromIndex, int toIndex) {
throw new UnsupportedOperationException();
//return null;
}
public Object[] toArray() {
throw new UnsupportedOperationException();
//return null;
}
@SuppressWarnings("unchecked")
public Object[] toArray(Object[] a) {
throw new UnsupportedOperationException();
//return null;
}
public void remove() {
throw new UnsupportedOperationException();
}
public long[] getAllObjectIds(){
throw new UnsupportedOperationException();
}
protected Object getObjectForOffset(long offset) throws IOException{
try {
if(_ticketCache == null){
_ticketCache = new TicketCleaner(this);
}
TransactionUtils.getContextTicketCache().setIoTicketsCache(_ticketCache);
return _session.getObjectForOffset(offset);
} finally{
TransactionUtils.getContextTicketCache().resetCache();
}
}
// @Override
// protected void finalize() throws Throwable {
// if(_ticketCache!=null){
// try {
// for (int i = _ticketCache.size()-1; i >= 0; --i) {
// IOTicket ticket = _ticketCache.elementAt(i);
//// if(!ticket.isClosed()){
//// System.err.println();
//// }
// ticket.close();
// }
// } catch (Throwable e) {
// //ignore all
// }
// }
// super.finalize();
// }
//experimental ticket holder to close buffers uppon GC
private static class TicketCleaner extends Vector<IOTicket> implements IWrCleanableResource {
public TicketCleaner(Object owner) {
WrGlobalQueuedResorceCleaner.getInstance().register(owner, this);
}
public void cleanOnEnqueue() {
for (int i = size()-1; i >= 0; --i) {
try {
IOTicket ticket = elementAt(i);
ticket.close();
} catch (IOException e) {
e.printStackTrace();//TODO add log?
}
}
}
}
}