001package co.codewizards.cloudstore.core.util;
002
003import java.net.URI;
004import java.net.URISyntaxException;
005import java.net.URL;
006import java.util.Collection;
007
008public final class Util {
009        private Util() { }
010
011        /**
012         * Convert an URL to an URI.
013         * @param url The URL to cenvert
014         * @return The URI
015         */
016        public static final URI urlToUri(final URL url) {
017                if (url == null)
018                        return null;
019
020                try {
021                        return new URI(url.getProtocol(), url.getAuthority(), url.getPath(), url.getQuery(), url.getRef());
022                } catch (URISyntaxException e) {
023                        // Since every URL is an URI, its transformation should never fail. But if it does, we rethrow.
024                        throw new RuntimeException(e);
025                }
026        }
027
028        /**
029         * Get the user name of the user who is currently authenticated at the operating system.
030         * This method simply calls <code>System.getProperty("user.name");</code>.
031         *
032         * @return the user name of the current operating system user.
033         */
034        public static final String getUserName()
035        {
036                return System.getProperty("user.name"); //$NON-NLS-1$
037        }
038
039        public static final <T> T assertNotNull(final String name, final T object) {
040                if (object == null)
041                        throw new IllegalArgumentException(String.format("%s == null", name));
042
043                return object;
044        }
045
046        public static final <T> T[] assertNotNullAndNoNullElement(final String name, final T[] array) {
047                assertNotNull(name, array);
048                for (int i = 0; i < array.length; i++) {
049                        if (array[i] == null)
050                                throw new IllegalArgumentException(String.format("%s[%s] == null", name, i));
051                }
052                return array;
053        }
054
055        public static final <E, T extends Collection<E>> T assertNotNullAndNoNullElement(final String name, final T collection) {
056                assertNotNull(name, collection);
057                int i = -1;
058                for (final E element : collection) {
059                        ++i;
060                        if (element == null)
061                                throw new IllegalArgumentException(String.format("%s[%s] == null", name, i));
062                }
063                return collection;
064        }
065
066        public static final boolean equal(final Object one, final Object two) {
067                return one == null ? two == null : one.equals(two);
068        }
069
070        public static final boolean equal(final long one, final long two) {
071                return one == two;
072        }
073
074        public static final boolean equal(final int one, final int two) {
075                return one == two;
076        }
077}