|
Java example source code file (DomainNameMappingBuilder.java)
The DomainNameMappingBuilder.java Java example source code
/*
* Copyright 2016 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.util;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import static io.netty.util.internal.ObjectUtil.checkNotNull;
/**
* Builder for immutable {@link DomainNameMapping} instances.
*
* @param <V> concrete type of value objects
*/
public final class DomainNameMappingBuilder<V> {
private final V defaultValue;
private final Map<String, V> map;
/**
* Constructor with default initial capacity of the map holding the mappings
*
* @param defaultValue the default value for {@link DomainNameMapping#map(String)} to return
* when nothing matches the input
*/
public DomainNameMappingBuilder(V defaultValue) {
this(4, defaultValue);
}
/**
* Constructor with initial capacity of the map holding the mappings
*
* @param initialCapacity initial capacity for the internal map
* @param defaultValue the default value for {@link DomainNameMapping#map(String)} to return
* when nothing matches the input
*/
public DomainNameMappingBuilder(int initialCapacity, V defaultValue) {
this.defaultValue = checkNotNull(defaultValue, "defaultValue");
map = new LinkedHashMap<String, V>(initialCapacity);
}
/**
* Adds a mapping that maps the specified (optionally wildcard) host name to the specified output value.
* Null values are forbidden for both hostnames and values.
* <p>
* <a href="http://en.wikipedia.org/wiki/Wildcard_DNS_record">DNS wildcard is supported as hostname.
* For example, you can use {@code *.netty.io} to match {@code netty.io} and {@code downloads.netty.io}.
* </p>
*
* @param hostname the host name (optionally wildcard)
* @param output the output value that will be returned by {@link DomainNameMapping#map(String)}
* when the specified host name matches the specified input host name
*/
public DomainNameMappingBuilder<V> add(String hostname, V output) {
map.put(checkNotNull(hostname, "hostname"), checkNotNull(output, "output"));
return this;
}
/**
* Creates a new instance of immutable {@link DomainNameMapping}
* Attempts to add new mappings to the result object will cause {@link UnsupportedOperationException} to be thrown
*
* @return new {@link DomainNameMapping} instance
*/
public DomainNameMapping<V> build() {
return new ImmutableDomainNameMapping<V>(defaultValue, map);
}
/**
* Immutable mapping from domain name pattern to its associated value object.
* Mapping is represented by two arrays: keys and values. Key domainNamePatterns[i] is associated with values[i].
*
* @param <V> concrete type of value objects
*/
private static final class ImmutableDomainNameMapping<V> extends DomainNameMapping
Other Java examples (source code examples)Here is a short list of links related to this Java DomainNameMappingBuilder.java source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.