001/**
002 *
003 * Copyright © 2016 Florian Schmaus
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smackx.iot.control.element;
018
019import java.util.Locale;
020
021import org.jivesoftware.smack.packet.NamedElement;
022import org.jivesoftware.smack.util.XmlStringBuilder;
023
024public abstract class SetData implements NamedElement {
025
026    public enum Type {
027        BOOL,
028        INT,
029        LONG,
030        DOUBLE,
031        ;
032
033        private final String toStringCache;
034
035        Type() {
036            toStringCache = this.name().toLowerCase(Locale.US);
037        }
038
039        @Override
040        public String toString() {
041            return toStringCache;
042        }
043    }
044
045    protected SetData(String name, Type type, String value) {
046        this.name = name;
047        this.type = type;
048        this.value = value;
049    }
050
051    private final String name;
052
053    private final Type type;
054
055    private final String value;
056
057    public final String getName() {
058        return name;
059    }
060
061    public final String getValue() {
062        return value;
063    }
064
065    public final Type getType() {
066        return type;
067    }
068
069    /**
070     * Returns the root element name.
071     *
072     * @return the element name.
073     */
074    @Override
075    public final String getElementName() {
076        return getType().toString();
077    }
078
079    /**
080     * Returns the XML representation of this Element.
081     *
082     * @return the stanza(/packet) extension as XML.
083     */
084    @Override
085    public final XmlStringBuilder toXML() {
086        XmlStringBuilder xml = new XmlStringBuilder(this);
087        xml.attribute("name", name);
088        xml.attribute("value", value);
089        xml.closeEmptyElement();
090        return xml;
091    }
092}