Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. public class Find {
  4.  
  5. private String text;
  6.  
  7. public Find(String text) {
  8. this.text = text;
  9. }
  10.  
  11. // En este match recibimos el String llamado StringPat y lo pasamos a atoms. A continuación, reviso si está vacío
  12. // y llamo a la función match que recibe un ArrayList de atoms.
  13. public boolean match(String stringPat) {
  14. ArrayList<Atom> atoms = Atom.getAtoms(stringPat);
  15. if (atoms.isEmpty()) return false;
  16. return match(atoms);
  17. }
  18.  
  19. private boolean match(ArrayList<Atom> atoms) {
  20. // Declaro un boolean que me indicará si los carácteres harán o no match.
  21. boolean matches = false;
  22. // Declaro un int que me indicará las posiciones que recorreremos del String text. Ya que si nos encontramos un
  23. // AT_START basta recorrerlo una vez.
  24. int finalPos = (atoms.get(0).getAtomType().equals(Atom.AtomType.AT_START) ? 1 : text.length());
  25. int initialPos = 0;
  26. Character currentTextChar;
  27. // Si la posición inicial es menor que la final y la dimensión del ArrayList sea menor o igual a la resta de la dimensión del texto
  28. // y la posición inicial, realizaremos los checks.
  29. while (initialPos < finalPos && atoms.size() <= text.length() - initialPos) {
  30. // Cuando intentamos hacer match siempre pensaremos que se hace, y ya si no se puede lo cambiaremos a false.
  31. matches = true;
  32. // Guardamos la posición en currentPos y inicio un for each que recorrerá los atoms.
  33. int currentPos = initialPos;
  34. for (Atom atom : atoms) {
  35. // Guardo la posición del texto actual para poder compararla.
  36. currentTextChar = text.charAt(currentPos);
  37. // Si el atom actual es de tipo AT_START, LIST o CHARACTER, comparamos si son iguales llamando a matches.
  38. // Si se da, incremento la posición actual y nos saltamos el resto del código.
  39. if (atom.getAtomType().equals(Atom.AtomType.AT_START) || (atom.getAtomType().equals(Atom.AtomType.LIST)) || (atom.getAtomType().equals(Atom.AtomType.CHARACTER))) {
  40. if (atom.matches(currentTextChar)) {
  41. currentPos++;
  42. continue;
  43. }
  44. // Si no se da, cambiamos el boolean a false y salimos del bucle.
  45. matches = false;
  46. break;
  47. }
  48. // Si es un AT_END, revisamos si son iguales y además si están en la última posición.
  49. // Si se da, salimos del código, sino también pero con el boolean en false.
  50. else if (atom.getAtomType().equals(Atom.AtomType.AT_END)) {
  51. if (atom.matches(currentTextChar) && currentPos == text.length() - 1) {
  52. break;
  53. }
  54. matches = false;
  55. break;
  56. }
  57. // Si el atom es de tipo ALL, basta incrementar la posición actual.
  58. else if (atom.getAtomType().equals(Atom.AtomType.ALL)) {
  59. currentPos++;
  60. }
  61. //TODO
  62. else if (atom.getAtomType().equals(Atom.AtomType.CLOSURE_AST)) {
  63. ArrayList<Atom> currentsAtoms = new ArrayList<>();
  64. for (int i = currentPos + 1; i < atoms.size(); i++) {
  65. currentsAtoms.add(atoms.get(i));
  66. }
  67. for (int i = 0; i < text.length(); i++) {
  68. text = text.substring(currentPos);
  69. Find find = new Find(text);
  70. }
  71. }
  72. }
  73. // Una vez fuera del bucle for each, revisamos si el boolean si es true, salimos del while, sino incrementamos la posición inicial.
  74. if (matches) break;
  75. else initialPos++;
  76. }
  77. return matches;
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement