All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.vladsch.flexmark.util.html.Attributes Maven / Gradle / Ivy

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() + '}';
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy