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

com.redhat.ceylon.compiler.typechecker.context.PhasedUnitMap Maven / Gradle / Ivy

There is a newer version: 1.3.3
Show newest version
package com.redhat.ceylon.compiler.typechecker.context;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.redhat.ceylon.compiler.typechecker.io.VirtualFile;
import com.redhat.ceylon.model.typechecker.model.Unit;

public abstract class PhasedUnitMap {

    protected Map phasedUnitPerPath = new LinkedHashMap();
    protected Map relativePathToPath = new HashMap();

    protected abstract StoredType toStoredType(ReturnedType phasedUnit); 
    protected abstract ReturnedType fromStoredType(StoredType storedValue, String path); 
    
    public boolean containsRelativePath(String relativePath) {
        return relativePathToPath.containsKey(relativePath);
    }
    
    public void addPhasedUnit(VirtualFile unitFile, ReturnedType phasedUnit) {
        phasedUnitPerPath.put(unitFile.getPath(), toStoredType(phasedUnit));
        relativePathToPath.put(phasedUnit.getPathRelativeToSrcDir(), unitFile.getPath());
    }

    public void removePhasedUnitForRelativePath(String relativePath) {
        String path = relativePathToPath.get(relativePath);
        ReturnedType phasedUnit = fromStoredType(phasedUnitPerPath.get(path), path);
        if (phasedUnit != null) {
            Unit unit = phasedUnit.getUnit();
            if (unit != null) {
                unit.getPackage().removeUnit(unit);
            }
        }
        relativePathToPath.remove(relativePath);
        phasedUnitPerPath.remove(path);
    }

    protected void addInReturnedList(List list, ReturnedType phasedUnit) {
        list.add(phasedUnit);
    }
    
    public List getPhasedUnits() {
        List list = new ArrayList(phasedUnitPerPath.size());
        for (Entry entry : phasedUnitPerPath.entrySet()) {
            addInReturnedList(list, fromStoredType(entry.getValue(), entry.getKey()));
        }
        return list;
    }

    public ReturnedType getPhasedUnit(VirtualFile file) {
        return getPhasedUnit(file.getPath());
    }

    public ReturnedType getPhasedUnit(String path) {
        StoredType storedValue = phasedUnitPerPath.get(path);
        return storedValue == null ? null : fromStoredType(storedValue, path); 
    }

    public ReturnedType getPhasedUnitFromRelativePath(String relativePath) {
        if (relativePath.startsWith("/")) {
            relativePath = relativePath.substring(1);
        }
        return getPhasedUnit(relativePathToPath.get(relativePath));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy