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

org.emfjson.jackson.utils.EObjects Maven / Gradle / Ivy

There is a newer version: 1.3.0
Show 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.jackson.utils;

import com.fasterxml.jackson.databind.DatabindContext;
import org.eclipse.emf.ecore.*;
import org.eclipse.emf.ecore.impl.DynamicEObjectImpl.BasicEMapEntry;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.emfjson.jackson.databind.EMFContext;

import java.util.Collection;

/**
 * Utility class to facilitate access or modification of eObjects.
 */
public class EObjects {

	/**
	 * Set or add a value to an object reference. The value must be
	 * an EObject.
	 *
	 * @param owner
	 * @param reference
	 * @param value
	 */
	public static void setOrAdd(EObject owner, EReference reference, Object value) {
		if (value != null) {
			if (reference.isMany()) {
				@SuppressWarnings("unchecked")
				Collection values = (Collection) owner.eGet(reference, false);
				if (values != null && value instanceof EObject) {
					values.add((EObject) value);
				}
			} else {
				owner.eSet(reference, value);
			}
		}
	}

	/**
	 * Checks that the contained object is in a different resource than it's owner, making
	 * it a contained proxy.
	 *
	 * @param owner
	 * @param contained
	 * @return true if proxy
	 */
	public static boolean isContainmentProxy(DatabindContext ctxt, EObject owner, EObject contained) {
		if (contained.eIsProxy())
			return true;

		Resource ownerResource = EMFContext.getResource(ctxt, owner);
		Resource containedResource = EMFContext.getResource(ctxt, contained);

		return ownerResource != null && ownerResource != containedResource;
	}

	/**
	 * Creates a map entry of type string, string.
	 *
	 * @param key   of entry
	 * @param value of entry
	 * @return entry
	 */
	@SuppressWarnings("unchecked")
	public static EObject createEntry(String key, Object value, EClass type) {
		if (type == EcorePackage.Literals.ESTRING_TO_STRING_MAP_ENTRY) {

			final EObject entry = EcoreUtil.create(EcorePackage.Literals.ESTRING_TO_STRING_MAP_ENTRY);
			entry.eSet(EcorePackage.Literals.ESTRING_TO_STRING_MAP_ENTRY__KEY, key);
			entry.eSet(EcorePackage.Literals.ESTRING_TO_STRING_MAP_ENTRY__VALUE, value);

			return entry;

		} else {

			final BasicEMapEntry entry = new BasicEMapEntry<>();
			entry.eSetClass(type);
			entry.setKey(key);
			entry.setValue(value);

			return entry;

		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy