JsonDomAttribute.java

  1. // Generated by delombok at Fri Nov 29 09:48:08 UTC 2024
  2. package de.larssh.json.dom;

  3. import static java.util.Collections.emptyList;
  4. import java.util.Iterator;
  5. import java.util.function.Supplier;
  6. import org.w3c.dom.Attr;
  7. import org.w3c.dom.TypeInfo;
  8. import de.larssh.utils.Nullables;
  9. import edu.umd.cs.findbugs.annotations.NonNull;
  10. import edu.umd.cs.findbugs.annotations.Nullable;

  11. /**
  12.  * JSON DOM implementation of {@link Attr}.
  13.  *
  14.  * @param <T> implementation specific JSON element type
  15.  */
  16. public class JsonDomAttribute<T> extends JsonDomNode<T> implements Attr {
  17.     /**
  18.      * Supplier providing the attribute value when requested. The value is not
  19.      * cached.
  20.      *
  21.      * @return value provider
  22.      */
  23.     private final Supplier<String> valueProvider;

  24.     /**
  25.      * Constructor of {@link JsonDomAttribute}.
  26.      *
  27.      * @param parentNode    parent node
  28.      * @param nodeName      node name
  29.      * @param valueProvider value provider
  30.      */
  31.     public JsonDomAttribute(final JsonDomElement<T> parentNode, final String nodeName, final Supplier<String> valueProvider) {
  32.         super(parentNode, nodeName);
  33.         this.valueProvider = valueProvider;
  34.     }

  35.     /**
  36.      * {@inheritDoc}
  37.      */
  38.     @Nullable
  39.     @Override
  40.     @SuppressWarnings("PMD.ReturnEmptyCollectionRatherThanNull")
  41.     public JsonDomNamedNodeMap<JsonDomAttribute<T>> getAttributes() {
  42.         return null;
  43.     }

  44.     /**
  45.      * {@inheritDoc}
  46.      */
  47.     @NonNull
  48.     @Override
  49.     public JsonDomNodeList<JsonDomNode<T>> getChildNodes() {
  50.         return new JsonDomNodeList<>(emptyList());
  51.     }

  52.     /**
  53.      * {@inheritDoc}
  54.      */
  55.     @Override
  56.     public T getJsonElement() {
  57.         return Nullables.orElseThrow(getParentNode()).getJsonElement();
  58.     }

  59.     /**
  60.      * {@inheritDoc}
  61.      */
  62.     @NonNull
  63.     @Override
  64.     public String getName() {
  65.         return getNodeName();
  66.     }

  67.     /**
  68.      * {@inheritDoc}
  69.      */
  70.     @Nullable
  71.     @Override
  72.     @SuppressWarnings({"PMD.CompareObjectsWithEquals", "PMD.LooseCoupling"})
  73.     public JsonDomAttribute<T> getNextSibling() {
  74.         final JsonDomNode<T> parentNode = Nullables.orElseThrow(getParentNode());
  75.         final JsonDomNamedNodeMap<JsonDomAttribute<T>> attributes = Nullables.orElseThrow(parentNode.getAttributes());
  76.         final Iterator<JsonDomAttribute<T>> iterator = attributes.values().iterator();
  77.         while (iterator.hasNext()) {
  78.             if (iterator.next() == this) {
  79.                 return iterator.hasNext() ? iterator.next() : null;
  80.             }
  81.         }
  82.         return null;
  83.     }

  84.     /**
  85.      * {@inheritDoc}
  86.      */
  87.     @Override
  88.     public short getNodeType() {
  89.         return ATTRIBUTE_NODE;
  90.     }

  91.     /**
  92.      * {@inheritDoc}
  93.      */
  94.     @NonNull
  95.     @Override
  96.     public String getNodeValue() {
  97.         return getValue();
  98.     }

  99.     /**
  100.      * {@inheritDoc}
  101.      */
  102.     @NonNull
  103.     @Override
  104.     public JsonDomDocument<T> getOwnerDocument() {
  105.         return Nullables.orElseThrow(getParentNode()).getOwnerDocument();
  106.     }

  107.     /**
  108.      * {@inheritDoc}
  109.      */
  110.     @NonNull
  111.     @Override
  112.     public JsonDomElement<T> getOwnerElement() {
  113.         return (JsonDomElement<T>) Nullables.orElseThrow(getParentNode());
  114.     }

  115.     /**
  116.      * {@inheritDoc}
  117.      */
  118.     @Nullable
  119.     @Override
  120.     @SuppressWarnings({"PMD.CompareObjectsWithEquals", "PMD.LooseCoupling"})
  121.     public JsonDomAttribute<T> getPreviousSibling() {
  122.         JsonDomAttribute<T> previousSibling = null;
  123.         JsonDomAttribute<T> currentSibling = null;
  124.         final JsonDomNode<T> parentNode = Nullables.orElseThrow(getParentNode());
  125.         final JsonDomNamedNodeMap<JsonDomAttribute<T>> attributes = Nullables.orElseThrow(parentNode.getAttributes());
  126.         final Iterator<JsonDomAttribute<T>> iterator = attributes.values().iterator();
  127.         while (currentSibling != this) {
  128.             previousSibling = currentSibling;
  129.             currentSibling = iterator.next();
  130.         }
  131.         return previousSibling;
  132.     }

  133.     /**
  134.      * {@inheritDoc}
  135.      */
  136.     @Nullable
  137.     @Override
  138.     public TypeInfo getSchemaTypeInfo() {
  139.         return null;
  140.     }

  141.     /**
  142.      * {@inheritDoc}
  143.      */
  144.     @Override
  145.     public boolean getSpecified() {
  146.         return true;
  147.     }

  148.     /**
  149.      * {@inheritDoc}
  150.      */
  151.     @NonNull
  152.     @Override
  153.     public String getTextContent() {
  154.         return getValue();
  155.     }

  156.     /**
  157.      * {@inheritDoc}
  158.      */
  159.     @NonNull
  160.     @Override
  161.     public String getValue() {
  162.         return valueProvider.get();
  163.     }

  164.     /**
  165.      * {@inheritDoc}
  166.      */
  167.     @Override
  168.     public boolean isId() {
  169.         return false;
  170.     }

  171.     /**
  172.      * {@inheritDoc}
  173.      */
  174.     @Override
  175.     public void setValue(@Nullable @SuppressWarnings("unused") final String value) {
  176.         throw new UnsupportedOperationException();
  177.     }

  178.     @java.lang.Override
  179.     @java.lang.SuppressWarnings("all")
  180.     @edu.umd.cs.findbugs.annotations.SuppressFBWarnings(justification = "generated code")
  181.     @lombok.Generated
  182.     public boolean equals(@edu.umd.cs.findbugs.annotations.Nullable final java.lang.Object o) {
  183.         if (o == this) return true;
  184.         if (!(o instanceof JsonDomAttribute)) return false;
  185.         final JsonDomAttribute<?> other = (JsonDomAttribute<?>) o;
  186.         if (!other.canEqual((java.lang.Object) this)) return false;
  187.         if (!super.equals(o)) return false;
  188.         final java.lang.Object this$valueProvider = this.valueProvider;
  189.         final java.lang.Object other$valueProvider = other.valueProvider;
  190.         if (this$valueProvider == null ? other$valueProvider != null : !this$valueProvider.equals(other$valueProvider)) return false;
  191.         return true;
  192.     }

  193.     @java.lang.SuppressWarnings("all")
  194.     @edu.umd.cs.findbugs.annotations.SuppressFBWarnings(justification = "generated code")
  195.     @lombok.Generated
  196.     protected boolean canEqual(@edu.umd.cs.findbugs.annotations.Nullable final java.lang.Object other) {
  197.         return other instanceof JsonDomAttribute;
  198.     }

  199.     @java.lang.Override
  200.     @java.lang.SuppressWarnings("all")
  201.     @edu.umd.cs.findbugs.annotations.SuppressFBWarnings(justification = "generated code")
  202.     @lombok.Generated
  203.     public int hashCode() {
  204.         final int PRIME = 59;
  205.         int result = super.hashCode();
  206.         final java.lang.Object $valueProvider = this.valueProvider;
  207.         result = result * PRIME + ($valueProvider == null ? 43 : $valueProvider.hashCode());
  208.         return result;
  209.     }
  210. }