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

io.dataspray.singletable.StringSerdeUtil Maven / Gradle / Ivy

Go to download

DynamoDB best practices encourages a single-table design that allows multiple record types to reside within the same table. The goal of this library is to improve the experience of Java developers and make it safer to define non-conflicting schema of each record, serializing and deserializing automatically and working with secondary indexes.

The newest version!
// SPDX-FileCopyrightText: 2019-2022 Matus Faro 
// SPDX-License-Identifier: Apache-2.0
package io.dataspray.singletable;

import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

@Slf4j
public class StringSerdeUtil {

    public final static char DELIMITER = ':';
    public final static char ESCAPER = '\\';

    private StringSerdeUtil() {
        // disable ctor
    }

    public static String mergeStrings(String... ss) {
        if (ss == null || ss.length == 0) {
            return null;
        }
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < ss.length; i++) {
            String s = ss[i];
            for (int j = 0; j < s.length(); j++) {
                char c = s.charAt(j);
                switch (c) {
                    case ESCAPER:
                        result.append(ESCAPER).append(ESCAPER);
                        break;
                    case DELIMITER:
                        result.append(ESCAPER).append(DELIMITER);
                        break;
                    default:
                        result.append(c);
                        break;
                }
            }
            if (i + 1 < ss.length) {
                result.append(DELIMITER);
            }
        }
        return result.toString();
    }

    public static String[] unMergeString(String s) {
        if (s == null) {
            return new String[0];
        }
        List results = Lists.newArrayList();
        StringBuilder result = new StringBuilder();
        boolean nextCharEscaped = false;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (nextCharEscaped) {
                nextCharEscaped = false;
                result.append(c);
                continue;
            }
            switch (c) {
                case ESCAPER:
                    nextCharEscaped = true;
                    break;
                case DELIMITER:
                    results.add(result.toString());
                    result = new StringBuilder();
                    break;
                default:
                    result.append(c);
                    break;
            }
        }
        results.add(result.toString());
        return results.toArray(new String[0]);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy