summaryrefslogtreecommitdiff
path: root/support/texplate/source/main/java/org/islandoftex/texplate/model/handlers/CSVListHandler.java
blob: 4b3d0c2e694c90931046ae301d03a76e5efb9937 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// SPDX-License-Identifier: BSD-3-Clause
package org.islandoftex.texplate.model.handlers;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * Implements a CSV list handler.
 *
 * @version 1.0
 * @since 1.0
 */
public class CSVListHandler implements Handler {

    /**
     * Applies the conversion to the string.
     *
     * @param string The string.
     * @return A list.
     */
    @Override
    public Object apply(String string) {
        return Arrays.stream(string.split(",(?=(?:[^\"]*\""
                + "[^\"]*\")*[^\"]*$)"))
                .map(String::trim)
                .filter(s -> !s.isEmpty())
                .collect(Collectors.toList());
    }

}