comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Calculation.java @ 3818:dc18457b1cef

merged flys-artifacts/pre2.7-2012-03-16
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:59 +0200
parents 6f4a1f513e89
children 3f24865082da
comparison
equal deleted inserted replaced
2456:60ab1054069d 3818:dc18457b1cef
1 package de.intevation.flys.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 de.intevation.artifacts.CallMeta;
13
14 import de.intevation.flys.artifacts.resources.Resources;
15
16 import de.intevation.flys.utils.Formatter;
17
18 public class Calculation
19 implements Serializable
20 {
21 public static class Problem
22 implements Serializable
23 {
24 protected Double km;
25 protected String msg;
26 protected Object [] args;
27
28 public Problem() {
29 }
30
31 public Problem(String msg) {
32 this.msg = msg;
33 }
34
35 public Problem(String msg, Object [] args) {
36 this.msg = msg;
37 this.args = args;
38 }
39
40 public Problem(double km, String msg) {
41 this.km = km;
42 this.msg = msg;
43 }
44
45 public Problem(double km, String msg, Object [] args) {
46 this.km = km;
47 this.msg = msg;
48 this.args = args;
49 }
50
51 public Element toXML(Document document, CallMeta meta) {
52 Element problem = document.createElement("problem");
53 if (km != null) {
54 problem.setAttribute(
55 "km",
56 Formatter.getCalculationKm(meta).format(km));
57 }
58 String text = args != null
59 ? Resources.getMsg(meta, msg, msg, args)
60 : Resources.getMsg(meta, msg, msg);
61 problem.setTextContent(text);
62 return problem;
63 }
64
65 @Override
66 public boolean equals(Object other) {
67 if (!(other instanceof Problem)) {
68 return false;
69 }
70 Problem o = (Problem)other;
71 return !(!msg.equals(o.msg)
72 || (km == null && o.km != null)
73 || (km != null && o.km == null)
74 || (km != null && !km.equals(o.km))
75 || !Arrays.equals(args, o.args));
76 }
77 } // class Problem
78
79 protected List<Problem> problems;
80
81 public Calculation() {
82 }
83
84 public Calculation(String msg) {
85 addProblem(msg);
86 }
87
88 protected List<Problem> checkProblems() {
89 if (problems == null) {
90 problems = new ArrayList<Problem>();
91 }
92 return problems;
93 }
94
95 public void addProblem(Problem problem) {
96 List<Problem> problems = checkProblems();
97 if (!problems.contains(problem)) {
98 problems.add(problem);
99 }
100 }
101
102 public void addProblem(String msg) {
103 addProblem(new Problem(msg));
104 }
105
106 public void addProblem(String msg, Object ... args) {
107 addProblem(new Problem(msg, args));
108 }
109
110 public void addProblem(double km, String msg) {
111 addProblem(new Problem(km, msg));
112 }
113
114 public void addProblem(double km, String msg, Object ... args) {
115 addProblem(new Problem(km, msg, args));
116 }
117
118 public boolean hasProblems() {
119 return problems != null && !problems.isEmpty();
120 }
121
122 public int numProblems() {
123 return problems != null ? problems.size() : 0;
124 }
125
126 public List<Problem> getProblems() {
127 return problems;
128 }
129
130 public void toXML(Document document, CallMeta meta) {
131
132 Element root = document.createElement("problems");
133
134 if (hasProblems()) {
135 for (Problem problem: problems) {
136 root.appendChild(problem.toXML(document, meta));
137 }
138 }
139
140 document.appendChild(root);
141 }
142 }
143 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org