001/*
002 * Copyright © 2025 CUI-OpenSource-Software (info@cuioss.de)
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package de.cuioss.test.generator.internal.net.java.quickcheck.generator.support;
017
018import de.cuioss.test.generator.internal.net.java.quickcheck.Generator;
019
020import java.util.HashMap;
021import java.util.Map;
022import java.util.Map.Entry;
023import java.util.Objects;
024import java.util.Set;
025
026public class SubmapGenerator<K, V> implements Generator<Map<K, V>> {
027
028    private final SubsetGenerator<Entry<K, V>> subsets;
029
030    public SubmapGenerator(Map<K, V> supermap) {
031        Objects.requireNonNull(supermap, "supermap");
032        this.subsets = new SubsetGenerator<>(supermap.entrySet());
033    }
034
035    public SubmapGenerator(Map<K, V> supermap, Generator<Integer> sizes) {
036        Objects.requireNonNull(supermap, "supermap");
037        Objects.requireNonNull(sizes, "sizes");
038        this.subsets = new SubsetGenerator<>(supermap.entrySet(), sizes);
039    }
040
041    @Override
042    public Map<K, V> next() {
043        Set<Entry<K, V>> entries = subsets.next();
044        Map<K, V> submap = new HashMap<>(entries.size());
045        for (Entry<K, V> e : entries)
046            submap.put(e.getKey(), e.getValue());
047        return submap;
048    }
049}