SetData.java

  1. /**
  2.  *
  3.  * Copyright © 2016-2021 Florian Schmaus
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.iot.control.element;

  18. import java.util.Locale;

  19. import org.jivesoftware.smack.packet.XmlElement;
  20. import org.jivesoftware.smack.util.XmlStringBuilder;

  21. public abstract class SetData implements XmlElement {

  22.     public enum Type {
  23.         BOOL,
  24.         INT,
  25.         LONG,
  26.         DOUBLE,
  27.         ;

  28.         private final String toStringCache;

  29.         Type() {
  30.             toStringCache = this.name().toLowerCase(Locale.US);
  31.         }

  32.         @Override
  33.         public String toString() {
  34.             return toStringCache;
  35.         }
  36.     }

  37.     protected SetData(String name, Type type, String value) {
  38.         this.name = name;
  39.         this.type = type;
  40.         this.value = value;
  41.     }

  42.     private final String name;

  43.     private final Type type;

  44.     private final String value;

  45.     public final String getName() {
  46.         return name;
  47.     }

  48.     public final String getValue() {
  49.         return value;
  50.     }

  51.     public final Type getType() {
  52.         return type;
  53.     }

  54.     /**
  55.      * Returns the root element name.
  56.      *
  57.      * @return the element name.
  58.      */
  59.     @Override
  60.     public final String getElementName() {
  61.         return getType().toString();
  62.     }

  63.     @Override
  64.     public final String getNamespace() {
  65.         return IoTSetRequest.NAMESPACE;
  66.     }

  67.     /**
  68.      * Returns the XML representation of this Element.
  69.      *
  70.      * @return the stanza extension as XML.
  71.      */
  72.     @Override
  73.     public final XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
  74.         XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace);
  75.         xml.attribute("name", name);
  76.         xml.attribute("value", value);
  77.         xml.closeEmptyElement();
  78.         return xml;
  79.     }
  80. }