001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.gwt.client.util;
029
030import java.util.Collection;
031import java.util.HashMap;
032import java.util.HashSet;
033import java.util.Map;
034import java.util.Set;
035
036/**
037 * A utility class with functions for dealing with maps.<p>
038 *
039 * @since 8.0.0
040 */
041public final class CmsClientCollectionUtil {
042
043    /**
044     * Hide default constructor.<p>
045     */
046    private CmsClientCollectionUtil() {
047
048        // do nothing
049    }
050
051    /**
052     * Returns the intersection of two sets without modifying the original sets.<p>
053     *
054     * @param <A> the type of objects contained in the sets
055     *
056     * @param first the first set
057     * @param second the second set
058     *
059     * @return the intersection of both sets
060     */
061    public static <A> Set<A> intersection(Set<A> first, Set<A> second) {
062
063        HashSet<A> result = new HashSet<A>(first);
064        result.retainAll(second);
065        return result;
066    }
067
068    /**
069     * Checks whether a collection is empty or null.<p>
070     *
071     * @param collection a collection
072     * @return true if <code>collection</code> is <code>null</code> or empty.
073     */
074    public static boolean isEmptyOrNull(Collection<?> collection) {
075
076        return (collection == null) || collection.isEmpty();
077    }
078
079    /**
080     * Parses properties from a string and returns them in a map.<p>
081     *
082     * @param text the text containing the properties
083     * @return the map with the parsed properties
084     */
085    public static Map<String, String> parseProperties(String text) {
086
087        String[] lines = text.split("\n");
088        Map<String, String> result = new HashMap<String, String>();
089        for (String line : lines) {
090            line = line.replaceFirst("^ +", "");
091            line = line.replaceAll("\r", "");
092            if (line.startsWith("#")) {
093                continue;
094            }
095            int eqPos = line.indexOf('=');
096            if (eqPos > 0) {
097                String key = line.substring(0, eqPos);
098                String value = line.substring(eqPos + 1);
099                result.put(key, value);
100            }
101        }
102        return result;
103    }
104
105    /**
106     * Returns a new map with all entries of the input map except those which have a value of null.<p>
107     *
108     * @param <A> the key type of the map
109     * @param <B> the value type of the map
110     * @param map the input map
111     *
112     * @return a map with all null entries removed
113     */
114    public static <A, B> Map<A, B> removeNullEntries(Map<A, B> map) {
115
116        HashMap<A, B> result = new HashMap<A, B>();
117
118        for (Map.Entry<A, B> entry : map.entrySet()) {
119            if (entry.getValue() != null) {
120                result.put(entry.getKey(), entry.getValue());
121            }
122        }
123        return result;
124    }
125
126    /**
127     * Copies entries from one map to another and deletes those entries in the target map for which
128     * the value in the source map is null.<p>
129     *
130     * @param <A> the key type of the map
131     * @param <B> the value type of the map
132     * @param source the source map
133     * @param target the target map
134     */
135    public static <A, B> void updateMapAndRemoveNulls(Map<A, B> source, Map<A, B> target) {
136
137        assert source != target;
138        for (Map.Entry<A, B> entry : source.entrySet()) {
139            A key = entry.getKey();
140            B value = entry.getValue();
141            if (value != null) {
142                target.put(key, value);
143            } else {
144                target.remove(key);
145            }
146        }
147    }
148}