Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.30 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3.  
  4. public class Atom {
  5. // Declaro el constructor de Atom.
  6. public Atom(AtomType at, ArrayList<Character> chars) {
  7. this.atomType = at;
  8. this.matchingChars = chars;
  9. }
  10.  
  11. // Declaro los atributos que tendrá un Atom.
  12. private AtomType atomType;
  13. private ArrayList<Character> matchingChars;
  14.  
  15.  
  16. // Declaro los getters de los atributos de Atom.
  17. public ArrayList<Character> getMatchingChars() {
  18. return matchingChars;
  19. }
  20.  
  21. public AtomType getAtomType() {
  22. return atomType;
  23. }
  24.  
  25. // matches() revisará si el atom y el caracter del texto de ese momento son iguales.
  26. public boolean matches(Character currentAtom) {
  27. for (Character c : matchingChars) {
  28. if (c.equals(currentAtom)) return true;
  29. }
  30. return false;
  31. }
  32.  
  33. // Este es el enum que me indicará que tipo de Atom tenemos en cada momento.
  34. enum AtomType {
  35. LIST, AT_START, AT_END, CLOSURE, ALL, CHARACTER
  36. }
  37.  
  38. // A partir de un String, devolveré un ArrayList de Atoms.
  39. static ArrayList<Atom> getAtoms(String s) {
  40. // Declaro un ArraList de tokens y un char que me indicará el Atom actual.
  41. ArrayList<Atom> atoms = new ArrayList<>();
  42. char currentAtom;
  43. for (int i = 0; i < s.length(); i++) {
  44. // Añadimos en currentAtom cada valor del String.
  45. currentAtom = s.charAt(i);
  46. // Revisamos si nos encontramos un '%' y si es la primera vez que pasamos.
  47. if (currentAtom == '%' && i == 0) {
  48. // Guardamos el valor siguiente en currentAtom ya que necesitamos guardar el valor siguiente.
  49. currentAtom = s.charAt(++i);
  50. // Añadimos al ArrayList un nuevo Atom de tipo AT_START, y añado en un arrayList el currentAtom.
  51. atoms.add(new Atom(AtomType.AT_START, newArrayListWithChars(currentAtom)));
  52. }
  53. // Realizamos la misma estructura para el resto de metacarácteres.
  54. else if (currentAtom == '@') {
  55. currentAtom = s.charAt(++i);
  56. atoms.add(new Atom(AtomType.CHARACTER, newArrayListWithChars(currentAtom)));
  57. } else if (currentAtom == '?') {
  58. // En este caso no hace falta pasarle un character.
  59. atoms.add(new Atom(AtomType.ALL, null));
  60. } else if (currentAtom == '[') {
  61. // Guardamos el valor de la posición del caracter ']'
  62. // y pasamos a la función buildList el substring de '[' a ']'.
  63. int endOfListPos = s.indexOf(']', i);
  64. atoms.add(new Atom(AtomType.LIST, buildList(s.substring(i + 1, endOfListPos))));
  65. // Por último pasamos la i a la posición de endOfListPos.
  66. i = endOfListPos;
  67. } else if (currentAtom == '+') {
  68. atoms.add(new Atom(AtomType.CLOSURE, atoms.get(atoms.size() - 1).getMatchingChars()));
  69. } else if (currentAtom == '*') {
  70. currentAtom = s.charAt(i - 1);
  71. atoms.remove(atoms.size() - 1);
  72. atoms.add(new Atom(AtomType.CLOSURE, newArrayListWithChars(currentAtom)));
  73. /* atoms.add(new Atom(AtomType.CLOSURE, atoms.get(atoms.size() - 1).getMatchingChars()));
  74. atoms.remove(atoms.size() - 2);
  75. */
  76. }
  77. // Si el valor que nos encontramos es un dolar y está en la última posición del String, será un Atom AT_END.
  78. else if (currentAtom == '$' && i == s.length() - 1) {
  79. // Guardamos en currentAtom el valor anterior del String.
  80. currentAtom = s.charAt(i - 1);
  81. // Eliminamos el atom anterior y los guardamos otra vez siendo un AT_END.
  82. atoms.remove(atoms.size() - 1);
  83. atoms.add(new Atom(AtomType.AT_END, newArrayListWithChars(currentAtom)));
  84. } else {
  85. // Si no entrá en el resto de if, será un carácter.
  86. atoms.add(new Atom(AtomType.CHARACTER, newArrayListWithChars(currentAtom)));
  87. }
  88. }
  89. return atoms;
  90. }
  91.  
  92. // Está función me devolverá un arrayList de caracteres con el valor de currentChar añadido.
  93. private static ArrayList<Character> newArrayListWithChars(char currentChar) {
  94. ArrayList<Character> result = new ArrayList<>();
  95. result.add(currentChar);
  96. return result;
  97. }
  98.  
  99.  
  100. @Override
  101. public String toString() {
  102. return String.format("AtomType: %s, Chars: %s", this.atomType, this.matchingChars);
  103. }
  104.  
  105. // BuildList generará un ArrayList de caracteres que nos indiquen en el rango ([ ]).
  106. private static ArrayList<Character> buildList(String s) {
  107. // Declaro un ArrayList donde añadiré los carácteres y un int llamado hyphenPos, que me indicará la posición del guón.
  108. ArrayList<Character> result = new ArrayList<>();
  109. int hyphenPos;
  110. // Este bucle me indica si el String tiene (uno o muchos) guiones.
  111. while ((hyphenPos = s.indexOf('-')) != -1) {
  112. // Si tiene guión, llamo a la funcion addCharSequence y le paso los valores que se encuentran a la izquierda
  113. // y a la derecha del guión.
  114. result = addCharSequence(s.charAt(hyphenPos - 1), s.charAt(hyphenPos + 1));
  115. // Una vez rellenado, sobreescribo el string quitandole los dos valores de alrededor (guión incluido).
  116. s = s.substring(0, hyphenPos - 1) + s.substring(hyphenPos + 2);
  117. }
  118. // Si no quedan guiones, añado cada valor que quede directamente.
  119. for (char c : s.toCharArray()) {
  120. result.add(c);
  121. }
  122. return result;
  123. }
  124.  
  125. // Esta función añade en un ArrayList todos los valores que se encuentran entre cada uno de los caracteres.
  126. private static ArrayList<Character> addCharSequence(int firstChar, int lastChar) {
  127. ArrayList<Character> result = new ArrayList<>();
  128. // Mientras el segundo valor sea mayor o igual al primero, añadiremos dicho valor al ArrayList y lo decrementaremos.
  129. while (lastChar >= firstChar) {
  130. result.add((char) lastChar);
  131. lastChar--;
  132. }
  133. // Por estetica, Ordeno el ArrayList por ordén alfabético y lo devuelvo.
  134. Collections.sort(result);
  135. return result;
  136. }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement