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