Don't create a new ParserImpl on each parser() call (#11255)

Motivation:

ParserImpl is stateless and so we can use the same instance multiple times

Modifications:

- Make constructor private
- Return the same instance all the time

Result:

Less object creation
This commit is contained in:
Norman Maurer 2021-05-14 15:19:04 +02:00 committed by GitHub
parent 265b2ea9bc
commit 78050c59ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -120,7 +120,7 @@ public final class HostsFileEntriesProvider {
* @return a new {@link HostsFileEntriesProvider.Parser}
*/
public static Parser parser() {
return new ParserImpl();
return ParserImpl.INSTANCE;
}
static final HostsFileEntriesProvider EMPTY =
@ -164,6 +164,12 @@ public final class HostsFileEntriesProvider {
private static final InternalLogger logger = InternalLoggerFactory.getInstance(Parser.class);
static final ParserImpl INSTANCE = new ParserImpl();
private ParserImpl() {
// singleton
}
@Override
public HostsFileEntriesProvider parse() throws IOException {
return parse(locateHostsFile(), Charset.defaultCharset());