PageRenderTime 55ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/jjil/src/jjil/core/Error.java

http://jjil.googlecode.com/
Java | 185 lines | 71 code | 17 blank | 97 comment | 6 complexity | 75d499d6403aa70d02dc53e5efb948b2 MD5 | raw file
  1. /**
  2. * Error defines a common error-reporting mechanism for all JJIL classes.
  3. * It includes an error code and up to three string objects representing
  4. * objects that explain the error, for example file names or images.
  5. *
  6. * Build-specific libraries like jjil.android or jjil.j2se will define
  7. * a Error.toString() class that converts the Error object into a localized
  8. * error message.
  9. *
  10. *
  11. */
  12. package jjil.core;
  13. /**
  14. * Error defines a common error-reporting mechanism for all JJIL classes.
  15. * It includes an error code and up to three string objects representing
  16. * objects that explain the error, for example file names or images.
  17. * @author webb
  18. *
  19. */
  20. public class Error extends Throwable {
  21. /**
  22. * J2ME's Java is only 1.4 so no enums. We must simulate them...
  23. */
  24. public static class PACKAGE {
  25. /**
  26. * Error code is defined in jjil.algorithm package.
  27. */
  28. public static final int ALGORITHM = 0;
  29. /**
  30. * Error code is defined in jjil.android package.
  31. */
  32. public static final int ANDROID = ALGORITHM + 1;
  33. /**
  34. * Error code is defined in jjil.core package.
  35. */
  36. public static final int CORE = ANDROID + 1;
  37. /**
  38. * Error code is defined in jjil.j2me package.
  39. */
  40. public static final int J2ME = CORE + 1;
  41. /**
  42. * Error code is defined in jjil.j2se package.
  43. */
  44. public static final int J2SE = J2ME + 1;
  45. /**
  46. * Count of packages.
  47. */
  48. public static final int COUNT = J2SE + 1;
  49. }
  50. /**
  51. * nCode is a general error code. Possible values are defined in the CODES enumerated
  52. * type (really, we use ints for compatibility with J2ME).
  53. */
  54. private int nCode;
  55. /**
  56. * The package where the error code is defined.
  57. */
  58. private int nPackage;
  59. /**
  60. * szParam1 is a primary parameter giving detailed error information.
  61. */
  62. private String szParam1;
  63. /**
  64. * szParam2 is a secondary parameter giving detailed error information.
  65. */
  66. private String szParam2;
  67. /**
  68. * szParam3 is a tertiary parameter giving detailed error information.
  69. */
  70. private String szParam3;
  71. /**
  72. * Copy constructor.
  73. * @param e Error object to copy.
  74. */
  75. public Error(Error e) {
  76. this.nPackage = e.getPackage();
  77. this.nCode = e.getCode();
  78. this.szParam1 = e.getParam1();
  79. this.szParam2 = e.getParam2();
  80. this.szParam3 = e.getParam3();
  81. }
  82. /**
  83. * This is how Error objects are created. The first two parameters determine
  84. * the specific type of error. The other parameters give information about
  85. * the objects causing the error.
  86. * @param nPackage package where error code is defined.
  87. * @param nCode : the error code
  88. * @param szParam1 : a first parameter giving detailed information
  89. * @param szParam2 : a second parameter giving detailed information
  90. * @param szParam3 : a third parameter giving detailed information
  91. */
  92. public Error(
  93. int nPackage,
  94. int nCode,
  95. String szParam1,
  96. String szParam2,
  97. String szParam3) {
  98. this.nPackage = nPackage;
  99. this.nCode = nCode;
  100. this.szParam1 = szParam1;
  101. this.szParam2 = szParam2;
  102. this.szParam3 = szParam3;
  103. }
  104. /**
  105. *
  106. * @return the error code.
  107. */
  108. public int getCode() {
  109. return this.nCode;
  110. }
  111. /**
  112. *
  113. * @return the package where the error code is defined.
  114. */
  115. public int getPackage() {
  116. return this.nPackage;
  117. }
  118. /**
  119. *
  120. * @return first parameter describing error.
  121. */
  122. public String getParam1() {
  123. return this.szParam1;
  124. }
  125. /**
  126. *
  127. * @return second parameter describing error.
  128. */
  129. public String getParam2() {
  130. return this.szParam2;
  131. }
  132. /**
  133. *
  134. * @return third parameter describing error.
  135. */
  136. public String getParam3() {
  137. return this.szParam3;
  138. }
  139. /**
  140. *
  141. * @return String including all parameters describing error.
  142. */
  143. protected String parameters() {
  144. String sz = "(";
  145. if (this.getParam1() != null) {
  146. sz += this.getParam1();
  147. }
  148. sz += ",";
  149. if (this.getParam2() != null) {
  150. sz += this.getParam2();
  151. }
  152. sz += ",";
  153. if (this.getParam3() != null) {
  154. sz += this.getParam3();
  155. }
  156. sz += ")";
  157. return sz;
  158. }
  159. /**
  160. *
  161. * @return String describing this instance of Error.
  162. */
  163. public String toString() {
  164. return new Integer(this.nPackage).toString() + " " +
  165. new Integer(this.nCode).toString() +
  166. parameters();
  167. }
  168. }