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

iot.jcypher.domain.mapping.CompoundObjectMapping Maven / Gradle / Ivy

Go to download

Provides seamlessly integrated Java access to graph databases (Neo4J) at different levels of abstraction.

The newest version!
package iot.jcypher.domain.mapping;

import iot.jcypher.domain.mapping.surrogate.InnerClassSurrogate;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;

public class CompoundObjectMapping extends ObjectMapping {

	private CompoundObjectType compoundObjectType;
	private Map, ObjectMapping> typeMappings;
	private HashSet fieldsToAccept;
	
	public CompoundObjectMapping(CompoundObjectType compoundObjectType,
			Map, ObjectMapping> typeMappings, Object toAccept) {
		super();
		this.compoundObjectType = compoundObjectType;
		this.typeMappings = typeMappings;
		if (toAccept != null) {
			Class toAcc = toAccept.getClass();
			if (toAccept instanceof InnerClassSurrogate)
				toAcc = ((InnerClassSurrogate)toAccept).getRealClass();
			this.fieldsToAccept = new HashSet();
			Iterator it = typeMappings.get(toAcc).fieldMappingsIterator();
			while(it.hasNext()) {
				this.fieldsToAccept.add(it.next());
			}
		} else
			this.fieldsToAccept = null;
	}

	@Override
	public Iterator fieldMappingsIterator() {
		return new FieldMappingIterator();
	}

	@Override
	public boolean shouldPerformFieldMapping(FieldMapping fieldMapping) {
		return this.fieldsToAccept == null || this.fieldsToAccept.contains(fieldMapping);
	}

	@Override
	public FieldMapping getFieldMappingForField(String fieldName) {
		throw new RuntimeException("not suppported");
	}

	/****************************************/
	public class FieldMappingIterator implements Iterator {

		private HashSet fieldMappingsSet = new HashSet();
		private Iterator typeIterator = compoundObjectType.typeIterator();
		private Iterator currentTypeFieldIterator;
		private FieldMapping nextFieldMapping;
		
		@Override
		public boolean hasNext() {
			this.nextFieldMapping = null;
			if (this.currentTypeFieldIterator == null)
				switchToNextType();
			if (this.currentTypeFieldIterator != null) { // else the end is reached
				while (this.currentTypeFieldIterator.hasNext()) {
					FieldMapping next = this.currentTypeFieldIterator.next();
					if (this.fieldMappingsSet.add(next)) { // this fieldMapping was not returned until now
						this.nextFieldMapping = next;
						return true;
					}
				}
				// reached the end of the currentTypeFieldIterator
				// recursively continue with the next one
				this.currentTypeFieldIterator = null;
				return hasNext();
			}
			return false;
		}

		@Override
		public FieldMapping next() {
			return this.nextFieldMapping;
		}

		@Override
		public void remove() {
			throw new RuntimeException("operation not supported");
		}
		
		private void switchToNextType() {
			while (this.typeIterator.hasNext()) {
				CompoundObjectType cType = this.typeIterator.next();
				Class typ = cType.getType();
				ObjectMapping om = typeMappings.get(typ);
				if (om != null) { // may be null if the type is a supertype or interface, which itself has never
										   // been stored to the graph
					this.currentTypeFieldIterator = om.fieldMappingsIterator();
					return;
				}
			}
			
			// we hav reached the end
			this.currentTypeFieldIterator = null;
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy