summaryrefslogtreecommitdiff
path: root/java/src/org/singinst/uf/model/Calculation.java
blob: 7d2330742bf5bfffc93d3917e172d482260b7d01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package org.singinst.uf.model;

import java.awt.Color;
import org.singinst.uf.presenter.HtmlUtil;

import org.singinst.uf.common.StringUtil;
import org.singinst.uf.math.MathUtil;

public abstract class Calculation implements Evaluable {

	private final String description;
	private StringBuilder htmlConsole = new StringBuilder();
	private final boolean prependDescription;

	public Calculation(String description) {
		this(description, true);
	}

	public Calculation(String description, boolean prependDescription) {
		this.description = description.intern();
		this.prependDescription = prependDescription;
	}

	public String getDescription() {
		return description;
	}
	
	public final double evaluate() {
		return evaluate(getHtmlConsole());
	}
	
	public final double evaluate(StringBuilder htmlConsole) {
		if (prependDescription) {
			htmlConsole.append(description);
		}
		double raw = rawEvaluate(htmlConsole);
		double rounded = MathUtil.round(raw, 12);
//		String percentage = MathUtil.round(raw * 100, 2) + "%"; //FIXME
//		htmlConsole.append(" = " + HtmlUtil.htmlcolorFromColor(getColor(), percentage) + "<br>\n");
		htmlConsole.append(" = " + HtmlUtil.htmlcolorFromColor(getColor(), StringUtil.formatProbability(raw)) + "<br>\n");
		
		return rounded;
	}
	
	protected abstract double rawEvaluate(StringBuilder htmlConsole);

	/**
	 * 	Returns the color for this calculation, so that we consistently display the correct one.
	 * 
	 * @return
	 * 	The color to display this calculation
	 */
	public Color getColor() {
		return Color.BLACK;
	}
	
	public StringBuilder getHtmlConsole() {
		return htmlConsole;
	}

	public void setHtmlConsole(StringBuilder htmlConsole) {
		this.htmlConsole = htmlConsole;
	}

}