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.checkerframework.checker.nullness.qual.Nullable;
027
028/**
029 * Creates map entries using sample keys and sample values.
030 *
031 * @author Jesse Wilson
032 */
033@GwtCompatible
034@ElementTypesAreNonnullByDefault
035public abstract class TestMapEntrySetGenerator<
036        K extends @Nullable Object, V extends @Nullable Object>
037    implements TestSetGenerator<Map.Entry<K, V>> {
038  private final SampleElements<K> keys;
039  private final SampleElements<V> values;
040
041  protected TestMapEntrySetGenerator(SampleElements<K> keys, SampleElements<V> values) {
042    this.keys = keys;
043    this.values = values;
044  }
045
046  @Override
047  public SampleElements<Entry<K, V>> samples() {
048    return SampleElements.mapEntries(keys, values);
049  }
050
051  @Override
052  public Set<Entry<K, V>> create(Object... elements) {
053    Entry<K, V>[] entries = createArray(elements.length);
054    arraycopy(elements, 0, entries, 0, elements.length);
055    return createFromEntries(entries);
056  }
057
058  public abstract Set<Entry<K, V>> createFromEntries(Entry<K, V>[] entries);
059
060  @Override
061  @SuppressWarnings("unchecked") // generic arrays make typesafety sad
062  public Entry<K, V>[] createArray(int length) {
063    return (Entry<K, V>[]) new Entry<?, ?>[length];
064  }
065
066  /** Returns the original element list, unchanged. */
067  @Override
068  public List<Entry<K, V>> order(List<Entry<K, V>> insertionOrder) {
069    return insertionOrder;
070  }
071}