001/*
002 * Copyright (C) 2008 The Guava Authors
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 */
016
017package com.google.common.collect.testing;
018
019import static java.lang.System.arraycopy;
020
021import com.google.common.annotations.GwtCompatible;
022import java.util.List;
023import java.util.Map;
024import java.util.Map.Entry;
025import java.util.Set;
026import org.jspecify.annotations.NullMarked;
027import org.checkerframework.checker.nullness.qual.Nullable;
028
029/**
030 * Creates map entries using sample keys and sample values.
031 *
032 * @author Jesse Wilson
033 */
034@GwtCompatible
035@NullMarked
036public abstract class TestMapEntrySetGenerator<
037        K extends @Nullable Object, V extends @Nullable Object>
038    implements TestSetGenerator<Map.Entry<K, V>> {
039  private final SampleElements<K> keys;
040  private final SampleElements<V> values;
041
042  protected TestMapEntrySetGenerator(SampleElements<K> keys, SampleElements<V> values) {
043    this.keys = keys;
044    this.values = values;
045  }
046
047  @Override
048  public SampleElements<Entry<K, V>> samples() {
049    return SampleElements.mapEntries(keys, values);
050  }
051
052  @Override
053  public Set<Entry<K, V>> create(Object... elements) {
054    Entry<K, V>[] entries = createArray(elements.length);
055    arraycopy(elements, 0, entries, 0, elements.length);
056    return createFromEntries(entries);
057  }
058
059  public abstract Set<Entry<K, V>> createFromEntries(Entry<K, V>[] entries);
060
061  @Override
062  @SuppressWarnings("unchecked") // generic arrays make typesafety sad
063  public Entry<K, V>[] createArray(int length) {
064    return (Entry<K, V>[]) new Entry<?, ?>[length];
065  }
066
067  /** Returns the original element list, unchanged. */
068  @Override
069  public List<Entry<K, V>> order(List<Entry<K, V>> insertionOrder) {
070    return insertionOrder;
071  }
072}