/** * Generates an array of entries from a map. Entries are defined in the API as * a class which consists of a {@code key} and {@code value} with a name * typically in the form of {@code Key_ValueMapEntry}, represented here as * {@code T}. The generated array can be used in objects where {@code * List<Key_ValueMapEntry>} is taken as a value. The input map must have same type * {@code K} as the {@code key} within {@code Key_ValueMapEntry}. The same * applies for {@code V} and {@code value} within {@code Key_ValueMapEntry}. * * @param <K> the type of the entry key * @param <V> the type of the entry value * @param <T> the map entry type * @param map a map of type {@code K} and {@code V} representing the entry * array * @param entryArray the entry array that entries will be added into * @return an array all map entries contained within the map parameter into * the provided array or a new array if there was not enough room */ public static <K, V, T> T[] toArray(Map<K, V> map, T[] entryArray) { return toList(map, entryArray.getClass().getComponentType()).toArray(entryArray); }
/** * Generates a map from an array an entries. Entries are defined in the API * as a class which consists of a {@code key} and {@code value} with a name * typically in the form of {@code Key_ValueMapEntry}, represented here as * {@code T}. The generated map must have same type {@code K} as the {@code * key} within {@code Key_ValueMapEntry}. The same applies for {@code V} and * {@code value} within {@code Key_ValueMapEntry}. If * {@code entryArray == null}, an emtpy map is returned. * * @param <K> the type of the entry key * @param <V> the type of the entry value * @param <T> the map entry type * @param entryArray the array of entries constituting a map as represented in * the API * @return a map of type {@code K} and {@code V} representing the entry array * @throws IllegalArgumentException if {@code entryArray} is not compatible * with {@code Map<K, V>}. */ public static <K, V, T> Map<K, V> toMap(T[] entryArray) throws IllegalArgumentException { if (entryArray == null) { return com.google.common.collect.Maps.<K, V>newHashMap(); } return toMap(Arrays.asList(entryArray)); }
/** * Tests {@link Maps#toArray(Map, Object[])} when the input array is large enough to hold all * entries in the supplied map. */ @Test public void testToArray_sufficientSize() { Entry[] arrayToFill = new Entry[entriesList.size()]; Entry[] expectedArray = entriesList.toArray(arrayToFill); Entry[] actualArray = Maps.<String, String, Entry>toArray(entriesMap, arrayToFill); assertArrayEquals(expectedArray, actualArray); assertSame( "toArray should not have created a new array since the input array was large enough", arrayToFill, actualArray); }
/** * Tests {@link Maps#toArray(Map, Object[])} when the input array is not large enough to hold all * entries in the supplied map. */ @Test public void testToArray_insufficientSize() { // Intentionally make the input array too small to fit all entries in the map. Entry[] arrayToFill = new Entry[0]; Entry[] expectedArray = entriesList.toArray(arrayToFill); Entry[] actualArray = Maps.<String, String, Entry>toArray(entriesMap, arrayToFill); assertArrayEquals(expectedArray, actualArray); assertNotSame( "toArray should have created a new array since the input array was not large enough", arrayToFill, actualArray); }
/** * Generates an array of entries from a map. Entries are defined in the API as * a class which consists of a {@code key} and {@code value} with a name * typically in the form of {@code Key_ValueMapEntry}, represented here as * {@code T}. The generated array can be used in objects where {@code * List<Key_ValueMapEntry>} is taken as a value. The input map must have same type * {@code K} as the {@code key} within {@code Key_ValueMapEntry}. The same * applies for {@code V} and {@code value} within {@code Key_ValueMapEntry}. * * @param <K> the type of the entry key * @param <V> the type of the entry value * @param <T> the map entry type * @param map a map of type {@code K} and {@code V} representing the entry * array * @param entryArray the entry array that entries will be added into * @return an array all map entries contained within the map parameter into * the provided array or a new array if there was not enough room */ public static <K, V, T> T[] toArray(Map<K, V> map, T[] entryArray) { return toList(map, entryArray.getClass().getComponentType()).toArray(entryArray); }
/** * Generates a map from an array an entries. Entries are defined in the API * as a class which consists of a {@code key} and {@code value} with a name * typically in the form of {@code Key_ValueMapEntry}, represented here as * {@code T}. The generated map must have same type {@code K} as the {@code * key} within {@code Key_ValueMapEntry}. The same applies for {@code V} and * {@code value} within {@code Key_ValueMapEntry}. If * {@code entryArray == null}, an emtpy map is returned. * * @param <K> the type of the entry key * @param <V> the type of the entry value * @param <T> the map entry type * @param entryArray the array of entries constituting a map as represented in * the API * @return a map of type {@code K} and {@code V} representing the entry array * @throws IllegalArgumentException if {@code entryArray} is not compatible * with {@code Map<K, V>}. */ public static <K, V, T> Map<K, V> toMap(T[] entryArray) throws IllegalArgumentException { if (entryArray == null) { return com.google.common.collect.Maps.<K, V>newHashMap(); } return toMap(Arrays.asList(entryArray)); }
/** * Gets the {@link Statement} representing the state of this statement * builder. * * @return the {@link Statement} */ public Statement toStatement() { Statement statement = new Statement(); statement.setQuery(queryBuilder.buildQuery()); statement.getValues().addAll( Maps.toList(queryBuilder.getBindVariableMap(), StringValueMapEntry.class)); return statement; }
@Test public void testToMap_array() { Map<String, String> map = Maps.<String, String, Entry>toMap(entriesList.toArray(new Entry[entriesList.size()])); assertEquals(entriesMap, map); }
/** * Gets the {@link Statement} representing the state of this statement * builder. * * @return the {@link Statement} */ public Statement toStatement() { Statement statement = new Statement(); statement.setQuery(queryBuilder.buildQuery()); statement.setValues(Maps.toList(queryBuilder.getBindVariableMap(), String_ValueMapEntry.class) .toArray(new String_ValueMapEntry[] {})); return statement; }
@Test public void testToMap_list() { Map<String, String> map = Maps.<String, String, Entry>toMap(entriesList); assertEquals(entriesMap, map); }
/** * Gets the {@link Statement} representing the state of this statement builder. * * @return the {@link Statement} */ public Statement toStatement() { Statement statement = new Statement(); statement.setQuery(queryBuilder.buildQuery()); statement .getValues() .addAll(Maps.toList(queryBuilder.getBindVariableMap(), StringValueMapEntry.class)); return statement; }
/** * Gets the {@link Statement} representing the state of this statement * builder. * * @return the {@link Statement} */ public Statement toStatement() { Statement statement = new Statement(); statement.setQuery(queryBuilder.buildQuery()); statement.setValues(Maps.toList(queryBuilder.getBindVariableMap(), String_ValueMapEntry.class) .toArray(new String_ValueMapEntry[] {})); return statement; }
/** * Gets the {@link Statement} representing the state of this statement * builder. * * @return the {@link Statement} */ public Statement toStatement() { Statement statement = new Statement(); statement.setQuery(queryBuilder.buildQuery()); statement.getValues().addAll( Maps.toList(queryBuilder.getBindVariableMap(), StringValueMapEntry.class)); return statement; }
/** * Gets the {@link Statement} representing the state of this statement * builder. * * @return the {@link Statement} */ public Statement toStatement() { Statement statement = new Statement(); statement.setQuery(queryBuilder.buildQuery()); statement.getValues().addAll( Maps.toList(queryBuilder.getBindVariableMap(), StringValueMapEntry.class)); return statement; }
/** * Gets the {@link Statement} representing the state of this statement builder. * * @return the {@link Statement} */ public Statement toStatement() { Statement statement = new Statement(); statement.setQuery(queryBuilder.buildQuery()); statement.setValues( Maps.toList(queryBuilder.getBindVariableMap(), String_ValueMapEntry.class) .toArray(new String_ValueMapEntry[] {})); return statement; }
/** * Gets the {@link Statement} representing the state of this statement * builder. * * @return the {@link Statement} */ public Statement toStatement() { Statement statement = new Statement(); statement.setQuery(queryBuilder.buildQuery()); statement.setValues(Maps.toList(queryBuilder.getBindVariableMap(), String_ValueMapEntry.class) .toArray(new String_ValueMapEntry[] {})); return statement; }
@Test public void testToList() { List<Entry> actualList = Maps.toList(entriesMap, Entry.class); assertEquals(entriesList, actualList); }