package tools.vitruv.framework.util.datatypes;

import static com.google.common.base.Preconditions.checkNotNull;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.resources.IResource;
import org.eclipse.emf.common.CommonPlugin;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;

import tools.vitruv.framework.util.bridges.EMFBridge;

/**
 * Implements the multiton design pattern.
 *
 * @author kramerm
 *
 */
public class VURI implements Comparable<VURI> {
    private static final Map<String, VURI> INSTANCES = new HashMap<String, VURI>();

    private org.eclipse.emf.common.util.URI emfURI;

    /** Multiton classes should not have a public or default constructor. */
    private VURI(final String uriString) {
        this.emfURI = EMFBridge.createURI(uriString);
    }

    public static synchronized VURI getInstance(final String key) {
        VURI instance = INSTANCES.get(key);
        if (instance == null) {
            instance = new VURI(key);
            String newKey = instance.toString();
            VURI oldInstance = INSTANCES.get(newKey);
            if (oldInstance != null) {
                INSTANCES.put(key, oldInstance);
                return oldInstance;
            } else {
                // we also have to map the actual string representation of the key after the vuri
                // was
                // created because a prefix may be prepended to the key while the VURI is created
                INSTANCES.put(newKey, instance);
            }
            INSTANCES.put(key, instance);
        }
        return instance;
    }

    public static VURI getInstance(final Resource resource) {
        return getInstance(resource.getURI());
    }

    public static VURI getInstance(final URI uri) {
        checkNotNull(uri, "URI for VURI");
        if (null == uri.toFileString()) {
            return getInstance(uri.toString());
        }
        return getInstance(uri.toFileString());
    }

    public static VURI getInstance(final IResource iResource) {
        String[] keyStrSegments = iResource.getFullPath().segments();
        StringBuilder keyString = new StringBuilder();
        for (int i = 0; i < keyStrSegments.length; i++) {
            if (i > 0) {
                keyString.append("/");
            }
            keyString.append(keyStrSegments[i]);
        }

        return getInstance(keyString.toString());
    }

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

    public String toResolvedAbsolutePath() {
        return CommonPlugin.resolve(this.emfURI).toFileString();
    }

    public URI getEMFUri() {
        return this.emfURI;
    }

    public String getFileExtension() {
        return this.emfURI.fileExtension();
    }

    public String getLastSegment() {
        String lastSegment = this.emfURI.lastSegment();
        return (lastSegment == null ? "" : lastSegment);
    }

    /**
     * Returns a new VURI that is created from the actual VURI by replacing its file extension with
     * newFileExt
     *
     * @param newFileExt
     * @return the new VURI with the replaced file extension
     */
    public VURI replaceFileExtension(final String newFileExt) {
        return VURI.getInstance(this.emfURI.trimFileExtension().appendFileExtension(newFileExt));
    }

    @Override
    public int compareTo(final VURI otherVURI) {
        return this.emfURI.toString().compareTo(otherVURI.toString());
    }
}

@@ -338,63 +338,8 @@
 e;%0A%0A
-import tools.vitruv.framework.util.bridges.EMFBridge;%0A%0A
 /**%0A
@@ -724,62 +724,684 @@
     
-    this.emfURI = EMFBridge.createURI(uriString);%0A    
+%09if (uriString != null) %7B%0A    %09%09%0A%09%09%09if (!uriString.startsWith(%22platform%22) && !uriString.startsWith(%22pathmap%22)) %7B%0A%09%09%09%09if (startsWithWindowsDriveLetterColonBackslash(uriString) %7C%7C uriString.startsWith(%22/%22)) %7B%0A%09%09%09%09%09emfURI = URI.createFileURI(uriString);%0A%09%09%09%09%09return;%0A%09%09%09%09%7D else if (!uriString.startsWith(%22http://%22)) %7B%0A%09%09%09%09%09emfURI = URI.createPlatformResourceURI(uriString, true);%0A%09%09%09%09%09return;%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D%0A    %09emfURI = URI.createURI(uriString);%0A    %7D%0A    %0A    private static boolean startsWithWindowsDriveLetterColonBackslash(final String uriString) %7B%0A%09%09char firstChar = uriString.charAt(0);%0A%09%09return Character.isLetter(firstChar) && uriString.regionMatches(1, %22:%5C%5C%22, 0, 2);%0A%09
 %7D%0A%0A 

Move URI calculation functionality (temporarily) to VURI