From c5a2fc44df90d93f28e85fd6612f84dcee5e5aed Mon Sep 17 00:00:00 2001 From: SiboVG Date: Mon, 27 Mar 2023 00:05:03 +0200 Subject: [PATCH] Create an error message & set --- core/src/net/sf/openrocket/logging/Error.java | 59 +++++++++++++++++++ .../net/sf/openrocket/logging/ErrorSet.java | 32 ++++++++++ 2 files changed, 91 insertions(+) create mode 100644 core/src/net/sf/openrocket/logging/Error.java create mode 100644 core/src/net/sf/openrocket/logging/ErrorSet.java diff --git a/core/src/net/sf/openrocket/logging/Error.java b/core/src/net/sf/openrocket/logging/Error.java new file mode 100644 index 000000000..69ba76dd6 --- /dev/null +++ b/core/src/net/sf/openrocket/logging/Error.java @@ -0,0 +1,59 @@ +package net.sf.openrocket.logging; + +import net.sf.openrocket.l10n.Translator; +import net.sf.openrocket.startup.Application; + +/** + * An error message wrapper. + */ +public abstract class Error extends Message { + private static final Translator trans = Application.getTranslator(); + + /** + * @return an Error with the specific text. + */ + public static Error fromString(String text) { + return new Error.Other(text); + } + + + ///////////// Specific Error classes ///////////// + + + /** + * An unspecified error type. This error type holds a String + * describing it. Two errors of this type are considered equal if the strings + * are identical. + */ + public static class Other extends Error { + private final String description; + + public Other(String description) { + this.description = description; + } + + @Override + public String toString() { + return description; + } + + @Override + public boolean equals(Object other) { + if (!(other instanceof Other)) + return false; + + Other o = (Other) other; + return (o.description.equals(this.description)); + } + + @Override + public int hashCode() { + return description.hashCode(); + } + + @Override + public boolean replaceBy(Message other) { + return false; + } + } +} diff --git a/core/src/net/sf/openrocket/logging/ErrorSet.java b/core/src/net/sf/openrocket/logging/ErrorSet.java new file mode 100644 index 000000000..76ea8977a --- /dev/null +++ b/core/src/net/sf/openrocket/logging/ErrorSet.java @@ -0,0 +1,32 @@ +package net.sf.openrocket.logging; + +import net.sf.openrocket.util.BugException; + +public class ErrorSet extends MessageSet { + /** + * Add an Error with the specified text to the set. The Error object + * is created using the {@link Error#fromString(String)} method. If an error of the + * same type exists in the set, the error that is left in the set is defined by the + * method {@link Error#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(Error.fromString(s)); + } + + @Override + public ErrorSet clone() { + try { + ErrorSet newSet = (ErrorSet) 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); + } + } +}