Initial commit
[debian/openrocket] / src / net / sf / openrocket / aerodynamics / WarningSet.java
1 package net.sf.openrocket.aerodynamics;
2
3 import java.util.AbstractSet;
4 import java.util.ArrayList;
5 import java.util.Iterator;
6
7 /**
8  * A set that contains multiple <code>Warning</code>s.  When adding a
9  * {@link Warning} to this set, the contents is checked for a warning of the
10  * same type.  If one is found, then the warning left in the set is determined
11  * by the method {@link #Warning.replaceBy(Warning)}.
12  * 
13  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
14  */
15 public class WarningSet extends AbstractSet<Warning> implements Cloneable {
16
17         private ArrayList<Warning> warnings = new ArrayList<Warning>();
18         
19         
20         /**
21          * Add a <code>Warning</code> to the set.  If a warning of the same type
22          * exists in the set, the warning that is left in the set is defined by the
23          * method {@link Warning#replaceBy(Warning)}.
24          */
25         @Override
26         public boolean add(Warning w) {
27                 int index = warnings.indexOf(w);
28                 
29                 if (index < 0) {
30                         warnings.add(w);
31                         return false;
32                 }
33                 
34                 Warning old = warnings.get(index);
35                 if (old.replaceBy(w)) {
36                         warnings.set(index, w);
37                 }
38                 
39                 return true;
40         }
41         
42         /**
43          * Add a <code>Warning</code> with the specified text to the set.  The Warning object
44          * is created using the {@link Warning#fromString(String)} method.  If a warning of the
45          * same type exists in the set, the warning that is left in the set is defined by the
46          * method {@link Warning#replaceBy(Warning)}.
47          * 
48          * @param s             the warning text.
49          */
50         public boolean add(String s) {
51                 return add(Warning.fromString(s));
52         }
53         
54         
55         @Override
56         public Iterator<Warning> iterator() {
57                 return warnings.iterator();
58         }
59
60         @Override
61         public int size() {
62                 return warnings.size();
63         }
64         
65         @SuppressWarnings("unchecked")
66         @Override
67         public WarningSet clone() {
68                 try {
69                         
70                         WarningSet newSet = (WarningSet) super.clone();
71                         newSet.warnings = (ArrayList<Warning>) this.warnings.clone();
72                         return newSet;
73                         
74                 } catch (CloneNotSupportedException e) {
75                         throw new RuntimeException("CloneNotSupportedException occurred, report bug!",e);
76                 }
77         }
78         
79         
80         @Override
81         public String toString() {
82                 String s = "";
83                 
84                 for (Warning w: warnings) {
85                         if (s.length() > 0)
86                                 s = s+",";
87                         s += w.toString();
88                 }
89                 return "WarningSet[" + s + "]";
90         }
91 }