Refactor WarningSet in general classes
This commit is contained in:
parent
bc3e812fe5
commit
2bc93530bc
43
core/src/net/sf/openrocket/logging/Message.java
Normal file
43
core/src/net/sf/openrocket/logging/Message.java
Normal file
@ -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 <code>true</code> if the <code>other</code> warning should replace
|
||||
* this message. The method should return <code>true</code> 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 <code>Message</code>s 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 <code>hashCode</code> method compatible with the <code>equals</code> method.
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return this.getClass().hashCode();
|
||||
}
|
||||
}
|
123
core/src/net/sf/openrocket/logging/MessageSet.java
Normal file
123
core/src/net/sf/openrocket/logging/MessageSet.java
Normal file
@ -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 <code>Message</code>s. 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)}.
|
||||
* <p>
|
||||
* A MessageSet can be made immutable by calling {@link #immute()}.
|
||||
*
|
||||
* @author Sampo Niskanen <sampo.niskanen@iki.fi>
|
||||
*/
|
||||
public abstract class MessageSet<E extends Message> extends AbstractSet<E> implements Cloneable, Monitorable {
|
||||
/** the actual array of messages */
|
||||
protected ArrayList<E> messages = new ArrayList<>();
|
||||
|
||||
protected Mutable mutable = new Mutable();
|
||||
private int modID = 0;
|
||||
|
||||
/**
|
||||
* Add a <code>Message</code> 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 <code>Message</code> 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 <code>Message</code> 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<E> iterator() {
|
||||
final Iterator<E> 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;
|
||||
}
|
||||
}
|
@ -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 <code>true</code> if the <code>other</code> warning should replace
|
||||
* this warning. The method should return <code>true</code> 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 <code>Warning</code>s 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 <code>hashCode</code> method compatible with the <code>equals</code> 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;
|
||||
}
|
||||
}
|
||||
|
@ -12,136 +12,37 @@ import net.sf.openrocket.util.Mutable;
|
||||
* A set that contains multiple <code>Warning</code>s. 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)}.
|
||||
* <p>
|
||||
* A WarningSet can be made immutable by calling {@link #immute()}.
|
||||
*
|
||||
* @author Sampo Niskanen <sampo.niskanen@iki.fi>
|
||||
*/
|
||||
public class WarningSet extends AbstractSet<Warning> implements Cloneable, Monitorable {
|
||||
|
||||
/** the actual array of warnings*/
|
||||
private ArrayList<Warning> warnings = new ArrayList<Warning>();
|
||||
|
||||
private Mutable mutable = new Mutable();
|
||||
|
||||
private int modID = 0;
|
||||
|
||||
/**
|
||||
* Add a <code>Warning</code> 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 <code>Warning</code> 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<Warning> {
|
||||
/**
|
||||
* Add a <code>Warning</code> 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 <code>Warning</code> 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<Warning> iterator() {
|
||||
final Iterator<Warning> iterator = warnings.iterator();
|
||||
return new Iterator<Warning>() {
|
||||
@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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user