RegistroBase.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package API;
  7. import java.util.ArrayList;
  8. import java.util.HashMap;
  9. import java.util.LinkedHashMap;
  10. import java.util.Map;
  11. import template.TemplateCtx;
  12. /**
  13. *
  14. * @author Eugenio
  15. */
  16. public class RegistroBase implements template.TemplateCtx {
  17. protected HashMap<String, String> attr;
  18. protected HashMap<String, ArrayList<String>> lists;
  19. public Instruction parent;
  20. public Instruction next;
  21. public Instruction prev;
  22. public HashMap<String, ArrayList<String>> GetLists() {
  23. return lists;
  24. }
  25. public RegistroBase() {
  26. lists = new LinkedHashMap<>();
  27. attr = new LinkedHashMap<>();
  28. }
  29. public void addList(String nome, ArrayList<String> lista) {
  30. lists.put(nome, lista);
  31. }
  32. public boolean existeLista(String nome) {
  33. return lists.containsKey(nome);
  34. }
  35. public void addInList(String nome, String value) {
  36. getList(nome).add(value);
  37. }
  38. public ArrayList<String> getList(String nome) {
  39. if (!existeLista(nome)) {
  40. lists.put(nome, new ArrayList<String>());
  41. }
  42. return lists.get(nome);
  43. }
  44. public void setParent(Instruction n) {
  45. parent = n;
  46. }
  47. public String G(String attrib) {
  48. return attr.containsKey(attrib) ? attr.get(attrib) : "";
  49. }
  50. public String get(String attrib, String orValue) {
  51. String val = G(attrib);
  52. return (val.equals("")) ? orValue : val;
  53. }
  54. public String format(String f) {
  55. String value = "";
  56. for (Map.Entry<String, String> entry : this.getProps().entrySet()) {
  57. value = entry.getValue() != null ? entry.getValue() : "NULL";
  58. f = f.replaceAll("\\{" + entry.getKey() + "\\}", value);
  59. }
  60. return f;
  61. }
  62. @Override
  63. public TemplateCtx Set(String id, String value) {
  64. S(id, value);
  65. return this;
  66. }
  67. public RegistroBase S(String attrib, String value) {
  68. attr.put(attrib, value);
  69. return this;
  70. }
  71. public RegistroBase Replace(String attrib, String regex, String value) {
  72. // attr.put(attrib, value);
  73. if (Has(attrib)) {
  74. S(attrib, G(attrib).replaceAll(regex, value));
  75. }
  76. return this;
  77. }
  78. public RegistroBase R(String attributes) {
  79. String[] attrs = attributes.split(",");
  80. for (String a1 : attrs) {
  81. if (this.attr.containsKey(a1)) {
  82. this.attr.remove(a1);
  83. }
  84. }
  85. return this;
  86. }
  87. public boolean testAndSet(String atributo, String cmp, String valor) {
  88. if (eq(atributo, cmp)) {
  89. S(atributo, valor);
  90. return true;
  91. }
  92. return false;
  93. }
  94. public boolean contem(String atributo, String part) {
  95. if (!Has(atributo)) {
  96. return false;
  97. }
  98. return G(atributo).contains(part);
  99. }
  100. @Override
  101. public String toString() {
  102. String reg = "";
  103. for (Map.Entry<String, String> entry : attr.entrySet()) {
  104. String string = entry.getKey();
  105. String string1 = entry.getValue();
  106. reg += "\t" + string + ":" + string1 + ",\n";
  107. }
  108. return ":\n" + reg;
  109. }
  110. public void somar(String atributo, int i) {
  111. if (!Has(atributo)) {
  112. set(atributo, i);
  113. } else {
  114. try {
  115. int nu = getInt(atributo);
  116. set(atributo, nu + i);
  117. } catch (Exception e) {
  118. e.printStackTrace();
  119. }
  120. }
  121. }
  122. public RegistroBase set(String attrib, Long valor) {
  123. S(attrib, "" + valor);
  124. return this;
  125. }
  126. public RegistroBase set(String attrib, int valor) {
  127. S(attrib, "" + valor);
  128. return this;
  129. }
  130. public String getText() {
  131. return G("value");
  132. }
  133. public String getRegistroClass() {
  134. return G("class");
  135. }
  136. public String getValorOuVazio(String string) {
  137. return (attr.containsKey(string)) ? attr.get(string) : "";
  138. }
  139. public boolean Has(String at) {
  140. return attr.containsKey(at);
  141. }
  142. public HashMap<String, String> getProps() {
  143. return attr;
  144. }
  145. public void del(String atrib) {
  146. attr.remove(atrib);
  147. }
  148. public int getInt(String atributo) throws Exception {
  149. String l = "";
  150. try {
  151. l = G(atributo);
  152. return Integer.parseInt(l);
  153. } catch (Exception e) {
  154. throw new Exception("[ID:" + G("id") + "]O atributo {" + atributo + ":'" + l + "'} não é inteiro.");
  155. }
  156. }
  157. public long getLong(String atributo) throws Exception {
  158. String l = "";
  159. try {
  160. l = G(atributo);
  161. return Long.parseLong(l);
  162. } catch (Exception e) {
  163. throw new Exception("[ID:" + G("id") + "]O atributo {" + atributo + ":'" + l + "'} não é long.");
  164. }
  165. }
  166. public boolean eq(String attrib, String value) {
  167. if (!attr.containsKey(attrib)) {
  168. return false;
  169. }
  170. return attr.get(attrib).equals(value);
  171. }
  172. public boolean in(String atributo, String[] valores) {
  173. if (!attr.containsKey(atributo)) {
  174. return false;
  175. }
  176. boolean in = false;
  177. for (String valor : valores) {
  178. if (attr.get(atributo).equals(valor)) {
  179. in = true;
  180. break;
  181. }
  182. }
  183. return in;
  184. }
  185. public boolean diferente(String atributo, String valor) {
  186. return !attr.get(atributo).equals(valor);
  187. }
  188. /**
  189. * Verifica Se um dado atributo é numerico.
  190. *
  191. * @param atributo
  192. * @return
  193. */
  194. public boolean isNumber(String atributo) {
  195. try {
  196. Integer.parseInt(G(atributo).replaceAll("\\s*", ""));
  197. return true;
  198. } catch (Exception e) {
  199. return false;
  200. }
  201. }
  202. /**
  203. * Copia o dado do regiStro r com atributio Fonte para regiStro atual
  204. * mantendo o nome do atributo
  205. *
  206. * @param atribs Fonte
  207. * @param r
  208. */
  209. public void copiar(String atrib, RegistroBase r) {
  210. S(atrib, r.G(atrib));
  211. }
  212. /**
  213. * Copia o dado do regiStro r com atributio Fonte para o atributo deStino no
  214. * regiStro atual
  215. *
  216. * @param atribs Fonte
  217. * @param atribd DeStino
  218. * @param r
  219. */
  220. public void copiar(String atribs, String atribd, RegistroBase r) {
  221. S(atribd, r.G(atribs));
  222. }
  223. public RegistroBase copy() {
  224. RegistroBase rb = new RegistroBase();
  225. for (Map.Entry<String, String> entry : attr.entrySet()) {
  226. rb.S(entry.getKey(), entry.getValue());
  227. }
  228. return rb;
  229. }
  230. }