RegistroBase.java 7.3 KB

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