1 package eu.ehri.project.utils; 2 3 import com.google.common.collect.Lists; 4 5 import java.util.Collections; 6 import java.util.List; 7 import java.util.stream.Collectors; 8 9 public class Table { 10 private final List<List<String>> data; 11 private final List<String> headers; 12 13 private Table(List<List<String>> data, List<String> headers) { 14 this.data = data; 15 this.headers = headers; 16 } 17 18 public static Table of(List<List<String>> data, List<String> headers) { 19 return new Table(data, headers); 20 } 21 22 public static Table of(List<List<String>> data) { 23 return of(data, null); 24 } 25 26 public boolean hasHeaders() { 27 return headers != null && !headers.isEmpty(); 28 } 29 30 public List<List<String>> rows() { 31 return data; 32 } 33 34 public List<String> headers() { 35 return headers; 36 } 37 38 @Override 39 public String toString() { 40 return data.toString(); 41 } 42 43 public boolean contains(String... values) { 44 return data.contains(Lists.newArrayList(values)); 45 } 46 47 public List<String> column(int num) { 48 if (data.isEmpty()) { 49 return Collections.emptyList(); 50 } else { 51 if (data.get(0).size() < num + 1) { 52 throw new IllegalArgumentException("Column " + num + " does not exist"); 53 } 54 return data 55 .stream() 56 .map(row -> row.size() < num + 1 ? "" : row.get(num)) 57 .collect(Collectors.toList()); 58 } 59 } 60 }