Remove dependency to OSGi user admin from API
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / util / LangUtils.java
index 0e214271d7a612642b35e0af3f1b971a56f89280..67748ccd7121ce2cbae24c428f12b6840f55fde7 100644 (file)
@@ -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 <code>null</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<String, Object> map(String key, Object value) {
-//             assert key != null;
-//             HashMap<String, Object> 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 <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map) {
+               return sortByValue(map, false);
+       }
+
+       public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map, boolean descending) {
+               List<Entry<K, V>> list = new ArrayList<>(map.entrySet());
+               list.sort(Entry.comparingByValue());
+               if (descending)
+                       Collections.reverse(list);
+
+               Map<K, V> result = new LinkedHashMap<>();
+               for (Entry<K, V> 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() {