Skip to content
Snippets Groups Projects
attribute.js 8.03 KiB
Newer Older
/**
 * This module represents an AttributeType.
 * AttributeTypes defines name, type (string, double,...) an associated parameter of an attribute.
 *
 * @module AttributeType
 * @fileOverview
 */
define(['parameterList'], function(ParameterList){
    return (function() {
         * Constructor: Initializes the ParameterList.
         *
         * @classdesc Attribute defines name, type (string, double,...) an associated parameter of an attribute.
         * @requires ParameterList
         * @constructs Attribute
        function Attribute() {
             * Name of the Attribute.
             *
            this._name = '';
             * Defines the type of the Attribute (i.e String, Double,...).
             *
            this._type = '';
            this._parameterList = new ParameterList();
            this._value = 'NO_VALUE';
             * Time when the value was set.
             *
            this._timestamp = new Date();
        /**
         * Builder for name.
         *
         * @param {String} name Name
         * @returns {Attribute}
         */
        Attribute.prototype.withName = function(name){
            this.setName(name);
            return this;
        };
        /**
         * Builder for type.
         *
         * @param {String} type Type
         * @returns {Attribute}
         */
        Attribute.prototype.withType = function(type){
            this.setType(type);
            return this;
        };
        /**
         * Builder for one parameter.
         *
         * @param {Parameter} parameter Parameter
         * @returns {Attribute}
         */
        Attribute.prototype.withParameter = function(parameter){
            this.addParameter(parameter);
            return this;
        };
        /**
         * Builder for parameterList.
         *
         * @param {(ParameterList|Array)} parameterList ParameterList
         * @returns {Attribute}
         */
        Attribute.prototype.withParameters = function(parameterList){
            this.setParameters(parameterList);
            return this;
        };
        /**
         * Builder for value.
         *
         * @param {String} value value
         * @returns {Attribute}
         */
        Attribute.prototype.withValue = function(value) {
            this.setValue(value);
            this.setTimestamp(new Date());
            return this;
        };
        /**
         * Builder for timestamp.
         *
         * @param {Date} timestamp timestamp
         * @returns {Attribute}
         */
        Attribute.prototype.withTimestamp = function(timestamp) {
            this.setTimestamp(timestamp);
            return this;
        };
        /**
         * Returns the name.
         *
         * @returns {string}
         */
        Attribute.prototype.getName = function(){
            return this._name;
        };
        /**
         * Returns the type.
         *
         * @returns {string}
         */
        Attribute.prototype.getType = function(){
            return this._type;
        };
        /**
         * Returns the parameters.
         *
         * @returns {ParameterList}
         */
        Attribute.prototype.getParameters = function(){
            return this._parameterList;
        };
        /**
         * Sets the name.
         *
         * @param {string} name Name
         */
        Attribute.prototype.setName = function(name){
            if(typeof name === 'string'){
                this._name = name;
            }
        };
        /**
         * Sets the type.
         *
         * @param {string} type Type
         */
        Attribute.prototype.setType = function(type){
            if(typeof type === 'string'){
                this._type = type;
            }
        };
        /**
         * Adds a parameter.
         *
         * @param {Parameter} parameter Parameter
         */
        Attribute.prototype.addParameter = function(parameter){
            this._parameterList.put(parameter);
        };
        /**
         * Adds a list of Parameter.
         *
         * @param {ParameterList} parameters ParameterList
         */
        Attribute.prototype.setParameters = function(parameters){
            this._parameterList.putAll(parameters);
        };
        /**
         * Returns true if the attribute is parameterized.
         *
         * @returns {boolean}
         */
        Attribute.prototype.hasParameters = function() {
            return this._parameterList.size() > 0;
        };
        /**
         * Sets the value.
         *
         * @param {string} value value
         * @returns {Attribute}
         */
        Attribute.prototype.setValue = function(value) {
            this._value = value;
            return this;
        };
        /**
         * Returns the value.
         *
         * @returns {string}
         */
        Attribute.prototype.getValue = function() {
            return this._value;
        };
        /**
         * Sets the timestamp.
         *
         * @param {Date} time timestamp
         */
        Attribute.prototype.setTimestamp = function(time) {
            this._timestamp = time;
        };
        /**
         * Returns the timestamp.
         *
         * @returns {Number}
         */
        Attribute.prototype.getTimestamp = function() {
            return this._timestamp;
        };
        /**
         *
         * @returns {boolean}
         */
        Attribute.prototype.hasInputParameter = function() {
            return this.hasParameters() && this._parameterList.hasInputParameter();
        };
        /**
         * Compares this instance with the given one.
         *
         * @param {Attribute} attribute Attribute that should be compared.
         * @returns {boolean}
         */
        Attribute.prototype.equalsTypeOf = function(attribute) {
            if (attribute.constructor === Attribute) {
                if (this.getName() == attribute.getName() && this.getType() == attribute.getType() && this.getParameters().equals(attribute.getParameters())) {
                    return true;
        /**
         *
         * @param {Attribute} attribute
         * @returns {Boolean}
         */
        Attribute.prototype.equalsValueOf = function(attribute) {
            if (attribute.constructor === Attribute) {
                if (this.equalsTypeOf(attribute) && this.getValue() == attribute.getValue()) {
                    return true;
        /**
         * Returns an identifier that uniquely describes the attribute type and its parameters.
         * The identifier can be used to compare two attribute types. <br/>
         * Format: (AttributeName:AttributeType)#[FirstParameterName:FirstParameterValue]…
         *
         * @returns {String}
         * @example (CI_USER_LOCATION_DISTANCE:FLOAT)#[CP_TARGET_LATITUDE:52][CP_TARGET_LONGITUDE:13][CP_UNIT:KILOMETERS]
         */
        Attribute.prototype.toString = function(typeOnly) {
            var identifier = "(" + this.getName() + ":" + this.getType() + ")";
            if (this.hasParameters()) {
                identifier += "#";
                for (var index in this.getParameters().getItems()) {
                    var theParameter = this.getParameters().getItems()[index];
                    identifier += theParameter.toString();
            if (!typeOnly) identifier += ":" + this.getValue();
            return identifier;
        };