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