001/*
002# Licensed Materials - Property of IBM
003# Copyright IBM Corp. 2016 
004 */
005package com.ibm.streamsx.iot;
006
007import java.io.IOException;
008import java.io.Serializable;
009
010import com.ibm.json.java.JSONObject;
011import com.ibm.streamsx.iot.spl.Schemas;
012import com.ibm.streamsx.topology.tuple.JSONAble;
013
014/**
015 * A device.
016 *
017 */
018public class Device implements JSONAble, Serializable {
019
020    private static final long serialVersionUID = 1L;
021
022    private final String typeId;
023    private final String id;
024
025    public Device(String typeId, String id) {
026        super();
027        this.typeId = typeId;
028        this.id = id;
029    }
030
031    /**
032     * Get the device type identifier.
033     * 
034     * @return device type identifier.
035     */
036    public String getTypeId() {
037        return typeId;
038    }
039
040    /**
041     * Get the device identifier.
042     * 
043     * @return device identifier.
044     */
045    public String getId() {
046        return id;
047    }
048
049    @Override
050    public JSONObject toJSON() {
051        JSONObject json = new JSONObject();
052        json.put(Schemas.TYPE_ID, getTypeId());
053        json.put(Schemas.DEVICE_ID, getId());
054        return json;
055    }
056
057    @Override
058    public String toString() {
059        try {
060            return toJSON().serialize();
061        } catch (IOException e) {
062            throw new RuntimeException(e);
063        }
064    }
065
066    @Override
067    public int hashCode() {
068        final int prime = 31;
069        int result = 1;
070        result = prime * result + ((id == null) ? 0 : id.hashCode());
071        result = prime * result + ((typeId == null) ? 0 : typeId.hashCode());
072        return result;
073    }
074
075    @Override
076    public boolean equals(Object obj) {
077        if (this == obj)
078            return true;
079        if (obj == null)
080            return false;
081        if (getClass() != obj.getClass())
082            return false;
083        Device other = (Device) obj;
084        if (id == null) {
085            if (other.id != null)
086                return false;
087        } else if (!id.equals(other.id))
088            return false;
089        if (typeId == null) {
090            if (other.typeId != null)
091                return false;
092        } else if (!typeId.equals(other.typeId))
093            return false;
094        return true;
095    }
096}