LibraryInfo.java

package edu.udel.cis.vsl.tass.model.impl;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Vector;

import edu.udel.cis.vsl.tass.model.IF.SyntaxException;
import edu.udel.cis.vsl.tass.model.IF.SystemFunctionIF;
import edu.udel.cis.vsl.tass.util.TASSInternalException;

/** A class used to store information about a library. */
public class LibraryInfo {

	/** The name of the library, e.g., "stdio". */
	private String libraryName;

	/** The library ID number. */
	private int libraryId;

	/**
	 * Some system functions defined in the library that have acutally been used
	 * at least once in a model. In position i, there is a non-null instance of
	 * system function with idInLibrary i from one of the processes.
	 */
	private Vector<SystemFunctionIF> representatives = new Vector<SystemFunctionIF>();

	/**
	 * Map which given name of a function in this library, returns the
	 * representiave instance of the system function with that name, or null if
	 * there is none.
	 */
	private Map<String, SystemFunctionIF> representativeMap = new LinkedHashMap<String, SystemFunctionIF>();

	public LibraryInfo(String libraryName, int libraryId) {
		this.libraryName = libraryName;
		this.libraryId = libraryId;
	}

	public SystemFunctionIF getRepresentativeWithName(String name) {
		return representativeMap.get(name);
	}

	public SystemFunctionIF getRepresentativeWithId(int id) {
		return representatives.get(id);
	}

	/**
	 * Registers this system function with the library info object. First,
	 * determines if a function with this name has already been registered with
	 * this library. If so, the id number of the old function is used to set the
	 * idInLibrary of the new one. Moreover, it is checked that the functions
	 * have the same input/output signatures (TODO).
	 * 
	 * If the library does not already have a function with this name, this is
	 * added to the list of representatives for this library info object and
	 * assigned a new ID number.
	 * 
	 */
	public void register(SystemFunctionIF function) throws SyntaxException {
		String name = function.name();
		SystemFunctionIF oldFunction = representativeMap.get(name);

		((SystemFunction) function).setLibraryInfo(this);
		if (function.idInLibrary() >= 0) {
			throw new TASSInternalException(
					"Attempt to register system function twice: " + function);
		}
		if (oldFunction == null) {
			((SystemFunction) function).setIdInLibrary(representatives.size());
			representatives.add(function);
			representativeMap.put(name, function);
		} else {
			((SystemFunction) function).setIdInLibrary(oldFunction
					.idInLibrary());
			// check signatures agree here
		}
	}

	public int libraryId() {
		return libraryId;
	}

	public String libraryName() {
		return libraryName;
	}
}