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.discovery;
018
019import java.util.List;
020import java.util.concurrent.CopyOnWriteArrayList;
021
022import org.jivesoftware.smack.util.Async;
023
024import org.jivesoftware.smackx.iot.element.NodeInfo;
025
026import org.jxmpp.jid.BareJid;
027
028public class ThingState {
029
030    private final NodeInfo nodeInfo;
031
032    private BareJid registry;
033    private BareJid owner;
034    private boolean removed;
035
036    private final List<ThingStateChangeListener> listeners = new CopyOnWriteArrayList<>();
037
038    ThingState(NodeInfo nodeInfo) {
039        this.nodeInfo = nodeInfo;
040    }
041
042    void setRegistry(BareJid registry) {
043        this.registry = registry;
044    }
045
046    void setUnregistered() {
047        this.registry = null;
048    }
049
050    void setOwner(final BareJid owner) {
051        this.owner = owner;
052        Async.go(new Runnable() {
053            @Override
054            public void run() {
055                for (ThingStateChangeListener thingStateChangeListener : listeners) {
056                    thingStateChangeListener.owned(owner);
057                }
058            }
059        });
060    }
061
062    void setUnowned() {
063        this.owner = null;
064    }
065
066    void setRemoved() {
067        removed = true;
068    }
069
070    public NodeInfo getNodeInfo() {
071        return nodeInfo;
072    }
073
074    public BareJid getRegistry() {
075        return registry;
076    }
077
078    public BareJid getOwner() {
079        return owner;
080    }
081
082    public boolean isOwned() {
083        return owner != null;
084    }
085
086    public boolean isRemoved() {
087        return removed;
088    }
089
090    public boolean setThingStateChangeListener(ThingStateChangeListener thingStateChangeListener) {
091        return listeners.add(thingStateChangeListener);
092    }
093
094    public boolean removeThingStateChangeListener(ThingStateChangeListener thingStateChangeListener) {
095        return listeners.remove(thingStateChangeListener);
096    }
097}