com.vladsch.flexmark.util.html.Attributes Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of flexmark-util-html Show documentation
Show all versions of flexmark-util-html Show documentation
flexmark-java html utility classes
The newest version!
package com.vladsch.flexmark.util.html;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
public class Attributes {
public static final Attributes EMPTY = new Attributes();
protected LinkedHashMap attributes;
public Attributes() {
attributes = null;
}
public Attributes(Attributes attributes) {
this.attributes =
attributes == null || attributes.attributes == null
? null
: new LinkedHashMap<>(attributes.attributes);
}
public MutableAttributes toMutable() {
return new MutableAttributes(this);
}
public Attributes toImmutable() {
return this;
}
public Attribute get(CharSequence key) {
if (attributes == null || key == null || key.length() == 0) {
return null;
}
String useKey = String.valueOf(key);
return attributes.get(useKey);
}
public String getValue(CharSequence key) {
if (attributes == null || key == null || key.length() == 0) {
return "";
}
String useKey = String.valueOf(key);
Attribute attribute = attributes.get(useKey);
if (attribute == null) {
return "";
}
return attribute.getValue();
}
public boolean contains(CharSequence key) {
if (attributes == null || key == null || key.length() == 0) {
return false;
}
String useKey = String.valueOf(key);
return attributes.containsKey(useKey);
}
public boolean containsValue(CharSequence key, CharSequence value) {
if (attributes == null) {
return false;
}
String useKey = String.valueOf(key);
Attribute attribute = attributes.get(useKey);
return attribute != null && attribute.containsValue(value);
}
public boolean isEmpty() {
return attributes == null || attributes.isEmpty();
}
public Set keySet() {
// CAUTION: attributes can be modified through this mutable set
return attributes != null ? attributes.keySet() : Collections.EMPTY_SET;
}
public Collection values() {
return attributes != null ? attributes.values() : Collections.EMPTY_LIST;
}
public Set> entrySet() {
// CAUTION: attributes can be modified through this mutable set
return attributes != null ? attributes.entrySet() : Collections.EMPTY_SET;
}
public void forEach(BiConsumer action) {
if (attributes != null) {
for (Map.Entry entry : attributes.entrySet()) {
action.accept(entry.getKey(), entry.getValue());
}
}
}
public int size() {
return attributes == null ? 0 : attributes.size();
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
String sep = "";
for (String attrName : keySet()) {
sb.append(sep).append(attrName);
Attribute attribute = attributes.get(attrName);
if (!attribute.getValue().isEmpty())
sb.append("=").append("\"").append(attribute.getValue().replace("\"", "\\\"")).append("\"");
sep = " ";
}
return "Attributes{" + sb.toString() + '}';
}
}