X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.cms%2Fsrc%2Forg%2Fargeo%2Fcms%2Futil%2FLangUtils.java;h=67748ccd7121ce2cbae24c428f12b6840f55fde7;hb=HEAD;hp=0e214271d7a612642b35e0af3f1b971a56f89280;hpb=55870eba50d8b28e72a3102fd18a17a6f23f7bad;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.cms/src/org/argeo/cms/util/LangUtils.java b/org.argeo.cms/src/org/argeo/cms/util/LangUtils.java index 0e214271d..67748ccd7 100644 --- a/org.argeo.cms/src/org/argeo/cms/util/LangUtils.java +++ b/org.argeo.cms/src/org/argeo/cms/util/LangUtils.java @@ -12,13 +12,16 @@ import java.time.temporal.ChronoUnit; import java.time.temporal.Temporal; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.Dictionary; import java.util.Enumeration; import java.util.HashMap; import java.util.Hashtable; import java.util.Iterator; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import java.util.Properties; import javax.naming.InvalidNameException; @@ -27,37 +30,16 @@ import javax.naming.ldap.LdapName; /** Utilities around Java basic features. */ public class LangUtils { /* - * NON-API OSGi + * OBJECTS and STRINGS */ /** - * Returns an array with the names of the provided classes. Useful when - * registering services with multiple interfaces in OSGi. + * Whether this {@link String} is null, empty, or only white + * spaces. */ - public static String[] names(Class... clzz) { - String[] res = new String[clzz.length]; - for (int i = 0; i < clzz.length; i++) - res[i] = clzz[i].getName(); - return res; + public static boolean isEmpty(String str) { + return str == null || "".equals(str.strip()); } -// /* -// * MAP -// */ -// /** -// * Creates a new {@link Map} with one key-value pair. Key should not be null, -// * but if the value is null, it returns an empty {@link Map}. -// * -// * @deprecated Use {@link Collections#singletonMap(Object, Object)} instead. -// */ -// @Deprecated -// public static Map map(String key, Object value) { -// assert key != null; -// HashMap props = new HashMap<>(); -// if (value != null) -// props.put(key, value); -// return props; -// } - /* * DICTIONARY */ @@ -279,6 +261,24 @@ public class LangUtils { throw new IllegalArgumentException("Index " + index + " is not available (size is " + i + ")"); } + public static > Map sortByValue(Map map) { + return sortByValue(map, false); + } + + public static > Map sortByValue(Map map, boolean descending) { + List> list = new ArrayList<>(map.entrySet()); + list.sort(Entry.comparingByValue()); + if (descending) + Collections.reverse(list); + + Map result = new LinkedHashMap<>(); + for (Entry entry : list) { + result.put(entry.getKey(), entry.getValue()); + } + + return result; + } + /* * EXCEPTIONS */ @@ -323,6 +323,20 @@ public class LangUtils { return count > 1 ? count + " seconds" : count + " second"; } + /* + * NON-API OSGi + */ + /** + * Returns an array with the names of the provided classes. Useful when + * registering services with multiple interfaces in OSGi. + */ + public static String[] names(Class... clzz) { + String[] res = new String[clzz.length]; + for (int i = 0; i < clzz.length; i++) + res[i] = clzz[i].getName(); + return res; + } + /** Singleton constructor. */ private LangUtils() {