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

org.emfjson.gwt.common.AsyncIterator Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2015 Guillaume Hillairet.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Guillaume Hillairet - initial API and implementation
 *
 */
package org.emfjson.gwt.common;

import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONValue;
import org.eclipse.emf.common.util.Callback;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.resource.Resource;

import org.emfjson.gwt.map.JsonReader;

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

import static org.emfjson.common.EObjects.setOrAdd;

public class AsyncIterator {

	final EObject object;
	final Iterator it;
	final Map map;
	final Callback done;
	final JsonReader reader;

	public AsyncIterator(EObject object,
	                     Map map,
	                     Callback done,
	                     JsonReader reader) {

		this.object = object;
		this.it = map.keySet().iterator();
		this.map = map;
		this.done = done;
		this.reader = reader;
	}

	public static void forEach(EObject object,
	                           Map map,
	                           Callback done,
	                           JsonReader reader) {

		new AsyncIterator(object, map, done, reader).forEach();
	}

	public static void forEach(
		final Resource resource,
		final int position,
		final JSONArray array,
		final JsonReader reader,
		final Callback callback) {

		if (position >= array.size()) {

			callback.onSuccess(resource);

		} else {

			final JSONValue current = array.get(position);
			final JSONObject currentObject = current.isObject();

			if (currentObject != null) {

				reader.parseObject(currentObject, null, new Callback() {
					@Override
					public void onFailure(Throwable caught) {
						callback.onFailure(caught);
					}

					@Override
					public void onSuccess(EObject result) {
						if (result != null) {
							resource.getContents().add(result);
						}
						forEach(resource, position + 1, array, reader, callback);
					}
				});

			} else {

				callback.onFailure(new ClassCastException());

			}
		}
	}

	public static void forEach(
		final Resource resource,
		final Iterator it,
		final Callback callback) {

		if (!it.hasNext()) {
			callback.onSuccess(resource);
		} else {
			AsyncReferenceEntry entry = it.next();
			entry.resolve(resource, new Callback() {
				@Override
				public void onFailure(Throwable caught) {
					callback.onFailure(caught);
				}

				@Override
				public void onSuccess(Resource result) {
					forEach(resource, it, callback);
				}
			});
		}
	}

	private void forEach() {
		if (it.hasNext()) {
			final EReference reference = it.next();
			final JSONValue value = map.get(reference);
			final JSONArray array = value.isArray();

			if (array != null) {

				forEach(0, array, reference);

			} else {

				JSONObject obj = value.isObject();
				if (obj == null) {

					done.onFailure(new ClassCastException());

				} else {

					reader.parseObject(value.isObject(), reference, new Callback() {
						@Override
						public void onFailure(Throwable caught) {
							done.onFailure(caught);
						}

						@Override
						public void onSuccess(EObject result) {
							setOrAdd(object, reference, result);
							forEach();
						}
					});

				}

			}

		} else {

			done.onSuccess(object);

		}
	}

	private void forEach(final int pos, final JSONArray array, final EReference reference) {
		if (pos >= array.size()) {

			forEach();

		} else {

			JSONValue current = array.get(pos);
			JSONObject obj = current.isObject();
			if (obj == null) {

				done.onFailure(new ClassCastException());

			} else {

				reader.parseObject(obj, reference, new Callback() {
					@Override
					public void onFailure(Throwable caught) {
						done.onFailure(caught);
					}

					@Override
					public void onSuccess(EObject result) {
						setOrAdd(object, reference, result);
						forEach(pos + 1, array, reference);
					}
				});

			}

		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy