All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.orientechnologies.orient.object.db.OLazyObjectSet Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2010-2012 Luca Garulli (l.garulli--at--orientechnologies.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.orientechnologies.orient.object.db;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import com.orientechnologies.orient.core.db.ODatabaseRecordThreadLocal;
import com.orientechnologies.orient.core.db.object.OLazyObjectMultivalueElement;
import com.orientechnologies.orient.core.db.object.OLazyObjectSetInterface;
import com.orientechnologies.orient.core.db.record.ODatabaseRecord;
import com.orientechnologies.orient.core.id.ORID;
import com.orientechnologies.orient.core.record.ORecord;
import com.orientechnologies.orient.core.record.ORecordInternal;
import com.orientechnologies.orient.core.record.impl.ODocument;

/**
 * Lazy implementation of Set. It's bound to a source ORecord object to keep track of changes. This avoid to call the makeDirty() by
 * hand when the set is changed.
 * 
 * Deprecated use {@link OObjectLazySet} instead
 * 
 * @author Luca Garulli (l.garulli--at--orientechnologies.com)
 * 
 */
@Deprecated
@SuppressWarnings("unchecked")
public class OLazyObjectSet implements OLazyObjectSetInterface, OLazyObjectMultivalueElement, Serializable {
	private static final long					serialVersionUID	= -2497274705163041241L;

	private final ORecord					sourceRecord;
	private final Collection	underlying;
	private String										fetchPlan;
	private boolean										converted					= false;
	private boolean										convertToRecord		= true;

	public OLazyObjectSet(final ORecord iSourceRecord, final Collection iSource) {
		this.sourceRecord = iSourceRecord;
		this.underlying = iSource;
	}

	public Iterator iterator() {
		return (Iterator) new OLazyObjectIterator(getDatabase(), sourceRecord, underlying.iterator(), convertToRecord);
	}

	public int size() {
		return underlying.size();
	}

	public boolean isEmpty() {
		return underlying.isEmpty();
	}

	public boolean contains(final Object o) {
		return underlying.contains(getDatabase().getRecordByUserObject(o, false));
	}

	public Object[] toArray() {
		return toArray(new Object[size()]);
	}

	public  T[] toArray(final T[] a) {
		underlying.toArray(a);
		final ODatabasePojoAbstract database = getDatabase();
		for (int i = 0; i < a.length; ++i)
			a[i] = (T) database.getUserObjectByRecord((ORecordInternal) a[i], fetchPlan);
		return a;
	}

	public boolean add(final Object e) {
		if (converted && e instanceof ORID)
			converted = false;
		setDirty();
		return underlying.add(getDatabase().getRecordByUserObject(e, true));
	}

	public boolean remove(final Object o) {
		setDirty();
		return underlying.remove(getDatabase().getRecordByUserObject(o, false));
	}

	public boolean containsAll(final Collection c) {
		final ODatabasePojoAbstract database = getDatabase();
		for (Object o : c)
			if (!underlying.contains(database.getRecordByUserObject(o, false)))
				return false;

		return true;
	}

	public boolean addAll(final Collection c) {
		boolean modified = false;
		setDirty();
		final ODatabasePojoAbstract database = getDatabase();
		for (Object o : c)
			if (!underlying.add(database.getRecordByUserObject(o, false)))
				modified = true;
		return modified;
	}

	public boolean retainAll(final Collection c) {
		setDirty();
		return underlying.retainAll(c);
	}

	public void clear() {
		setDirty();
		underlying.clear();
	}

	public boolean removeAll(final Collection c) {
		setDirty();
		boolean modified = false;
		final ODatabasePojoAbstract database = getDatabase();
		for (Object o : c)
			if (!underlying.remove(database.getRecordByUserObject(o, false)))
				modified = true;
		return modified;
	}

	public String getFetchPlan() {
		return fetchPlan;
	}

	public OLazyObjectSet setFetchPlan(String fetchPlan) {
		this.fetchPlan = fetchPlan;
		return this;
	}

	public boolean isConvertToRecord() {
		return convertToRecord;
	}

	public void setConvertToRecord(boolean convertToRecord) {
		this.convertToRecord = convertToRecord;
	}

	@Override
	public String toString() {
		return underlying.toString();
	}

	public void setDirty() {
		if (sourceRecord != null)
			sourceRecord.setDirty();
	}

	public void detach() {
		convertAll();
	}

	public boolean isConverted() {
		return converted;
	}

	protected void convertAll() {
		if (converted || !convertToRecord)
			return;

		final Set copy = new HashSet(this);
		underlying.clear();
		final ODatabasePojoAbstract database = getDatabase();
		for (Object e : copy) {
			if (e != null) {
				if (e instanceof ORID)
					add(database.getUserObjectByRecord((ORecordInternal) ((ODatabaseRecord) getDatabase().getUnderlying()).load((ORID) e, fetchPlan), fetchPlan));
				else if (e instanceof ODocument)
					add(database.getUserObjectByRecord((ORecordInternal) e, fetchPlan));
				else
					add(e);
			}
		}

		converted = true;
	}

	protected ODatabasePojoAbstract getDatabase() {
		return (ODatabasePojoAbstract) ODatabaseRecordThreadLocal.INSTANCE.get().getDatabaseOwner();
	}
}