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

org.emfjson.gwt.common.AsyncCache 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 org.eclipse.emf.common.util.Callback;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.ResourceSet;

import org.emfjson.common.Cache;

public class AsyncCache extends Cache {

	public void getEClass(final ResourceSet resourceSet, final String type, final Callback callback) {
		if (type == null) {
			callback.onFailure(new IllegalArgumentException("Cannot get EClass from null identifier."));
		}

		final EClass eClass = mapOfClasses.get(type);
		if (eClass == null) {
			URI uri = mapOfURIs.get(type);
			if (uri == null) {
				uri = URI.createURI(type);
				mapOfURIs.put(type, uri);
			}
			resourceSet.getEObject(uri, new Callback() {
				@Override
				public void onSuccess(EObject result) {
					if (result instanceof EClass) {
						final EClass resultEClass = (EClass) result;
						mapOfClasses.put(type, resultEClass);
						callback.onSuccess(resultEClass);
					} else {
						callback.onFailure(new ClassCastException("Cannot cast object " + result + " as EClass."));
					}
				}

				@Override
				public void onFailure(Throwable caught) {
					callback.onFailure(caught);
				}
			});
		} else {
			callback.onSuccess(eClass);
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy