001/* 002 * Copyright (C) 2009 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 com.google.common.collect.testing.Helpers.orderEntriesByKey; 020 021import com.google.common.annotations.GwtCompatible; 022import java.util.List; 023import java.util.Map; 024import java.util.Map.Entry; 025 026/** 027 * Implementation helper for {@link TestMapGenerator} for use with enum maps. 028 * 029 * @author Kevin Bourrillion 030 */ 031@GwtCompatible 032@ElementTypesAreNonnullByDefault 033public abstract class TestEnumMapGenerator implements TestMapGenerator<AnEnum, String> { 034 035 @Override 036 public SampleElements<Entry<AnEnum, String>> samples() { 037 return new SampleElements<>( 038 Helpers.mapEntry(AnEnum.A, "January"), 039 Helpers.mapEntry(AnEnum.B, "February"), 040 Helpers.mapEntry(AnEnum.C, "March"), 041 Helpers.mapEntry(AnEnum.D, "April"), 042 Helpers.mapEntry(AnEnum.E, "May")); 043 } 044 045 @Override 046 public final Map<AnEnum, String> create(Object... entries) { 047 @SuppressWarnings("unchecked") 048 Entry<AnEnum, String>[] array = (Entry<AnEnum, String>[]) new Entry<?, ?>[entries.length]; 049 int i = 0; 050 for (Object o : entries) { 051 @SuppressWarnings("unchecked") 052 Entry<AnEnum, String> e = (Entry<AnEnum, String>) o; 053 array[i++] = e; 054 } 055 return create(array); 056 } 057 058 protected abstract Map<AnEnum, String> create(Entry<AnEnum, String>[] entries); 059 060 @Override 061 @SuppressWarnings("unchecked") 062 public final Entry<AnEnum, String>[] createArray(int length) { 063 return (Entry<AnEnum, String>[]) new Entry<?, ?>[length]; 064 } 065 066 @Override 067 public final AnEnum[] createKeyArray(int length) { 068 return new AnEnum[length]; 069 } 070 071 @Override 072 public final String[] createValueArray(int length) { 073 return new String[length]; 074 } 075 076 /** Returns the elements sorted in natural order. */ 077 @Override 078 public Iterable<Entry<AnEnum, String>> order(List<Entry<AnEnum, String>> insertionOrder) { 079 return orderEntriesByKey(insertionOrder); 080 } 081}