add methods: maskAndTruncate; nCopies; nCopiesWithGlue

This commit is contained in:
2025-01-23 09:32:46 -06:00
parent 6725704b13
commit 42a8d37493
2 changed files with 128 additions and 0 deletions

View File

@ -23,6 +23,7 @@ package com.kingsrook.qqq.backend.core.utils;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -475,4 +476,59 @@ public class StringUtils
return (s);
}
/*******************************************************************************
**
*******************************************************************************/
public static String maskAndTruncate(String value)
{
return (maskAndTruncate(value, "** MASKED **", 6, 4));
}
/*******************************************************************************
**
*******************************************************************************/
public static String maskAndTruncate(String value, String mask, int minLengthToMask, int charsToShowOnEnds)
{
if(!hasContent(value))
{
return ("");
}
if(value.length() < minLengthToMask || value.length() < 2 * charsToShowOnEnds)
{
return mask;
}
if(value.length() < charsToShowOnEnds * 3)
{
return (value.substring(0, charsToShowOnEnds) + mask);
}
return (value.substring(0, charsToShowOnEnds) + mask + value.substring(value.length() - charsToShowOnEnds));
}
/***************************************************************************
**
***************************************************************************/
public static String nCopies(int n, String s)
{
return (nCopiesWithGlue(n, s, ""));
}
/***************************************************************************
**
***************************************************************************/
public static String nCopiesWithGlue(int n, String s, String glue)
{
return (StringUtils.join(glue, Collections.nCopies(n, s)));
}
}