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