Newer
Older
AlgebraicDataflowArchitectureModel / AlgebraicDataflowArchitectureModel / src / models / dataConstraintModel / JsonTerm.java
package models.dataConstraintModel;

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

import models.algebra.Constant;
import models.algebra.Expression;
import models.algebra.Symbol;
import models.algebra.Term;

public class JsonTerm extends Term {
	private Map<String, Integer> keyToIndex = new HashMap<>();

	public JsonTerm() {
		super(new Symbol("json", -1));
		setType(DataConstraintModel.typeJson);
	}
	
	public void addMember(String key, Expression value) {
		if (keyToIndex.containsKey(key)) {
			setChild(keyToIndex.get(key), value);
		} else {
			keyToIndex.put(key, getChildren().size());
			addChild(value);
		}
	}
	
	public Set<String> keySet() {
		return keyToIndex.keySet();
	}

	public Expression get(String key) {
		return getChild(keyToIndex.get(key));
	}

	public Expression get(Constant key) {
		return getChild(keyToIndex.get(key.toString()));
	}
	
	public String toString() {
		String jsonStr = "{";
		String delim = "";
		for (String key: keyToIndex.keySet()) {
			jsonStr += delim + key + ": " + getChild(keyToIndex.get(key)).toString();
			delim = ", ";
		}
		return jsonStr + "}";
	}
}