Skip to content

Commit

Permalink
Added Path.substr(beginIndex), added StringUtil to increment number s…
Browse files Browse the repository at this point in the history
…uffix on string.
  • Loading branch information
Aklakan committed Aug 6, 2024
1 parent 50def81 commit c7fc1e7
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public interface Path<T>

Path<T> subpath(int beginIndex, int endIndex);

/** Experimental. Not part of nio Path.*/
Path<T> subpath(int beginIndex);

// default Path<T> subpath(int beginIndex) {
// return subpath(beginIndex, getNameCount());
// }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ public P subpath(int beginIndex, int endIndex) {
return newPath(false, segments.subList(beginIndex, endIndex));
}

@Override
public P subpath(int beginIndex) {
int endIndex = getNameCount();
return subpath(beginIndex, endIndex);
}

@Override
public boolean startsWith(Path<T> other) {
boolean result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ public Path<String> subpath(int beginIndex, int endIndex) {
return wrapInternal(getDelegate().subpath(beginIndex, endIndex));
}

@Override
public Path<String> subpath(int beginIndex) {
int endIndex = getNameCount();
return subpath(beginIndex, endIndex);
}

@Override
public boolean startsWith(Path<String> other) {
return getDelegate().startsWith(((PathNio)other).getDelegate());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ public F subpath(int beginIndex, int endIndex) {
return wrapOrNull(getDelegate().subpath(beginIndex, endIndex));
}

@Override
public F subpath(int beginIndex) {
return wrapOrNull(getDelegate().subpath(beginIndex));
}

@Override
public boolean startsWith(Path<T> other) {
return getDelegate().startsWith(unwrap(other));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ default void validateBuildArg(TupleBridge<?, ?> bridge) {
}
}

/** If the dimension is 1 then return the component, otherwise the tuple. */
// FIXME What's the best name for this method? compact?
default Object compact(D domainObject) {
Object result = getDimension() == 1
? get(domainObject, 0)
: domainObject;
return result;
}

/** If dimension == 1 then return the object as is, otherwise interpret it as a domain tuple. */
// default Object fromObject(Object componentOrTuple) {
//
// }

default C[] toComponentArray(D domainObject) {
int len = getDimension();
@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.aksw.commons.util.string;

import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.MessageDigest;
Expand All @@ -13,6 +14,7 @@
import java.util.NavigableSet;
import java.util.SortedMap;
import java.util.TreeSet;
import java.util.function.Predicate;

public class StringUtils
{
Expand Down Expand Up @@ -626,4 +628,56 @@ public static String md5Hash(String string) {
return md5Hash(string.getBytes());
}


/**
* Return the substring of a string that only consists of digits.
* <p>
* Examples:
* <pre>
* "abc123" -&gt; "123"
* "abc" -&gt; ""
* "abc123.456" -&gt; "456"
* </pre>
*/
public static String numberSuffix(String base) {
int l = base.length();
int i;
for (i = l - 1; i >= 0; --i) {
char c = base.charAt(i);
if (!Character.isDigit(c)) {
break;
}
}
String result = base.substring(i + 1, l);
return result;
}

public static String allocateName(String base, boolean forceNumberSuffix, Predicate<String> skip) {
String result = null;
if (!forceNumberSuffix) {
if (!skip.test(base)) {
result = base;
}
}

if (result == null) {
String numberStr = numberSuffix(base);
String prefix = base.substring(0, base.length() - numberStr.length());

BigInteger current = numberStr.isEmpty()
? BigInteger.valueOf(0)
: new BigInteger(numberStr);

BigInteger one = BigInteger.valueOf(1);

while (true) {
current = current.add(one);
result = prefix + current;
if (!skip.test(result)) {
break;
}
}
}
return result;
}
}

0 comments on commit c7fc1e7

Please sign in to comment.