All Downloads are FREE. Search and download functionalities are using the official Maven repository.

brooklyn.event.basic.BasicSensor Maven / Gradle / Ivy

There is a newer version: 0.7.0-M1
Show newest version
package brooklyn.event.basic;

import static com.google.common.base.Preconditions.checkNotNull;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import brooklyn.entity.Entity;
import brooklyn.event.Sensor;
import brooklyn.event.SensorEvent;

import com.google.common.base.Objects;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;
import com.google.common.reflect.TypeToken;

/**
 * Parent for all {@link Sensor}s.
 */
public class BasicSensor implements Sensor {
    private static final long serialVersionUID = -3762018534086101323L;
    private static final Logger LOG = LoggerFactory.getLogger(BasicSensor.class);
    
    private static final Splitter dots = Splitter.on('.');

    private TypeToken typeToken;
    private Class type;
    private String name;
    private String description;
    private transient List nameParts;
    
    // FIXME In groovy, fields were `public final` with a default constructor; do we need the gson?
    public BasicSensor() { /* for gson */ }

    /** name is typically a dot-separated identifier; description is optional */
    public BasicSensor(Class type, String name) {
        this(type, name, name);
    }
    
    public BasicSensor(Class type, String name, String description) {
        this(TypeToken.of(type), name, description);
    }
    
    public BasicSensor(TypeToken typeToken, String name, String description) {
        this.typeToken = checkNotNull(typeToken, "typeToken");
        this.type = typeToken.getRawType();
        this.name = checkNotNull(name, "name");
        this.description = description;
    }

    /** @see Sensor#getTypeToken() */
    public TypeToken getTypeToken() { return typeToken; }
    
    /** @see Sensor#getType() */
    public Class getType() { return type; }
 
    /** @see Sensor#getTypeName() */
    public String getTypeName() { 
        return type.getName();
    }
 
    /** @see Sensor#getName() */
    public String getName() { return name; }
 
    /** @see Sensor#getNameParts() */
    public synchronized List getNameParts() {
        if (nameParts==null) nameParts = ImmutableList.copyOf(dots.split(name));
        return nameParts; 
    }
 
    /** @see Sensor#getDescription() */
    public String getDescription() { return description; }
    
    /** @see Sensor#newEvent(Entity, Object) */
    public SensorEvent newEvent(Entity producer, T value) {
        return new BasicSensorEvent(this, producer, value);
    }
    
    @Override
    public int hashCode() {
        return Objects.hashCode(getTypeName(), name, description);
    }
 
    @Override
    public boolean equals(Object other) {
        if (this==other) return true;
        if (!(other instanceof BasicSensor)) return false;
        BasicSensor o = (BasicSensor) other;
        
        return Objects.equal(getTypeName(), o.getTypeName()) && Objects.equal(name, o.name) && Objects.equal(description, o.description);
    }
    
    @Override
    public String toString() {
        return String.format("Sensor: %s (%s)", name, getTypeName());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy