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