|
| 1 | +package io.github.raphaelrighetti.filmes.regex; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Collections; |
| 5 | +import java.util.List; |
| 6 | +import java.util.regex.Matcher; |
| 7 | +import java.util.regex.Pattern; |
| 8 | + |
| 9 | +public class JsonMatcher { |
| 10 | + |
| 11 | + private List<String> filmes = new ArrayList<>(); |
| 12 | + private List<String> titles = new ArrayList<>(); |
| 13 | + private List<String> urlImages = new ArrayList<>(); |
| 14 | + private List<String> years = new ArrayList<>(); |
| 15 | + private List<String> ranks = new ArrayList<>(); |
| 16 | + |
| 17 | + public JsonMatcher(String json) { |
| 18 | + checaStringNull(json); |
| 19 | + json = formataJson(json); |
| 20 | + preencheTudo(json); |
| 21 | + } |
| 22 | + |
| 23 | + public List<String> getFilmes() { |
| 24 | + checaListaVazia(filmes); |
| 25 | + return Collections.unmodifiableList(filmes); |
| 26 | + } |
| 27 | + |
| 28 | + public List<String> getTitles() { |
| 29 | + checaListaVazia(titles); |
| 30 | + return Collections.unmodifiableList(titles); |
| 31 | + } |
| 32 | + |
| 33 | + public List<String> getUrlImages() { |
| 34 | + checaListaVazia(urlImages); |
| 35 | + return Collections.unmodifiableList(urlImages); |
| 36 | + } |
| 37 | + |
| 38 | + public List<String> getYears() { |
| 39 | + checaListaVazia(years); |
| 40 | + return Collections.unmodifiableList(years); |
| 41 | + } |
| 42 | + |
| 43 | + public List<String> getRanks() { |
| 44 | + checaListaVazia(ranks); |
| 45 | + return Collections.unmodifiableList(ranks); |
| 46 | + } |
| 47 | + |
| 48 | + private void preencheFilmes(String json) { |
| 49 | + Matcher matcher = Pattern.compile("(\\{[^{}]*})").matcher(json); |
| 50 | + while (matcher.find()) { |
| 51 | + filmes.add(matcher.group()); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private void preencheTitles(String json) { |
| 56 | + Matcher matcher = Pattern.compile("\"title\":\"([^\"]*)\"").matcher(json); |
| 57 | + while (matcher.find()) { |
| 58 | + titles.add(matcher.group()); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private void preencheUrlImages(String json) { |
| 63 | + Matcher matcher = Pattern.compile("\"image\":\"([^\"]*)\"").matcher(json); |
| 64 | + while (matcher.find()) { |
| 65 | + urlImages.add(matcher.group()); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private void preencheYears(String json) { |
| 70 | + Matcher matcher = Pattern.compile("\"year\":\"([^\"]*)\"").matcher(json); |
| 71 | + while (matcher.find()) { |
| 72 | + years.add(matcher.group()); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private void preencheRanks(String json) { |
| 77 | + Matcher matcher = Pattern.compile("\"rank\":\"([^\"]*)\"").matcher(json); |
| 78 | + while (matcher.find()) { |
| 79 | + ranks.add(matcher.group()); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private void preencheTudo(String json) { |
| 84 | + preencheFilmes(json); |
| 85 | + preencheTitles(json); |
| 86 | + preencheUrlImages(json); |
| 87 | + preencheYears(json); |
| 88 | + preencheRanks(json); |
| 89 | + } |
| 90 | + |
| 91 | + private void checaStringNull(String json) { |
| 92 | + if (json == null) { |
| 93 | + throw new NullPointerException("É necessário passar os dados!"); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private void checaListaVazia(List<String> lista) { |
| 98 | + if (lista.isEmpty()) { |
| 99 | + throw new NullPointerException("A lista está vazia!"); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private String formataJson(String json) { |
| 104 | + Matcher matcherStart = Pattern.compile("\\{\"items\":\\[").matcher(json); |
| 105 | + String resultWithoutStart = matcherStart.replaceAll(""); |
| 106 | + |
| 107 | + Matcher matcherEnd = Pattern.compile("],\"errorMessage\":\"\"}").matcher(resultWithoutStart); |
| 108 | + String result = matcherEnd.replaceAll(""); |
| 109 | + |
| 110 | + return result; |
| 111 | + } |
| 112 | +} |
0 commit comments