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