|
| 1 | +/* |
| 2 | + * Copyright (c) 2023-2024 Ryuu Mitsuki |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.mitsuki.jmatrix.enums; |
| 18 | + |
| 19 | +import com.mitsuki.jmatrix.Matrix; |
| 20 | +import com.mitsuki.jmatrix.exception.*; |
| 21 | + |
| 22 | +/** |
| 23 | + * The {@code JMErrorCode} enum represents error codes for various error conditions |
| 24 | + * that can occur within a <b>JMatrix</b> library. Each error code is associated with |
| 25 | + * an integer error number, a string representation of the error code, and a descriptive |
| 26 | + * error message. |
| 27 | + * |
| 28 | + * <p>This enum provides methods to retrieve the error number, error code, and error message. |
| 29 | + * Additionally, it provides a static method to convert an error number or a string |
| 30 | + * representation of the error code back to its corresponding {@code JMErrorCode} enum value, |
| 31 | + * it is declared as {@link #valueOf(Object)}. The method does not throws an error if the type |
| 32 | + * of value is unrecognized, instead it returns {@code null}. |
| 33 | + * |
| 34 | + * <p><b>Example usage:</b> |
| 35 | + * <pre> |
| 36 | + * JMErrorCode errorCode = JMErrorCode.INVIDX; |
| 37 | + * int errno = errorCode.getErrno(); // 201 |
| 38 | + * String errcode = errorCode.getCode(); // INVIDX |
| 39 | + * String errmsg = errorCode.getMessage(); // Given index is out bounds |
| 40 | + * </pre> |
| 41 | + * |
| 42 | + * <p>Get the error code from a thrown exception: |
| 43 | + * <pre> |
| 44 | + * try { |
| 45 | + * Matrix m = new Matrix(); |
| 46 | + * m = Matrix.mult(m, 5); |
| 47 | + * } catch (Exception e) { |
| 48 | + * if (e instanceof JMatrixBaseException) { |
| 49 | + * e = (JMatrixBaseException) e; |
| 50 | + * String errcode = e.getErrorCode().getCode(); // NULLMT |
| 51 | + * } |
| 52 | + * } |
| 53 | + * </pre> |
| 54 | + * |
| 55 | + * @since 1.5.0 |
| 56 | + * @version 1.0, 05 June 2024 |
| 57 | + * @author <a href="https://github.com/mitsuki31" target="_blank"> |
| 58 | + * Ryuu Mitsuki</a> |
| 59 | + * @license <a href="https://www.apache.org/licenses/LICENSE-2.0" target="_blank"> |
| 60 | + * Apache License 2.0</a> |
| 61 | + * @see java.lang.Enum |
| 62 | + */ |
| 63 | +public enum JMErrorCode { |
| 64 | + /** |
| 65 | + * Error code indicating that the given index is out of bounds. |
| 66 | + * |
| 67 | + * <p>This error code is useful to indicates that user gives an index |
| 68 | + * either of row or column (which from methods that indexing-related, |
| 69 | + * such as {@link Matrix#insertRow(int, double[])}, {@link |
| 70 | + * Matrix#dropColumn(int)}, and {@link Matrix#display(int)}). |
| 71 | + * |
| 72 | + * <p><b>Error number:</b> {@code 201} |
| 73 | + * <p><b>Related exception:</b> {@link InvalidIndexException} |
| 74 | + */ |
| 75 | + INVIDX( 0xD3 ^ (0xD + 0xD), "Given index is out of bounds" ), |
| 76 | + /** |
| 77 | + * Error code indicating that the matrix has an invalid type. |
| 78 | + * |
| 79 | + * <p>This error code will be in the {@link IllegalMatrixSizeException} exception |
| 80 | + * which related to invalid type of matrix. For example, user attempting to |
| 81 | + * calculate trace with a {@code 5x3} matrix, which is an illegal attemption |
| 82 | + * because trace calculation are only for square matrices. |
| 83 | + * |
| 84 | + * <p><b>Error number:</b> {@code 202} |
| 85 | + * <p><b>Related exception:</b> {@link IllegalMatrixSizeException} |
| 86 | + */ |
| 87 | + INVTYP( 0xD3 ^ (0xD + 0xC), "Matrix has invalid type of matrix" ), |
| 88 | + /** |
| 89 | + * Error code indicating that the matrix is {@code null} or the matrix |
| 90 | + * entries is {@code null}. |
| 91 | + * |
| 92 | + * <p>A matrix with {@code null} entries can be constructed using |
| 93 | + * {@link Matrix#Matrix()} constructor, which is not require any arguments. |
| 94 | + * Thus, the returned matrix is a matrix with a {@code null} entries (in other |
| 95 | + * words, an uninitialized matrix). |
| 96 | + * |
| 97 | + * <p><b>Error number:</b> {@code 203} |
| 98 | + * <p><b>Related exception:</b> {@link NullMatrixException} |
| 99 | + */ |
| 100 | + NULLMT( 0xD3 ^ (0xD + 0xB), "Matrix is null" ), |
| 101 | + /** |
| 102 | + * Error code indicating an unknown error. |
| 103 | + * |
| 104 | + * <p>This error code can be made by the {@link JMatrixBaseException} class |
| 105 | + * when failed to evaluate the given error number (errno) or the cause exception |
| 106 | + * is not an instance of that class while trying to get the error code from the |
| 107 | + * given cause exception, and ended up constructs with this error code. |
| 108 | + * |
| 109 | + * <p><b>Error number:</b> {@code 400} |
| 110 | + * <p><b>Related exception:</b> {@link JMatrixBaseException} |
| 111 | + */ |
| 112 | + UNKERR( ((0xD3 << 1) - (0xF & 0xF) - 0b111), "Unknown error" ); |
| 113 | + |
| 114 | + /** The error nuumber stored for later retrieval by {@link #getErrno()} */ |
| 115 | + private int errno = 0x00; |
| 116 | + /** The error code stored for later retrieval by {@link #getCode()} */ |
| 117 | + private String code = null; |
| 118 | + /** The error message stored for later retrieval by {@link #getMessage()} */ |
| 119 | + private String message = null; |
| 120 | + |
| 121 | + /** |
| 122 | + * Constructs a {@code JMErrorCode} with the specified error number and detail message. |
| 123 | + * |
| 124 | + * @param errno The error number associated with this error code. |
| 125 | + * @param message The descriptive error message associated with this error code. |
| 126 | + * |
| 127 | + * @since 1.5.0 |
| 128 | + */ |
| 129 | + JMErrorCode(int errno, String message) { |
| 130 | + this.code = super.toString(); |
| 131 | + this.errno = errno; |
| 132 | + this.message = message; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Returns the error number associated with this error code. |
| 137 | + * |
| 138 | + * @return The error number. |
| 139 | + * |
| 140 | + * @since 1.5.0 |
| 141 | + * @see #getErrnoStr() |
| 142 | + */ |
| 143 | + public int getErrno() { |
| 144 | + return this.errno; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Returns the error number as a string in the format {@code JM###}. |
| 149 | + * |
| 150 | + * @return The error number as a string, prefixed with {@code JM}. |
| 151 | + * |
| 152 | + * @since 1.5.0 |
| 153 | + * @see #getErrno() |
| 154 | + */ |
| 155 | + public String getErrnoStr() { |
| 156 | + return "JM" + this.errno; // Return: JM### |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Returns the string representation of this error code. |
| 161 | + * |
| 162 | + * @return The error code. |
| 163 | + * |
| 164 | + * @since 1.5.0 |
| 165 | + */ |
| 166 | + public String getCode() { |
| 167 | + return this.code; |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Returns the descriptive error message associated with this error code. |
| 172 | + * |
| 173 | + * @return The descriptive error message. |
| 174 | + * |
| 175 | + * @since 1.5.0 |
| 176 | + */ |
| 177 | + public String getMessage() { |
| 178 | + return this.message; |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * {@inheritDoc} |
| 183 | + * |
| 184 | + * @since 1.5.0 |
| 185 | + */ |
| 186 | + @Override |
| 187 | + public String toString() { |
| 188 | + return String.format("%s[%s]", this.code, this.errno); |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * Returns the {@code JMErrorCode} corresponding to the specified value. |
| 193 | + * |
| 194 | + * <p>The value can be either an integer (error number) or a string (error code). |
| 195 | + * If the type of {@code x} is not recognized, this method returns {@code null}. |
| 196 | + * |
| 197 | + * @param <T> The type of value, which can be either {@link Integer} or {@link String}. |
| 198 | + * @param x The value to be evaluated. |
| 199 | + * |
| 200 | + * @return The corresponding {@code JMErrorCode}, or {@code null} if the type of |
| 201 | + * {@code x} is not known. |
| 202 | + * |
| 203 | + * @throws IllegalArgumentException If this enum has no constant with the specified |
| 204 | + * name (with string representation). |
| 205 | + * |
| 206 | + * @since 1.5.0 |
| 207 | + * @see Enum#valueOf(Class, String) |
| 208 | + */ |
| 209 | + public static <T extends Object> JMErrorCode valueOf(T x) { |
| 210 | + if (x instanceof Integer) { |
| 211 | + for (JMErrorCode ec : JMErrorCode.values()) { |
| 212 | + if ((Integer) x == ec.getErrno()) return ec; |
| 213 | + } |
| 214 | + } else if (x instanceof String) { |
| 215 | + // This might throws an `IllegalArgumentException` |
| 216 | + return JMErrorCode.valueOf((String) x); |
| 217 | + } |
| 218 | + //* No error being thrown if got unknown type, but return `null` |
| 219 | + return null; |
| 220 | + } |
| 221 | +} |
0 commit comments