Gets a Map holding entries corresponding to the key Iterable provided. - Java Collection Framework

Java examples for Collection Framework:Iterable

Description

Gets a Map holding entries corresponding to the key Iterable provided.

Demo Code

/*// w  ww.  j a va  2s.  co m
 * Copyright Terracotta, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Main{
    /**
     * Entries for populating {@link org.ehcache.spi.cache.Store Store} and
     * {@link org.ehcache.spi.loader.CacheLoader CacheLoader} instances in the
     * unit tests.  The entries used are generally subset using the key sets
     * {@link #KEY_SET_A}, {@link #KEY_SET_B}, {@link #KEY_SET_C}, and/or
     * {@link #KEY_SET_F}.
     * <p/>
     * Some tests are dependent on the order of the keys/entries.  In general,
     * for each key set ('xxxXn'), the keys/entries must be ordered by 'n'.
     */
    static final Map<String, String> TEST_ENTRIES;
    /**
     * Gets a {@code Map} holding entries corresponding to the key {@code Iterable} provided.  Each entry
     * value is prepended with the prefix value provided.
     *
     * @param prefix the non-{@code null} prefix used to alter each entry's value
     * @param subset the {@code Iterable} over the keys for entries to copy into the result {@code Map}
     *
     * @return a new, insert-ordered, modifiable {@code Map} holding the designated entries
     */
    static Map<String, String> getAltEntryMap(final String prefix,
            final Iterable<String> subset) {
        assert prefix != null;
        return getEntryMap(Collections.singletonList(subset), prefix);
    }
    /**
     * Gets a {@code Map} holding entries corresponding to the key {@code Iterable} provided.
     *
     * @param subset the {@code Iterable} over the keys for entries to copy into the result {@code Map}
     *
     * @return a new, insert-ordered, modifiable {@code Map} holding the designated entries
     */
    static Map<String, String> getEntryMap(final Iterable<String> subset) {
        return getEntryMap(Collections.singletonList(subset), null);
    }
    /**
     * Gets a {@code Map} holding entries corresponding to the key {@code Iterable}s provided.
     *
     * @param subset1 the first {@code Iterable} over the keys for entries to copy into the result {@code Map}
     * @param subset2 the second {@code Iterable}s over the keys for entries to copy into the result {@code Map}
     *
     * @return a new, insert-ordered, modifiable {@code Map} holding the designated entries
     */
    static Map<String, String> getEntryMap(final Iterable<String> subset1,
            final Iterable<String> subset2) {
        final List<Iterable<String>> subsets = new ArrayList<Iterable<String>>(
                2);
        subsets.add(subset1);
        subsets.add(subset2);
        return getEntryMap(subsets, null);
    }
    /**
     * Gets a {@code Map} holding entries corresponding to the key {@code Iterable}s provided.
     *
     * @param subset1 the first {@code Iterable} over the keys for entries to copy into the result {@code Map}
     * @param subset2 the second {@code Iterable}s over the keys for entries to copy into the result {@code Map}
     * @param subset3 the third {@code Iterable}s over the keys for entries to copy into the result {@code Map}
     *
     * @return a new, insert-ordered, modifiable {@code Map} holding the designated entries
     */
    static Map<String, String> getEntryMap(final Iterable<String> subset1,
            final Iterable<String> subset2, final Iterable<String> subset3) {
        final List<Iterable<String>> subsets = new ArrayList<Iterable<String>>(
                3);
        subsets.add(subset1);
        subsets.add(subset2);
        subsets.add(subset3);
        return getEntryMap(subsets, null);
    }
    /**
     * Gets a {@code Map} holding entries corresponding to the key {@code Iterable}s provided.
     *
     * @param subset1 the first {@code Iterable} over the keys for entries to copy into the result {@code Map}
     * @param subset2 the second {@code Iterable}s over the keys for entries to copy into the result {@code Map}
     * @param subset3 the third {@code Iterable}s over the keys for entries to copy into the result {@code Map}
     * @param subset4 the fourth {@code Iterable}s over the keys for entries to copy into the result {@code Map}
     *
     * @return a new, insert-ordered, modifiable {@code Map} holding the designated entries
     */
    static Map<String, String> getEntryMap(final Iterable<String> subset1,
            final Iterable<String> subset2, final Iterable<String> subset3,
            final Iterable<String> subset4) {
        final List<Iterable<String>> subsets = new ArrayList<Iterable<String>>(
                4);
        subsets.add(subset1);
        subsets.add(subset2);
        subsets.add(subset3);
        subsets.add(subset4);
        return getEntryMap(subsets, null);
    }
    /**
     * Gets a {@code Map} holding entries corresponding to the key {@code Iterable}s provided.
     *
     * @param subsets the {@code Iterable}s over the keys for entries to copy into the result {@code Map}
     * @param prefix the value to prepend to each entry value; if {@code null}, the values are not altered
     *
     * @return a new, insert-ordered, modifiable {@code Map} holding the designated entries
     */
    private static Map<String, String> getEntryMap(
            final List<Iterable<String>> subsets, final String prefix) {
        final StringBuilder sb = (prefix == null ? null
                : new StringBuilder(prefix));
        final Map<String, String> entryMap = new LinkedHashMap<String, String>();
        for (final Iterable<String> subset : subsets) {
            for (final String key : subset) {
                String value = TEST_ENTRIES.get(key);
                if (prefix != null) {
                    sb.setLength(prefix.length());
                    value = sb.append(value).toString();
                }
                entryMap.put(key, value);
            }
        }
        return entryMap;
    }
}

Related Tutorials