Skip to content

Commit

Permalink
streamline singleton handling
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri committed Jun 2, 2024
1 parent 5df39fc commit 21c88b9
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public final class HTMLNamedEntitiesParser {
/*
* Our single instance of the parser, we don't have state, so we are safe
*/
private static final HTMLNamedEntitiesParser instance = new HTMLNamedEntitiesParser();
public static final HTMLNamedEntitiesParser INSTANCE = new HTMLNamedEntitiesParser();

/*
* Our starting point of the pseudo tree of entities. The root level is a little special, because of the size,
Expand Down Expand Up @@ -114,16 +114,6 @@ private HTMLNamedEntitiesParser() {
}
}

/**
* Returns the singleton. The singleton is stateless and can safely be used in a multi-threaded
* context.
*
* @return the singleton instance of the parser, can never be null
*/
public static HTMLNamedEntitiesParser get() {
return instance;
}

/**
* Utility method, mostly for testing, that allows us to look up and entity from a string
* instead from single characters.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/htmlunit/cyberneko/HTMLScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -1344,7 +1344,7 @@ protected int scanEntityRef(final XMLString str, final XMLString plainValue, fin
HTMLNamedEntitiesParser.State lastMatchingResult = null;

while (nextChar != -1) {
final HTMLNamedEntitiesParser.State intermediateResult = HTMLNamedEntitiesParser.get().lookup(nextChar, result);
final HTMLNamedEntitiesParser.State intermediateResult = HTMLNamedEntitiesParser.INSTANCE.lookup(nextChar, result);

if (intermediateResult.endNode_) {
result = intermediateResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ protected void printCharacters(final XMLString text, final boolean normalize) {
for (int i = 0; i < text.length(); i++) {
final char c = text.charAt(i);
if (c != '\n') {
final String entity = HTMLNamedEntitiesParser.get().lookupEntityRefFor(Character.toString(c));
final String entity = HTMLNamedEntitiesParser.INSTANCE.lookupEntityRefFor(Character.toString(c));
if (entity != null) {
printer_.print(entity);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,7 @@ public class CoreDOMImplementationImpl implements DOMImplementation {
private int docAndDoctypeCounter_ = 0;

/** Dom implementation singleton. */
private static final CoreDOMImplementationImpl singleton = new CoreDOMImplementationImpl();

// NON-DOM: Obtain and return the single shared object
public static DOMImplementation getDOMImplementation() {
return singleton;
}
public static final CoreDOMImplementationImpl INSTANCE = new CoreDOMImplementationImpl();

/**
* {@inheritDoc}
Expand Down Expand Up @@ -197,8 +192,8 @@ protected CoreDocumentImpl createDocument(final DocumentType doctype) {
*/
@Override
public Object getFeature(final String feature, final String version) {
if (singleton.hasFeature(feature, version)) {
return singleton;
if (INSTANCE.hasFeature(feature, version)) {
return INSTANCE;
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ public NodeList getElementsByTagName(final String tagname) {
public DOMImplementation getImplementation() {
// Currently implemented as a singleton, since it's hardcoded
// information anyway.
return CoreDOMImplementationImpl.getDOMImplementation();
return CoreDOMImplementationImpl.INSTANCE;
}

/**
Expand Down Expand Up @@ -948,7 +948,7 @@ public Entity createEntity(final String name) throws DOMException {
protected int getNodeNumber() {
if (documentNumber_ == 0) {

final CoreDOMImplementationImpl cd = (CoreDOMImplementationImpl) CoreDOMImplementationImpl.getDOMImplementation();
final CoreDOMImplementationImpl cd = CoreDOMImplementationImpl.INSTANCE;
documentNumber_ = cd.assignDocumentNumber();
}
return documentNumber_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,7 @@
public class DOMImplementationImpl extends CoreDOMImplementationImpl {

/** Dom implementation singleton. */
private static final DOMImplementationImpl singleton = new DOMImplementationImpl();

// NON-DOM: Obtain and return the single shared object
public static DOMImplementation getDOMImplementation() {
return singleton;
}
public static final DOMImplementationImpl INSTANCE = new DOMImplementationImpl();

/**
* {@inheritDoc}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public Node cloneNode(final boolean deep) {
public DOMImplementation getImplementation() {
// Currently implemented as a singleton, since it's hardcoded
// information anyway.
return DOMImplementationImpl.getDOMImplementation();
return DOMImplementationImpl.INSTANCE;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,7 @@ protected int getNodeNumber() {
// The doctype is disconnected and not associated with any document.
// Assign the doctype a number relative to the implementation.
if (doctypeNumber_ == 0) {

final CoreDOMImplementationImpl cd = (CoreDOMImplementationImpl) CoreDOMImplementationImpl.getDOMImplementation();
final CoreDOMImplementationImpl cd = CoreDOMImplementationImpl.INSTANCE;
doctypeNumber_ = cd.assignDocTypeNumber();
}
return doctypeNumber_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
public class HTMLNamedEntitiesParserTest {
@Test
public void happyPath() {
final State r = HTMLNamedEntitiesParser.get().lookup("Beta;");
final State r = HTMLNamedEntitiesParser.INSTANCE.lookup("Beta;");
assertTrue(r.isMatch_);
assertTrue(r.endNode_);
assertTrue(r.endsWithSemicolon_);
Expand All @@ -50,7 +50,7 @@ public void happyPath() {
@Test
public void happyPathOneCharDiff() {
{
final State r = HTMLNamedEntitiesParser.get().lookup("Colon;");
final State r = HTMLNamedEntitiesParser.INSTANCE.lookup("Colon;");
assertTrue(r.isMatch_);
assertTrue(r.endNode_);
assertTrue(r.endsWithSemicolon_);
Expand All @@ -60,7 +60,7 @@ public void happyPathOneCharDiff() {
assertEquals(6, r.length_);
}
{
final State r = HTMLNamedEntitiesParser.get().lookup("Colone;");
final State r = HTMLNamedEntitiesParser.INSTANCE.lookup("Colone;");
assertTrue(r.isMatch_);
assertTrue(r.endNode_);
assertTrue(r.endsWithSemicolon_);
Expand All @@ -74,7 +74,7 @@ public void happyPathOneCharDiff() {
@Test
public void happyPathTwoVersionEntity() {
{
final State r = HTMLNamedEntitiesParser.get().lookup("gt");
final State r = HTMLNamedEntitiesParser.INSTANCE.lookup("gt");
assertEquals("gt", r.entityOrFragment_);
assertTrue(r.isMatch_);
assertFalse(r.endNode_);
Expand All @@ -84,7 +84,7 @@ public void happyPathTwoVersionEntity() {
assertEquals(2, r.length_);
}
{
final State r = HTMLNamedEntitiesParser.get().lookup("gt;");
final State r = HTMLNamedEntitiesParser.INSTANCE.lookup("gt;");
assertEquals("gt;", r.entityOrFragment_);
assertTrue(r.isMatch_);
assertTrue(r.endNode_);
Expand All @@ -98,7 +98,7 @@ public void happyPathTwoVersionEntity() {
@Test
public void happyPathTwoVersionEntity2() {
{
final State r = HTMLNamedEntitiesParser.get().lookup("ccedil");
final State r = HTMLNamedEntitiesParser.INSTANCE.lookup("ccedil");
assertEquals("ccedil", r.entityOrFragment_);
assertTrue(r.isMatch_);
assertFalse(r.endNode_);
Expand All @@ -108,7 +108,7 @@ public void happyPathTwoVersionEntity2() {
assertEquals(6, r.length_);
}
{
final State r = HTMLNamedEntitiesParser.get().lookup("ccedil;");
final State r = HTMLNamedEntitiesParser.INSTANCE.lookup("ccedil;");
assertEquals("ccedil;", r.entityOrFragment_);
assertTrue(r.isMatch_);
assertTrue(r.endNode_);
Expand All @@ -122,7 +122,7 @@ public void happyPathTwoVersionEntity2() {
@Test
public void fullyUnknown() {
{
final State r = HTMLNamedEntitiesParser.get().lookup("abc;");
final State r = HTMLNamedEntitiesParser.INSTANCE.lookup("abc;");
assertFalse(r.isMatch_);
assertFalse(r.endNode_);
assertFalse(r.endsWithSemicolon_);
Expand All @@ -138,7 +138,7 @@ public void fullyUnknown() {
*/
@Test
public void notit() {
final State r = HTMLNamedEntitiesParser.get().lookup("notit;");
final State r = HTMLNamedEntitiesParser.INSTANCE.lookup("notit;");
assertTrue(r.isMatch_);
assertFalse(r.endNode_);
assertFalse(r.endsWithSemicolon_);
Expand All @@ -153,7 +153,7 @@ public void notit() {
*/
@Test
public void notSemicolon() {
final State r = HTMLNamedEntitiesParser.get().lookup("not;");
final State r = HTMLNamedEntitiesParser.INSTANCE.lookup("not;");
assertTrue(r.isMatch_);
assertTrue(r.endNode_);
assertTrue(r.endsWithSemicolon_);
Expand All @@ -168,7 +168,7 @@ public void notSemicolon() {
*/
@Test
public void notHash() {
final State r = HTMLNamedEntitiesParser.get().lookup("not#");
final State r = HTMLNamedEntitiesParser.INSTANCE.lookup("not#");
assertTrue(r.isMatch_);
assertFalse(r.endNode_);
assertFalse(r.endsWithSemicolon_);
Expand All @@ -184,7 +184,7 @@ public void notHash() {
*/
@Test
public void smallerThanA() {
final State r = HTMLNamedEntitiesParser.get().lookup("9ot#");
final State r = HTMLNamedEntitiesParser.INSTANCE.lookup("9ot#");
assertFalse(r.isMatch_);
assertFalse(r.endNode_);
assertFalse(r.endsWithSemicolon_);
Expand All @@ -200,7 +200,7 @@ public void smallerThanA() {
*/
@Test
public void largeThanLowercaseZ() {
final State r = HTMLNamedEntitiesParser.get().lookup("{any");
final State r = HTMLNamedEntitiesParser.INSTANCE.lookup("{any");
assertFalse(r.isMatch_);
assertFalse(r.endNode_);
assertFalse(r.endsWithSemicolon_);
Expand All @@ -216,7 +216,7 @@ public void largeThanLowercaseZ() {
*/
@Test
public void oneCharInAHoleWithoutNextLevel() {
final State r = HTMLNamedEntitiesParser.get().lookup("[any");
final State r = HTMLNamedEntitiesParser.INSTANCE.lookup("[any");
assertFalse(r.isMatch_);
assertFalse(r.endNode_);
assertFalse(r.endsWithSemicolon_);
Expand Down Expand Up @@ -248,7 +248,7 @@ public void allEntitiesWithSemicolonFull() throws IOException {
return;
}

final State r = HTMLNamedEntitiesParser.get().lookup(key);
final State r = HTMLNamedEntitiesParser.INSTANCE.lookup(key);
assertTrue(r.isMatch_);

if (key.endsWith(";")) {
Expand All @@ -275,13 +275,13 @@ public void allEntitiesWithSemicolonFull() throws IOException {
*/
@Test
public void lookupEntityRefFor() throws IOException {
assertNull(HTMLNamedEntitiesParser.get().lookupEntityRefFor("a"));
assertNull(HTMLNamedEntitiesParser.INSTANCE.lookupEntityRefFor("a"));

assertEquals("&auml;", HTMLNamedEntitiesParser.get().lookupEntityRefFor("ä"));
assertEquals("&Ouml;", HTMLNamedEntitiesParser.get().lookupEntityRefFor("Ö"));
assertEquals("&auml;", HTMLNamedEntitiesParser.INSTANCE.lookupEntityRefFor("ä"));
assertEquals("&Ouml;", HTMLNamedEntitiesParser.INSTANCE.lookupEntityRefFor("Ö"));

// make sure we return the entry with the semicolon at end
assertEquals("&yacute;", HTMLNamedEntitiesParser.get().lookupEntityRefFor("\u00FD"));
assertEquals("&yacute;", HTMLNamedEntitiesParser.INSTANCE.lookupEntityRefFor("\u00FD"));

}
}

0 comments on commit 21c88b9

Please sign in to comment.