|
| 1 | +package org.polars.scala.polars.api; |
| 2 | + |
| 3 | +import scala.Boolean; |
| 4 | +import scala.Int; |
| 5 | +import scala.jdk.javaapi.CollectionConverters; |
| 6 | + |
| 7 | +import java.time.LocalDate; |
| 8 | +import java.time.LocalDateTime; |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.Arrays; |
| 11 | +import java.util.Iterator; |
| 12 | +import java.util.List; |
| 13 | +import java.util.stream.Collectors; |
| 14 | +import java.util.stream.StreamSupport; |
| 15 | + |
| 16 | +class JSeries { |
| 17 | + final static String EmptyString = ""; |
| 18 | + |
| 19 | + static Series ofList(String name, Iterable<Iterable> values) { |
| 20 | + Iterator<Iterable> valuesIter = values.iterator(); |
| 21 | + List<Series> sList = new ArrayList<>(); |
| 22 | + |
| 23 | + while (valuesIter.hasNext()) { |
| 24 | + Iterable subList = valuesIter.next(); |
| 25 | + Object head = subList.iterator().next(); |
| 26 | + |
| 27 | + Series thisSeries; |
| 28 | + if (head instanceof Integer || head instanceof Int) { |
| 29 | + thisSeries = Series.ofInt(EmptyString, subList); |
| 30 | + } else if (head instanceof Long) { |
| 31 | + thisSeries = Series.ofLong(EmptyString, subList); |
| 32 | + } else if (head instanceof Float) { |
| 33 | + thisSeries = Series.ofFloat(EmptyString, subList); |
| 34 | + } else if (head instanceof Double) { |
| 35 | + thisSeries = Series.ofDouble(EmptyString, subList); |
| 36 | + } else if (head instanceof Boolean) { |
| 37 | + thisSeries = Series.ofBoolean(EmptyString, subList); |
| 38 | + } else if (head instanceof LocalDate) { |
| 39 | + thisSeries = Series.ofDate(EmptyString, subList); |
| 40 | + } else if (head instanceof LocalDateTime) { |
| 41 | + thisSeries = Series.ofDateTime(EmptyString, subList); |
| 42 | + } else if (head instanceof String) { |
| 43 | + thisSeries = Series.ofString(EmptyString, subList); |
| 44 | + } else if (head instanceof java.lang.Iterable) { |
| 45 | + thisSeries = ofList(EmptyString, subList); |
| 46 | + } else if (head instanceof scala.collection.Iterable) { |
| 47 | + Iterable<Iterable> s = (Iterable<Iterable>) StreamSupport.stream(subList.spliterator(), false) |
| 48 | + .map(v -> CollectionConverters.asJava((scala.collection.Iterable) v)) |
| 49 | + .collect(Collectors.toList()); |
| 50 | + |
| 51 | + thisSeries = ofList(EmptyString, s); |
| 52 | + } else if (head.getClass().isArray()) { |
| 53 | + Iterable<Iterable> s = (Iterable<Iterable>) StreamSupport.stream(subList.spliterator(), false) |
| 54 | + .map(v -> Arrays.asList((Object[]) v)) |
| 55 | + .collect(Collectors.toList()); |
| 56 | + |
| 57 | + thisSeries = ofList(EmptyString, s); |
| 58 | + } else { |
| 59 | + throw new IllegalArgumentException( |
| 60 | + String.format("Nested series of provided internal type `%s` is currently not supported.", head.getClass().getSimpleName()) |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + sList.add(thisSeries); |
| 65 | + } |
| 66 | + |
| 67 | + return Series.ofSeries(name, sList); |
| 68 | + } |
| 69 | +} |
0 commit comments