Skip to content

Commit

Permalink
Add DomainNameMapping.entries to allow retrieving the domain match lists
Browse files Browse the repository at this point in the history
Motivation:

See #4200.

Modifications:

Add DomainNameMapping.entries to allow retrieving the domain match lists.

Result:

People can use DomainNameMapping.entries to retrive the match list in DomainNameMapping.
  • Loading branch information
windie authored and normanmaurer committed May 17, 2016
1 parent f44f3e7 commit a56ef03
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
12 changes: 12 additions & 0 deletions common/src/main/java/io/netty/util/DomainMappingBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package io.netty.util;

import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -137,6 +139,16 @@ public V map(String hostname) {
return defaultValue;
}

@Override
public Set<Map.Entry<String, V>> entries() {
int length = domainNamePatterns.length;
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
public String toString() {
String defaultValueStr = defaultValue.toString();
Expand Down
9 changes: 9 additions & 0 deletions common/src/main/java/io/netty/util/DomainNameMapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
import io.netty.util.internal.StringUtil;

import java.net.IDN;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import static io.netty.util.internal.ObjectUtil.checkNotNull;
import static io.netty.util.internal.StringUtil.commonSuffixOfLength;
Expand Down Expand Up @@ -132,6 +134,13 @@ public V map(String hostname) {
return defaultValue;
}

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

@Override
public String toString() {
return StringUtil.simpleClassName(this) + "(default: " + defaultValue + ", map: " + map + ')';
Expand Down
36 changes: 36 additions & 0 deletions common/src/test/java/io/netty/util/DomainNameMappingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package io.netty.util;

import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -181,4 +184,37 @@ public void testToString() {
"ImmutableDomainNameMapping(default: NotFound, map: {*.netty.io=Netty, downloads.netty.io=Netty-Download})",
mapping.toString());
}

@Test
public void testEntries() {
DomainNameMapping<String> mapping = new DomainNameMapping<String>("NotFound")
.add("netty.io", "Netty")
.add("downloads.netty.io", "Netty-Downloads");

Map<String, String> entries = new HashMap<String, String>();
for (Map.Entry<String, String> entry: mapping.entries()) {
entries.put(entry.getKey(), entry.getValue());
}

assertEquals(2, entries.size());
assertEquals("Netty", entries.get("netty.io"));
assertEquals("Netty-Downloads", entries.get("downloads.netty.io"));
}

@Test
public void testEntriesWithImmutableDomainNameMapping() {
DomainNameMapping<String> mapping = new DomainMappingBuilder<String>("NotFound")
.add("netty.io", "Netty")
.add("downloads.netty.io", "Netty-Downloads")
.build();

Map<String, String> entries = new HashMap<String, String>();
for (Map.Entry<String, String> entry: mapping.entries()) {
entries.put(entry.getKey(), entry.getValue());
}

assertEquals(2, entries.size());
assertEquals("Netty", entries.get("netty.io"));
assertEquals("Netty-Downloads", entries.get("downloads.netty.io"));
}
}

0 comments on commit a56ef03

Please sign in to comment.