Replace DomainNameMapping.entries() with asMap()

Motivation:

DomainNameMapping.entries() returns Set<Map.Entry<String, V>>, which
doesn't sound very natural.

Modifications:

Replace entries() with asMap() which returns a Map<String, V> instead.

Result:

- Better looking API
- User can do a lookup because it's a Map
This commit is contained in:
Trustin Lee 2016-05-18 18:07:53 +09:00 committed by Norman Maurer
parent 55dd7d035f
commit ea1f60dbf0
3 changed files with 21 additions and 27 deletions

View File

@ -17,7 +17,6 @@
package io.netty.util; package io.netty.util;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
@ -53,7 +52,7 @@ public final class DomainMappingBuilder<V> {
*/ */
public DomainMappingBuilder(int initialCapacity, V defaultValue) { public DomainMappingBuilder(int initialCapacity, V defaultValue) {
this.defaultValue = checkNotNull(defaultValue, "defaultValue"); this.defaultValue = checkNotNull(defaultValue, "defaultValue");
this.map = new LinkedHashMap<String, V>(initialCapacity); map = new LinkedHashMap<String, V>(initialCapacity);
} }
/** /**
@ -80,7 +79,7 @@ public final class DomainMappingBuilder<V> {
* @return new {@link DomainNameMapping} instance * @return new {@link DomainNameMapping} instance
*/ */
public DomainNameMapping<V> build() { public DomainNameMapping<V> build() {
return new ImmutableDomainNameMapping<V>(this.defaultValue, this.map); return new ImmutableDomainNameMapping<V>(defaultValue, map);
} }
/** /**
@ -98,6 +97,7 @@ public final class DomainMappingBuilder<V> {
private final String[] domainNamePatterns; private final String[] domainNamePatterns;
private final V[] values; private final V[] values;
private final Map<String, V> map;
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private ImmutableDomainNameMapping(V defaultValue, Map<String, V> map) { private ImmutableDomainNameMapping(V defaultValue, Map<String, V> map) {
@ -108,12 +108,18 @@ public final class DomainMappingBuilder<V> {
domainNamePatterns = new String[numberOfMappings]; domainNamePatterns = new String[numberOfMappings];
values = (V[]) new Object[numberOfMappings]; values = (V[]) new Object[numberOfMappings];
final Map<String, V> mapCopy = new LinkedHashMap<String, V>(map.size());
int index = 0; int index = 0;
for (Map.Entry<String, V> mapping : mappings) { for (Map.Entry<String, V> mapping : mappings) {
domainNamePatterns[index] = normalizeHostname(mapping.getKey()); final String hostname = normalizeHostname(mapping.getKey());
values[index] = mapping.getValue(); final V value = mapping.getValue();
domainNamePatterns[index] = hostname;
values[index] = value;
mapCopy.put(hostname, value);
++index; ++index;
} }
this.map = Collections.unmodifiableMap(mapCopy);
} }
@Override @Override
@ -140,13 +146,8 @@ public final class DomainMappingBuilder<V> {
} }
@Override @Override
public Set<Map.Entry<String, V>> entries() { public Map<String, V> asMap() {
int length = domainNamePatterns.length; return map;
Map<String, V> map = new HashMap<String, V>(length);
for (int index = 0; index < length; ++index) {
map.put(domainNamePatterns[index], values[index]);
}
return Collections.unmodifiableSet(map.entrySet());
} }
@Override @Override

View File

@ -137,8 +137,8 @@ public class DomainNameMapping<V> implements Mapping<String, V> {
/** /**
* Returns a read-only {@link Set} of the domain mapping patterns and their associated value objects. * Returns a read-only {@link Set} of the domain mapping patterns and their associated value objects.
*/ */
public Set<Map.Entry<String, V>> entries() { public Map<String, V> asMap() {
return Collections.unmodifiableSet(map.entrySet()); return Collections.unmodifiableMap(map);
} }
@Override @Override

View File

@ -16,11 +16,10 @@
package io.netty.util; package io.netty.util;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test; import org.junit.Test;
import java.util.Map;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@ -186,15 +185,12 @@ public class DomainNameMappingTest {
} }
@Test @Test
public void testEntries() { public void testAsMap() {
DomainNameMapping<String> mapping = new DomainNameMapping<String>("NotFound") DomainNameMapping<String> mapping = new DomainNameMapping<String>("NotFound")
.add("netty.io", "Netty") .add("netty.io", "Netty")
.add("downloads.netty.io", "Netty-Downloads"); .add("downloads.netty.io", "Netty-Downloads");
Map<String, String> entries = new HashMap<String, String>(); Map<String, String> entries = mapping.asMap();
for (Map.Entry<String, String> entry: mapping.entries()) {
entries.put(entry.getKey(), entry.getValue());
}
assertEquals(2, entries.size()); assertEquals(2, entries.size());
assertEquals("Netty", entries.get("netty.io")); assertEquals("Netty", entries.get("netty.io"));
@ -202,16 +198,13 @@ public class DomainNameMappingTest {
} }
@Test @Test
public void testEntriesWithImmutableDomainNameMapping() { public void testAsMapWithImmutableDomainNameMapping() {
DomainNameMapping<String> mapping = new DomainMappingBuilder<String>("NotFound") DomainNameMapping<String> mapping = new DomainMappingBuilder<String>("NotFound")
.add("netty.io", "Netty") .add("netty.io", "Netty")
.add("downloads.netty.io", "Netty-Downloads") .add("downloads.netty.io", "Netty-Downloads")
.build(); .build();
Map<String, String> entries = new HashMap<String, String>(); Map<String, String> entries = mapping.asMap();
for (Map.Entry<String, String> entry: mapping.entries()) {
entries.put(entry.getKey(), entry.getValue());
}
assertEquals(2, entries.size()); assertEquals(2, entries.size());
assertEquals("Netty", entries.get("netty.io")); assertEquals("Netty", entries.get("netty.io"));