create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / aerodynamics / WarningSet.java
1 package net.sf.openrocket.aerodynamics;
2
3 import java.util.AbstractSet;
4 import java.util.Iterator;
5
6 import net.sf.openrocket.util.ArrayList;
7 import net.sf.openrocket.util.BugException;
8 import net.sf.openrocket.util.Monitorable;
9 import net.sf.openrocket.util.Mutable;
10
11 /**
12  * A set that contains multiple <code>Warning</code>s.  When adding a
13  * {@link Warning} to this set, the contents is checked for a warning of the
14  * same type.  If one is found, then the warning left in the set is determined
15  * by the method {@link Warning#replaceBy(Warning)}.
16  * <p>
17  * A WarningSet can be made immutable by calling {@link #immute()}.
18  * 
19  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
20  */
21 public class WarningSet extends AbstractSet<Warning> implements Cloneable, Monitorable {
22         
23         private ArrayList<Warning> warnings = new ArrayList<Warning>();
24         
25         private Mutable mutable = new Mutable();
26         
27         private int modID = 0;
28         
29         /**
30          * Add a <code>Warning</code> to the set.  If a warning of the same type
31          * exists in the set, the warning that is left in the set is defined by the
32          * method {@link Warning#replaceBy(Warning)}.
33          * 
34          * @throws IllegalStateException        if this warning set has been made immutable.
35          */
36         @Override
37         public boolean add(Warning w) {
38                 mutable.check();
39                 
40                 modID++;
41                 int index = warnings.indexOf(w);
42                 
43                 if (index < 0) {
44                         warnings.add(w);
45                         return false;
46                 }
47                 
48                 Warning old = warnings.get(index);
49                 if (old.replaceBy(w)) {
50                         warnings.set(index, w);
51                 }
52                 
53                 return true;
54         }
55         
56         /**
57          * Add a <code>Warning</code> with the specified text to the set.  The Warning object
58          * is created using the {@link Warning#fromString(String)} method.  If a warning of the
59          * same type exists in the set, the warning that is left in the set is defined by the
60          * method {@link Warning#replaceBy(Warning)}.
61          * 
62          * @param s             the warning text.
63          * @throws IllegalStateException        if this warning set has been made immutable.
64          */
65         public boolean add(String s) {
66                 mutable.check();
67                 return add(Warning.fromString(s));
68         }
69         
70         
71         @Override
72         public Iterator<Warning> iterator() {
73                 final Iterator<Warning> iterator = warnings.iterator();
74                 return new Iterator<Warning>() {
75                         @Override
76                         public boolean hasNext() {
77                                 return iterator.hasNext();
78                         }
79                         
80                         @Override
81                         public Warning next() {
82                                 return iterator.next();
83                         }
84                         
85                         @Override
86                         public void remove() {
87                                 mutable.check();
88                                 iterator.remove();
89                         }
90                         
91                 };
92         }
93         
94         @Override
95         public int size() {
96                 return warnings.size();
97         }
98         
99         
100         public void immute() {
101                 mutable.immute();
102         }
103         
104         
105         @Override
106         public WarningSet clone() {
107                 try {
108                         
109                         WarningSet newSet = (WarningSet) super.clone();
110                         newSet.warnings = this.warnings.clone();
111                         newSet.mutable = this.mutable.clone();
112                         return newSet;
113                         
114                 } catch (CloneNotSupportedException e) {
115                         throw new BugException("CloneNotSupportedException occurred, report bug!", e);
116                 }
117         }
118         
119         
120         @Override
121         public String toString() {
122                 String s = "";
123                 
124                 for (Warning w : warnings) {
125                         if (s.length() > 0)
126                                 s = s + ",";
127                         s += w.toString();
128                 }
129                 return "WarningSet[" + s + "]";
130         }
131         
132         @Override
133         public int getModID() {
134                 return modID;
135         }
136 }