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; 023 024/** 025 * Implementation helper for {@link TestMapGenerator} for use with maps of strings. 026 * 027 * @author Chris Povirk 028 * @author Jared Levy 029 * @author George van den Driessche 030 */ 031@GwtCompatible 032@ElementTypesAreNonnullByDefault 033public abstract class TestStringMapGenerator implements TestMapGenerator<String, String> { 034 035 @Override 036 public SampleElements<Entry<String, String>> samples() { 037 return new SampleElements<>( 038 Helpers.mapEntry("one", "January"), 039 Helpers.mapEntry("two", "February"), 040 Helpers.mapEntry("three", "March"), 041 Helpers.mapEntry("four", "April"), 042 Helpers.mapEntry("five", "May")); 043 } 044 045 @Override 046 public Map<String, String> create(Object... entries) { 047 @SuppressWarnings("unchecked") 048 Entry<String, String>[] array = (Entry<String, String>[]) new Entry<?, ?>[entries.length]; 049 int i = 0; 050 for (Object o : entries) { 051 @SuppressWarnings("unchecked") 052 Entry<String, String> e = (Entry<String, String>) o; 053 array[i++] = e; 054 } 055 return create(array); 056 } 057 058 protected abstract Map<String, String> create(Entry<String, String>[] entries); 059 060 @Override 061 @SuppressWarnings("unchecked") 062 public final Entry<String, String>[] createArray(int length) { 063 return (Entry<String, String>[]) new Entry<?, ?>[length]; 064 } 065 066 @Override 067 public final String[] createKeyArray(int length) { 068 return new String[length]; 069 } 070 071 @Override 072 public final String[] createValueArray(int length) { 073 return new String[length]; 074 } 075 076 /** Returns the original element list, unchanged. */ 077 @Override 078 public Iterable<Entry<String, String>> order(List<Entry<String, String>> insertionOrder) { 079 return insertionOrder; 080 } 081}