Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/artifacts/model/Calculation.java @ 5838:5aa05a7a34b7
Rename modules to more fitting names.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 15:23:37 +0200 |
parents | flys-artifacts/src/main/java/org/dive4elements/river/artifacts/model/Calculation.java@bd047b71ab37 |
children | 4897a58c8746 |
comparison
equal
deleted
inserted
replaced
5837:d9901a08d0a6 | 5838:5aa05a7a34b7 |
---|---|
1 package org.dive4elements.river.artifacts.model; | |
2 | |
3 import org.w3c.dom.Document; | |
4 import org.w3c.dom.Element; | |
5 | |
6 import java.util.List; | |
7 import java.util.ArrayList; | |
8 import java.util.Arrays; | |
9 | |
10 import java.io.Serializable; | |
11 | |
12 import org.dive4elements.artifacts.CallMeta; | |
13 | |
14 import org.dive4elements.river.artifacts.resources.Resources; | |
15 | |
16 import org.dive4elements.river.utils.Formatter; | |
17 | |
18 /** A calculation that can have result and problems. */ | |
19 public class Calculation | |
20 implements Serializable | |
21 { | |
22 /** Problem of a calculation. | |
23 * Has location and message. */ | |
24 public static class Problem | |
25 implements Serializable | |
26 { | |
27 protected Double km; | |
28 protected String msg; | |
29 protected Object [] args; | |
30 | |
31 public Problem() { | |
32 } | |
33 | |
34 public Problem(String msg) { | |
35 this.msg = msg; | |
36 } | |
37 | |
38 public Problem(String msg, Object [] args) { | |
39 this.msg = msg; | |
40 this.args = args; | |
41 } | |
42 | |
43 public Problem(double km, String msg) { | |
44 this.km = km; | |
45 this.msg = msg; | |
46 } | |
47 | |
48 public Problem(double km, String msg, Object [] args) { | |
49 this.km = km; | |
50 this.msg = msg; | |
51 this.args = args; | |
52 } | |
53 | |
54 public Element toXML(Document document, CallMeta meta) { | |
55 Element problem = document.createElement("problem"); | |
56 if (km != null) { | |
57 problem.setAttribute( | |
58 "km", | |
59 Formatter.getCalculationKm(meta).format(km)); | |
60 } | |
61 String text = args != null | |
62 ? Resources.getMsg(meta, msg, msg, args) | |
63 : Resources.getMsg(meta, msg, msg); | |
64 problem.setTextContent(text); | |
65 return problem; | |
66 } | |
67 | |
68 public String getMsg() { | |
69 return msg; | |
70 } | |
71 | |
72 @Override | |
73 public boolean equals(Object other) { | |
74 if (!(other instanceof Problem)) { | |
75 return false; | |
76 } | |
77 Problem o = (Problem)other; | |
78 return !(!msg.equals(o.msg) | |
79 || (km == null && o.km != null) | |
80 || (km != null && o.km == null) | |
81 || (km != null && !km.equals(o.km)) | |
82 || !Arrays.equals(args, o.args)); | |
83 } | |
84 } // class Problem | |
85 | |
86 protected List<Problem> problems; | |
87 | |
88 public Calculation() { | |
89 } | |
90 | |
91 public Calculation(String msg) { | |
92 addProblem(msg); | |
93 } | |
94 | |
95 /** New Calculation with error which can be translated given args. */ | |
96 public Calculation(String msg, Object ... args) { | |
97 addProblem(msg, args); | |
98 } | |
99 | |
100 protected List<Problem> checkProblems() { | |
101 if (problems == null) { | |
102 problems = new ArrayList<Problem>(); | |
103 } | |
104 return problems; | |
105 } | |
106 | |
107 public void addProblems(Calculation other) { | |
108 List<Problem> otherProblems = other.problems; | |
109 if (otherProblems != null) { | |
110 List<Problem> problems = checkProblems(); | |
111 for (Problem problem: otherProblems) { | |
112 if (!problems.contains(problem)) { | |
113 problems.add(problem); | |
114 } | |
115 } | |
116 } | |
117 } | |
118 | |
119 public void addProblem(Problem problem) { | |
120 List<Problem> problems = checkProblems(); | |
121 if (!problems.contains(problem)) { | |
122 problems.add(problem); | |
123 } | |
124 } | |
125 | |
126 public void addProblem(String msg) { | |
127 addProblem(new Problem(msg)); | |
128 } | |
129 | |
130 public void addProblem(String msg, Object ... args) { | |
131 addProblem(new Problem(msg, args)); | |
132 } | |
133 | |
134 public void addProblem(double km, String msg) { | |
135 addProblem(new Problem(km, msg)); | |
136 } | |
137 | |
138 public void addProblem(double km, String msg, Object ... args) { | |
139 addProblem(new Problem(km, msg, args)); | |
140 } | |
141 | |
142 public boolean hasProblems() { | |
143 return problems != null && !problems.isEmpty(); | |
144 } | |
145 | |
146 public int numProblems() { | |
147 return problems != null ? problems.size() : 0; | |
148 } | |
149 | |
150 public List<Problem> getProblems() { | |
151 return problems; | |
152 } | |
153 | |
154 public void toXML(Document document, CallMeta meta) { | |
155 | |
156 Element root = document.createElement("problems"); | |
157 | |
158 if (hasProblems()) { | |
159 for (Problem problem: problems) { | |
160 root.appendChild(problem.toXML(document, meta)); | |
161 } | |
162 } | |
163 | |
164 document.appendChild(root); | |
165 } | |
166 } | |
167 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |