-
Notifications
You must be signed in to change notification settings - Fork 288
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract DefinitionMap as a public class
- Loading branch information
Showing
5 changed files
with
52 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 0 additions & 27 deletions
27
commonmark/src/main/java/org/commonmark/internal/LinkReferenceDefinitions.java
This file was deleted.
Oops, something went wrong.
38 changes: 38 additions & 0 deletions
38
commonmark/src/main/java/org/commonmark/node/DefinitionMap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package org.commonmark.node; | ||
|
||
import org.commonmark.internal.util.Escaping; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* A map that can be used to store and lookup reference definitions by a label. The labels are case-insensitive and | ||
* normalized, the same way as for {@link LinkReferenceDefinition} nodes. | ||
* | ||
* @param <V> the type of value | ||
*/ | ||
public class DefinitionMap<V> { | ||
|
||
// LinkedHashMap for determinism and to preserve document order | ||
private final Map<String, V> definitions = new LinkedHashMap<>(); | ||
|
||
/** | ||
* Store a new definition unless one is already in the map. | ||
*/ | ||
public void putIfAbsent(String label, V definition) { | ||
String normalizedLabel = Escaping.normalizeLabelContent(label); | ||
|
||
// spec: When there are multiple matching link reference definitions, the first is used | ||
definitions.putIfAbsent(normalizedLabel, definition); | ||
} | ||
|
||
/** | ||
* Lookup a definition by normalized label. | ||
* | ||
* @return the value or null | ||
*/ | ||
public V get(String label) { | ||
String normalizedLabel = Escaping.normalizeLabelContent(label); | ||
return definitions.get(normalizedLabel); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters