All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.dimajix.common.text.ParserUtils Maven / Gradle / Ivy

There is a newer version: 1.2.0-synapse3.3-spark3.3-hadoop3.3
Show newest version
/*
 * Copyright (C) 2020 The Flowman Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.dimajix.common.text;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import com.google.common.collect.Maps;
import lombok.val;


public final class ParserUtils {
    private ParserUtils() {
    }

    static public List parseDelimitedList(String list)  {
        return Arrays.stream(list.split(",")).map(String::trim).filter(s -> !s.isEmpty()).collect(Collectors.toList());
    }

    static public Map parseDelimitedKeyValues(String list) {
        return Arrays.stream(list.split(","))
            .map(String::trim)
            .filter(p -> p.contains("="))
            .map(p -> splitSetting(p))
            .filter(p -> !p.getKey().isEmpty())
            .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
    }


    static public Map splitSettings(String[] settings) {
        return splitSettings(Arrays.stream(settings));
    }

    static public Map splitSettings(Iterable settings) {
        val stream = StreamSupport.stream(settings.spliterator(),false);
        return splitSettings(stream);
    }
    static Map splitSettings(Stream settings) {
        return settings
            .map(ParserUtils::splitSetting)
            .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
    }

    static public Map.Entry splitSetting(String setting) {
        val sep = setting.indexOf('=');
        if (sep < 0) {
            return Maps.immutableEntry(setting, "");
        }
        else {
            val key = setting.substring(0, sep).trim();
            val value = setting.substring(sep + 1).trim().replaceAll("^\"|\"$", "");
            return Maps.immutableEntry(key, value);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy