diff --git a/core/src/net/sf/openrocket/logging/Message.java b/core/src/net/sf/openrocket/logging/Message.java new file mode 100644 index 000000000..9b813c1a8 --- /dev/null +++ b/core/src/net/sf/openrocket/logging/Message.java @@ -0,0 +1,43 @@ +package net.sf.openrocket.logging; + +/** + * Baseclass for logging messages (warnings, errors...) + */ +public abstract class Message { + /** + * @return a Message with the specific text. + */ + public static Message fromString(String text) { + return new Warning.Other(text); + } + + /** + * Return true if the other warning should replace + * this message. The method should return true if the other + * indicates a "worse" condition than the current warning. + * + * @param other the message to compare to + * @return whether this message should be replaced + */ + public abstract boolean replaceBy(Message other); + + + /** + * Two Messages are by default considered equal if they are of + * the same class. Therefore only one instance of a particular message type + * is stored in a {@link MessageSet}. Subclasses may override this method for + * more specific functionality. + */ + @Override + public boolean equals(Object o) { + return o != null && (o.getClass() == this.getClass()); + } + + /** + * A hashCode method compatible with the equals method. + */ + @Override + public int hashCode() { + return this.getClass().hashCode(); + } +} diff --git a/core/src/net/sf/openrocket/logging/MessageSet.java b/core/src/net/sf/openrocket/logging/MessageSet.java new file mode 100644 index 000000000..98642d1aa --- /dev/null +++ b/core/src/net/sf/openrocket/logging/MessageSet.java @@ -0,0 +1,123 @@ +package net.sf.openrocket.logging; + +import net.sf.openrocket.util.ArrayList; +import net.sf.openrocket.util.BugException; +import net.sf.openrocket.util.Monitorable; +import net.sf.openrocket.util.Mutable; + +import java.util.AbstractSet; +import java.util.Iterator; + +/** + * A set that contains multiple Messages. When adding a + * {@link Message} to this set, the contents is checked for a message of the + * same type. If one is found, then the message left in the set is determined + * by the method {@link Message#replaceBy(Message)}. + *

+ * A MessageSet can be made immutable by calling {@link #immute()}. + * + * @author Sampo Niskanen + */ +public abstract class MessageSet extends AbstractSet implements Cloneable, Monitorable { + /** the actual array of messages */ + protected ArrayList messages = new ArrayList<>(); + + protected Mutable mutable = new Mutable(); + private int modID = 0; + + /** + * Add a Message to the set. If a message of the same type + * exists in the set, the message that is left in the set is defined by the + * method {@link Message#replaceBy(Message)}. + * + * @throws IllegalStateException if this message set has been made immutable. + */ + @Override + public boolean add(E m) { + mutable.check(); + + modID++; + int index = messages.indexOf(m); + + if (index < 0) { + messages.add(m); + return false; + } + + E old = messages.get(index); + if (old.replaceBy(m)) { + messages.set(index, m); + } + + return true; + } + + /** + * Add a Message with the specified text to the set. + * + * @param s the message text. + * @throws IllegalStateException if this message set has been made immutable. + */ + public abstract boolean add(String s); + + /** + * Add a Message of the specified type with the specified discriminator to the + * set. + * @param m the message + * @param d the extra discriminator + * + */ + public boolean add (E m, String d) { + return this.add(m.toString() + ": \"" + d + "\""); + } + + @Override + public Iterator iterator() { + final Iterator iterator = messages.iterator(); + return new Iterator<>() { + @Override + public boolean hasNext() { + return iterator.hasNext(); + } + + @Override + public E next() { + return iterator.next(); + } + + @Override + public void remove() { + mutable.check(); + iterator.remove(); + } + }; + } + + @Override + public int size() { + return messages.size(); + } + + + public void immute() { + mutable.immute(); + } + + + @Override + public String toString() { + StringBuilder s = new StringBuilder(); + + for (Message m : messages) { + if (s.length() > 0) + s.append(","); + s.append(m.toString()); + } + return "Messages[" + s + "]"; + } + + @Override + public int getModID() { + return modID; + } +} diff --git a/core/src/net/sf/openrocket/logging/Warning.java b/core/src/net/sf/openrocket/logging/Warning.java index 8d053758c..9d1bc0421 100644 --- a/core/src/net/sf/openrocket/logging/Warning.java +++ b/core/src/net/sf/openrocket/logging/Warning.java @@ -6,50 +6,22 @@ import net.sf.openrocket.startup.Application; import net.sf.openrocket.simulation.FlightEvent; import net.sf.openrocket.unit.UnitGroup; -public abstract class Warning { +/** + * A warning message wrapper. + */ +public abstract class Warning extends Message { /** support to multiple languages warning */ private static final Translator trans = Application.getTranslator(); - + /** * @return a Warning with the specific text. */ public static Warning fromString(String text) { return new Warning.Other(text); } - - /** - * Return true if the other warning should replace - * this warning. The method should return true if the other - * warning indicates a "worse" condition than the current warning. - * - * @param other the warning to compare to - * @return whether this warning should be replaced - */ - public abstract boolean replaceBy(Warning other); - - - /** - * Two Warnings are by default considered equal if they are of - * the same class. Therefore only one instance of a particular warning type - * is stored in a {@link WarningSet}. Subclasses may override this method for - * more specific functionality. - */ - @Override - public boolean equals(Object o) { - return o != null && (o.getClass() == this.getClass()); - } - - /** - * A hashCode method compatible with the equals method. - */ - @Override - public int hashCode() { - return this.getClass().hashCode(); - } - - - + + ///////////// Specific warning classes ///////////// @@ -80,9 +52,9 @@ public abstract class Warning { return (trans.get("Warning.LargeAOA.str2") + UnitGroup.UNITS_ANGLE.getDefaultUnit().toString(aoa) + ")."); } - + @Override - public boolean replaceBy(Warning other) { + public boolean replaceBy(Message other) { if (!(other instanceof LargeAOA)) return false; @@ -119,7 +91,7 @@ public abstract class Warning { } @Override - public boolean replaceBy(Warning other) { + public boolean replaceBy(Message other) { return false; } } @@ -134,7 +106,7 @@ public abstract class Warning { /** * Sole constructor. The argument is an event which has occurred after landing * - * @param event the event that caused this warning + * @param _event the event that caused this warning */ public EventAfterLanding(FlightEvent _event) { this.event = _event; @@ -154,7 +126,7 @@ public abstract class Warning { } @Override - public boolean replaceBy(Warning other) { + public boolean replaceBy(Message other) { return false; } } @@ -249,7 +221,7 @@ public abstract class Warning { @Override - public boolean replaceBy(Warning other) { + public boolean replaceBy(Message other) { return false; } @@ -349,7 +321,7 @@ public abstract class Warning { } @Override - public boolean replaceBy(Warning other) { + public boolean replaceBy(Message other) { return false; } } diff --git a/core/src/net/sf/openrocket/logging/WarningSet.java b/core/src/net/sf/openrocket/logging/WarningSet.java index 2c248786b..2220904ba 100644 --- a/core/src/net/sf/openrocket/logging/WarningSet.java +++ b/core/src/net/sf/openrocket/logging/WarningSet.java @@ -12,136 +12,37 @@ import net.sf.openrocket.util.Mutable; * A set that contains multiple Warnings. When adding a * {@link Warning} to this set, the contents is checked for a warning of the * same type. If one is found, then the warning left in the set is determined - * by the method {@link Warning#replaceBy(Warning)}. + * by the method {@link Warning#replaceBy(Message)}. *

* A WarningSet can be made immutable by calling {@link #immute()}. * * @author Sampo Niskanen */ -public class WarningSet extends AbstractSet implements Cloneable, Monitorable { - - /** the actual array of warnings*/ - private ArrayList warnings = new ArrayList(); - - private Mutable mutable = new Mutable(); - - private int modID = 0; - - /** - * Add a Warning to the set. If a warning of the same type - * exists in the set, the warning that is left in the set is defined by the - * method {@link Warning#replaceBy(Warning)}. - * - * @throws IllegalStateException if this warning set has been made immutable. - */ - @Override - public boolean add(Warning w) { - mutable.check(); - - modID++; - int index = warnings.indexOf(w); - - if (index < 0) { - warnings.add(w); - return false; - } - - Warning old = warnings.get(index); - if (old.replaceBy(w)) { - warnings.set(index, w); - } - - return true; - } - - /** - * Add a Warning with the specified text to the set. The Warning object - * is created using the {@link Warning#fromString(String)} method. If a warning of the - * same type exists in the set, the warning that is left in the set is defined by the - * method {@link Warning#replaceBy(Warning)}. - * - * @param s the warning text. - * @throws IllegalStateException if this warning set has been made immutable. - */ - public boolean add(String s) { - mutable.check(); - return add(Warning.fromString(s)); - } +public class WarningSet extends MessageSet { + /** + * Add a Warning with the specified text to the set. The Warning object + * is created using the {@link Message#fromString(String)} method. If a warning of the + * same type exists in the set, the warning that is left in the set is defined by the + * method {@link Warning#replaceBy(Message)}. + * + * @param s the message text. + * @throws IllegalStateException if this message set has been made immutable. + */ + public boolean add(String s) { + mutable.check(); + return add(Warning.fromString(s)); + } - /** - * Add a Warning of the specified type with the specified discriminator to the - * set. - * @param w the warning - * @param d the extra discriminator - * - */ - public boolean add (Warning w, String d) { - return this.add(w.toString() + ": \"" + d + "\""); - } - - @Override - public Iterator iterator() { - final Iterator iterator = warnings.iterator(); - return new Iterator() { - @Override - public boolean hasNext() { - return iterator.hasNext(); - } - - @Override - public Warning next() { - return iterator.next(); - } - - @Override - public void remove() { - mutable.check(); - iterator.remove(); - } - - }; - } - - @Override - public int size() { - return warnings.size(); - } - - - public void immute() { - mutable.immute(); - } - - - @Override - public WarningSet clone() { - try { - - WarningSet newSet = (WarningSet) super.clone(); - newSet.warnings = this.warnings.clone(); - newSet.mutable = this.mutable.clone(); - return newSet; - - } catch (CloneNotSupportedException e) { - throw new BugException("CloneNotSupportedException occurred, report bug!", e); - } - } - - - @Override - public String toString() { - String s = ""; - - for (Warning w : warnings) { - if (s.length() > 0) - s = s + ","; - s += w.toString(); - } - return "WarningSet[" + s + "]"; - } - - @Override - public int getModID() { - return modID; - } + @Override + public WarningSet clone() { + try { + WarningSet newSet = (WarningSet) super.clone(); + newSet.messages = this.messages.clone(); + newSet.mutable = this.mutable.clone(); + return newSet; + + } catch (CloneNotSupportedException e) { + throw new BugException("CloneNotSupportedException occurred, report bug!", e); + } + } }