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.data.element;
018
019import org.jivesoftware.smack.util.XmlStringBuilder;
020
021public abstract class IoTDataField extends IoTDataExtensionElement {
022
023    enum Type {
024        integer("int"),
025        bool("boolean"),
026        ;
027
028        Type(String stringRepresentation) {
029            this.stringRepresentation = stringRepresentation;
030        }
031
032        private final String stringRepresentation;
033    }
034
035    private final Type type;
036
037    private final String name;
038
039    protected IoTDataField(Type type, String name) {
040        this.type = type;
041        this.name = name;
042    }
043
044    public final String getName() {
045        return name;
046    }
047
048    @Override
049    public final String getElementName() {
050        return type.stringRepresentation;
051    }
052
053    @Override
054    public final XmlStringBuilder toXML(org.jivesoftware.smack.packet.XmlEnvironment enclosingNamespace) {
055        XmlStringBuilder xml = new XmlStringBuilder(this, enclosingNamespace);
056        xml.attribute("name", name).attribute("value", getValueString());
057        // TODO handle 'unit' attribute as special case if <numeric/> is implemented.
058        xml.closeEmptyElement();
059        return xml;
060    }
061
062    private String valueString;
063
064    public final String getValueString() {
065        if (valueString == null) {
066            valueString = getValueInternal();
067        }
068        return valueString;
069    }
070
071    protected abstract String getValueInternal();
072
073    public static class IntField extends IoTDataField {
074
075        private final int value;
076
077        public IntField(String name, int value) {
078            super(Type.integer, name);
079            this.value = value;
080        }
081
082        @Override
083        protected String getValueInternal() {
084            return Integer.toString(value);
085        }
086
087        public int getValue() {
088            return value;
089        }
090    }
091
092    public static class BooleanField extends IoTDataField {
093
094        private final boolean value;
095
096        public BooleanField(String name, boolean value) {
097            super(Type.bool, name);
098            this.value = value;
099        }
100
101        @Override
102        protected String getValueInternal() {
103            return Boolean.toString(value);
104        }
105
106        public boolean getValue() {
107            return value;
108        }
109    }
110}