PageRenderTime 45ms CodeModel.GetById 12ms app.highlight 29ms RepoModel.GetById 1ms app.codeStats 0ms

/interpreter/tags/at2dist110511/src/edu/vub/util/TempFieldGenerator.java

http://ambienttalk.googlecode.com/
Java | 150 lines | 121 code | 23 blank | 6 comment | 12 complexity | d91c60b9815058f101dc89586d967312 MD5 | raw file
  1package edu.vub.util;
  2
  3import java.util.HashMap;
  4import java.util.Iterator;
  5import java.util.Vector;
  6
  7import edu.vub.at.exceptions.InterpreterException;
  8import edu.vub.at.exceptions.XSerializationError;
  9import edu.vub.at.objects.ATObject;
 10import edu.vub.at.objects.ATTypeTag;
 11import edu.vub.at.objects.natives.NATText;
 12
 13/**
 14 * Uniquely maps ATObjects to their serialized version
 15 * and links them to a temporary name
 16 * 
 17 * @author kpinte
 18 */
 19public class TempFieldGenerator {
 20	
 21	private static final String PREFIX = "_v";
 22	
 23
 24	
 25	private final Vector<ATObject> objects_;
 26	private final HashMap<ATObject, NATText> objectToCode_;
 27	private final HashMap<ATObject, NATText> objectToName_;
 28	private final HashMap<ATTypeTag, NATText> typeToCode_;
 29	private final HashMap<ATTypeTag, NATText> typeToName_;
 30	private int nameCounter_;
 31	private int quoteLevel_;
 32	
 33	public TempFieldGenerator() {
 34		objects_ = new Vector<ATObject>();
 35		objectToCode_ = new HashMap<ATObject, NATText>();
 36		objectToName_ = new HashMap<ATObject, NATText>();
 37		typeToCode_ = new HashMap<ATTypeTag, NATText>();
 38		typeToName_ = new HashMap<ATTypeTag, NATText>();
 39		nameCounter_ = 0;
 40		quoteLevel_ = 0;
 41	}
 42	
 43	private synchronized String generateName() {
 44		nameCounter_++;
 45		return PREFIX + nameCounter_;
 46	}
 47	
 48	public boolean inQuote() {
 49		return (quoteLevel_ > 0);
 50	}
 51	
 52	public boolean incQuoteLevel() {
 53		quoteLevel_++;
 54		return inQuote();
 55	}
 56	
 57	public boolean decQuoteLevel() throws XSerializationError {
 58		if (quoteLevel_ <= 0) {
 59			throw new XSerializationError("quote level below 0");
 60		}
 61		quoteLevel_--;
 62		return inQuote();
 63	}
 64	
 65	public NATText put(ATObject object, NATText code) {
 66		NATText name;
 67		if (this.contains(object)){
 68			name = objectToName_.get(object);
 69		} else {
 70			name = NATText.atValue(this.generateName());
 71			objects_.add(object);
 72		}
 73		objectToCode_.put(object, code);
 74		objectToName_.put(object, name);
 75		if (this.inQuote()) {
 76			return NATText.atValue("#" + name.javaValue);
 77		} else {
 78			return name;
 79		} 
 80	}
 81	
 82	public NATText putType(ATTypeTag tt, NATText name, NATText code) {
 83		typeToCode_.put(tt, code);
 84		typeToName_.put(tt, name);
 85		if (this.inQuote()) {
 86			return NATText.atValue("#" + name.javaValue);
 87		} else {
 88			return name;
 89		} 
 90	}
 91	
 92	public NATText getName(ATObject key) {
 93		NATText name = objectToName_.get(key);
 94		if (this.inQuote()) {
 95			return NATText.atValue("#" + name.javaValue);
 96		} else {
 97			return name;
 98		} 
 99	}
100	
101	public NATText getTypeName(ATTypeTag tt) {
102		NATText name = typeToName_.get(tt);
103		if (this.inQuote()) {
104			return NATText.atValue("#" + name.javaValue);
105		} else {
106			return name;
107		} 
108	}
109	
110	public NATText getTypeCode(ATTypeTag tt) {
111		return typeToCode_.get(tt);
112	}
113	
114	public NATText getCode(ATObject key) {
115		return objectToCode_.get(key);
116	}
117	
118	public Boolean contains(ATObject object) {
119		return objectToName_.containsKey(object);
120	}
121	
122	public Boolean containsType(ATTypeTag tt) {
123		return typeToName_.containsKey(tt);
124	}
125	
126	public NATText generateCode(NATText targetName) throws InterpreterException {
127		StringBuffer out = new StringBuffer("{");
128		Iterator<ATTypeTag> tit = typeToName_.keySet().iterator();
129		while (tit.hasNext()) {
130			ATTypeTag tt = tit.next();
131			NATText name = this.getTypeName(tt);
132			NATText code = this.getTypeCode(tt);
133			out.append(code.javaValue + "; ");
134		};
135			
136		Iterator<ATObject> it = objects_.iterator();
137		while(it.hasNext()) {
138			ATObject key = it.next();
139			NATText name = this.getName(key);
140			NATText code = this.getCode(key);
141			out.append("def " + name.javaValue + " := ");
142			out.append(code.javaValue);
143			out.append("; ");
144		}
145		out.append(targetName.javaValue);
146		out.append("}()");
147		return NATText.atValue(out.toString());
148	}
149
150}