RegistroBase.java 7.5 KB

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