Java Essentials
clone()
method from the Object
class
- class
Object
is the root of the class hierarchy, every class has Object
as a superclass
- all objects, including arrays, implement the methods of this class
Object.clone()
method
- creates and returns a copy of this object, precise meaning of "copy" may depend on the class of the object
- performs a specific cloning operation
- first, if the class of this object does not implement the interface
Cloneable
, then a CloneNotSupportedException
is thrown
- otherwise, this method creates a new instance of the class of this object and initializes all its fields with exactly the contents of the corresponding fields of this object, as if by assignment
- the contents of the fields are not themselves cloned, thus, this method performs a "shallow copy" of this object, not a "deep copy" operation
Cloneable
interface
- a marker interface (meaning it does not declare any methods) that indicates to the
Object.clone()
method that it is legal for that method to make a field-for-field copy of instances of that class
- by convention, classes that implement this interface should override
Object.clone
(which is protected) with a public method
Prototype Pattern in Java
- available in Java out of the box with a
Cloneable
interface and overriding the clone()
method from the Object
class
Netty
- an asynchronous event-driven network application framework for rapid development of maintainable high-performance protocol servers & clients
- it greatly simplifies and streamlines network programming such as TCP and UDP socket server
Prototype Pattern in Netty
- to manage configurations for decoding HTTP messages
HttpDecoderConfig
holds settings that influence how HTTP messages are decoded, including maximum chunk size, support for chunked encoding, maximum header sizes, etc.
- Netty's
HttpDecoderConfig
class implements the Cloneable
interface and overrides Object.clone()
(which is protected) with a public method
- the class documentation states that
HttpDecoderConfig
objects are mutable to reduce allocation but also cloneable in case a defensive copy is needed
- the Prototype Pattern is employed to enable the creation of configuration objects that can be easily duplicated, particularly useful when you want to have a base configuration object with a set of default settings and then create slightly modified versions of this configuration for different use cases without affecting the original configuration object
/**
* A configuration object for specifying the behavior of {@link HttpObjectDecoder} and its subclasses.
* <p>
* The {@link HttpDecoderConfig} objects are mutable to reduce allocation,
* but also {@link Cloneable} in case a defensive copy is needed.
*/
public final class HttpDecoderConfig implements Cloneable {
private int maxChunkSize = HttpObjectDecoder.DEFAULT_MAX_CHUNK_SIZE;
private boolean chunkedSupported = HttpObjectDecoder.DEFAULT_CHUNKED_SUPPORTED;
private boolean allowPartialChunks = HttpObjectDecoder.DEFAULT_ALLOW_PARTIAL_CHUNKS;
private HttpHeadersFactory headersFactory = DefaultHttpHeadersFactory.headersFactory();
private HttpHeadersFactory trailersFactory = DefaultHttpHeadersFactory.trailersFactory();
private boolean allowDuplicateContentLengths = HttpObjectDecoder.DEFAULT_ALLOW_DUPLICATE_CONTENT_LENGTHS;
private int maxInitialLineLength = HttpObjectDecoder.DEFAULT_MAX_INITIAL_LINE_LENGTH;
private int maxHeaderSize = HttpObjectDecoder.DEFAULT_MAX_HEADER_SIZE;
private int initialBufferSize = HttpObjectDecoder.DEFAULT_INITIAL_BUFFER_SIZE;
@Override
public HttpDecoderConfig clone() {
try {
return (HttpDecoderConfig) super.clone();
} catch (CloneNotSupportedException e) {
throw new AssertionError();
}
}
HttpDecoderConfig
is designed to configure the behavior of HttpObjectDecoder
abstract class (and its concrete implementations) which is responsible for the actual decoding of incoming HTTP messages
- When an instance of
HttpObjectDecoder
is created, it can be supplied with a HttpDecoderConfig
object that customizes its behavior. This design allows for flexible and configurable decoding of HTTP messages, enabling the decoder to be adapted to different requirements and scenarios.