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