package archmapper.main.model;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import archmapper.main.Preferences;
/**
* Caches architecture information so that the mapping files need not be parsed
* when it is not necessary. Checks if one of the mapping files has changed and
* only returns the cached version if none of the files has changed.
*
* @author mg
*
*/
public class ArchitectureMappingCache {
protected static Map<IProject, MappingInformation> mappings = new HashMap<IProject, MappingInformation>();
public static ArchitectureMappingModel getArchitectureMappingModel(
IProject project) {
ArchitectureMappingModel model = null;
MappingInformation mapping = mappings.get(project);
if (mapping != null) {
// look if other mapping files have been declared in the preferences...
Preferences pref = new Preferences(project);
if (mapping.model.getArchitecture().getAdlFile() != null &&
!mapping.model.getArchitecture().getAdlFile().equals(pref.getArchitectureFile())) {
mapping = null;
} else if (mapping.model.getArchMapping().getMappingFile() != null &&
!mapping.model.getArchMapping().getMappingFile().equals(pref.getArchitectureMappingFile())) {
mapping = null;
}
// look if one of the mapping files has changed since the
// last parsing...
if (mapping != null) {
IFile[] files = new IFile[] {mapping.model.getArchitecture().getAdlFile(),
mapping.model.getArchMapping().getMappingFile(),
mapping.model.getStyleMapping().getMappingFile() };
long[] oldTimeStamps = new long[] { mapping.archModificationStamp,
mapping.archMappingModificationStamp,
mapping.styleMappingModificationStamp };
for (int i=0; i<files.length; i++) {
if (files[i] != null) {
if (files[i].getModificationStamp() != oldTimeStamps[i]) {
mapping = null;
}
}
}
}
if (mapping != null) {
model = mapping.model;
}
}
if (mapping == null) {
model = new ArchitectureMappingModel(project);
mapping = new MappingInformation();
mapping.model = model;
mapping.archModificationStamp = model.getArchitecture()
.getAdlFile().getModificationStamp();
if (model.getArchMapping().getMappingFile() != null) {
mapping.archMappingModificationStamp = model.getArchMapping()
.getMappingFile().getModificationStamp();
}
if (model.getStyleMapping().getMappingFile() != null) {
mapping.styleMappingModificationStamp = model.getStyleMapping()
.getMappingFile().getModificationStamp();
}
mappings.put(project, mapping);
}
return model;
}
}
class MappingInformation {
public ArchitectureMappingModel model;
public long archModificationStamp;
public long archMappingModificationStamp;
public long styleMappingModificationStamp;
}