Extract util methods to separate classes

This commit is contained in:
AsamK 2018-11-18 10:45:26 +01:00
parent 4ab904b88e
commit 7443225d96
6 changed files with 168 additions and 131 deletions

View file

@ -1,10 +1,56 @@
package org.asamk.signal.util;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
public class Util {
public static File createTempFile() throws IOException {
return File.createTempFile("signal_tmp_", ".tmp");
private Util() {
}
public static String formatSafetyNumber(String digits) {
final int partCount = 12;
int partSize = digits.length() / partCount;
StringBuilder f = new StringBuilder(digits.length() + partCount);
for (int i = 0; i < partCount; i++) {
f.append(digits, i * partSize, (i * partSize) + partSize).append(" ");
}
return f.toString();
}
public static Map<String, String> getQueryMap(String query) {
String[] params = query.split("&");
Map<String, String> map = new HashMap<>();
for (String param : params) {
String name = null;
final String[] paramParts = param.split("=");
try {
name = URLDecoder.decode(paramParts[0], "utf-8");
} catch (UnsupportedEncodingException e) {
// Impossible
}
String value = null;
try {
value = URLDecoder.decode(paramParts[1], "utf-8");
} catch (UnsupportedEncodingException e) {
// Impossible
}
map.put(name, value);
}
return map;
}
public static String join(CharSequence separator, Iterable<? extends CharSequence> list) {
StringBuilder buf = new StringBuilder();
for (CharSequence str : list) {
if (buf.length() > 0) {
buf.append(separator);
}
buf.append(str);
}
return buf.toString();
}
}