1 package net.sf.openrocket.aerodynamics;
3 import java.util.AbstractSet;
4 import java.util.ArrayList;
5 import java.util.Iterator;
7 import net.sf.openrocket.util.BugException;
8 import net.sf.openrocket.util.Monitorable;
9 import net.sf.openrocket.util.Mutable;
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)}.
17 * A WarningSet can be made immutable by calling {@link #immute()}.
19 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
21 public class WarningSet extends AbstractSet<Warning> implements Cloneable, Monitorable {
23 private ArrayList<Warning> warnings = new ArrayList<Warning>();
25 private Mutable mutable = new Mutable();
27 private int modID = 0;
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)}.
34 * @throws IllegalStateException if this warning set has been made immutable.
37 public boolean add(Warning w) {
41 int index = warnings.indexOf(w);
48 Warning old = warnings.get(index);
49 if (old.replaceBy(w)) {
50 warnings.set(index, w);
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)}.
62 * @param s the warning text.
63 * @throws IllegalStateException if this warning set has been made immutable.
65 public boolean add(String s) {
67 return add(Warning.fromString(s));
72 public Iterator<Warning> iterator() {
73 final Iterator<Warning> iterator = warnings.iterator();
74 return new Iterator<Warning>() {
76 public boolean hasNext() {
77 return iterator.hasNext();
81 public Warning next() {
82 return iterator.next();
86 public void remove() {
96 return warnings.size();
100 public void immute() {
105 @SuppressWarnings("unchecked")
107 public WarningSet clone() {
110 WarningSet newSet = (WarningSet) super.clone();
111 newSet.warnings = (ArrayList<Warning>) this.warnings.clone();
112 newSet.mutable = this.mutable.clone();
115 } catch (CloneNotSupportedException e) {
116 throw new BugException("CloneNotSupportedException occurred, report bug!", e);
122 public String toString() {
125 for (Warning w : warnings) {
130 return "WarningSet[" + s + "]";
134 public int getModID() {