Skip to content
Snippets Groups Projects
attributeList.js 11.5 KiB
Newer Older
define(['abstractList', 'attribute'], function(AbstractList, Attribute) {
    return (function() {
         * @classdesc This class represents a list for Attribute.
         * @extends AbstractList
        function AttributeList() {
            AbstractList.call(this);
            this._type = Attribute;
        AttributeList.prototype = Object.create(AbstractList.prototype);
        AttributeList.prototype.constructor = AttributeList;
        /**
         * Create an AttributeList from the description provided by a Widget or Interpreter.
         *
         * @static
         * @param {Discoverer} discoverer
         * @param {Array} attributeDescription
         * @returns {AttributeList}
         */
        AttributeList.fromAttributeDescription = function(discoverer, attributeDescription) {
            var theAttributeList = new AttributeList();
            for(var attributeDescriptionIndex in attributeDescription) {
                theAttributeList.put(discoverer.buildAttribute(
                    attributeDescription[attributeDescriptionIndex].name,
                    attributeDescription[attributeDescriptionIndex].type,
                    attributeDescription[attributeDescriptionIndex].parameterList,
                    true));
            }
            return theAttributeList;
        };

        /**
         * Adds the specified item to the itemList.
         *
         * @public
         * @param {Attribute} attribute AttributeType
         * @param {boolean} multipleInstances
         */
        AttributeList.prototype.put = function(attribute, multipleInstances) {
            multipleInstances = typeof multipleInstances == "undefined" ? false : multipleInstances;
            if (attribute instanceof this._type) {
                if (multipleInstances || !(this.containsTypeOf(attribute))) {
                    this._items.push(attribute);
                } else {
                    this.updateValue(attribute);
        /**
         * Adds all items in the specified list to the
         * itemList.
         *
         * @public
         * @param {(AttributeList|Array)} attributeList AttributeList
         */
        AttributeList.prototype.putAll = function(attributeList) {
            var list = [];
            if (attributeList instanceof Array) {
                list = attributeList;
            } else if (attributeList.constructor === AttributeList) {
                list = attributeList.getItems();
            }
            for ( var i in list) {
                this.put(list[i]);
            }
        };
         * @deprecated Use containsTypeOf or containsValueOf instead.
         * @param {Attribute} attribute
         * @param {?Boolean} typeOnly
         * @returns {Boolean}
        AttributeList.prototype.contains = function(attribute, typeOnly) {
            typeOnly = typeof typeOnly == "undefined" ? false : typeOnly;
            return typeOnly ? this.containsTypeOf(attribute) : this.containsValueOf(attribute);
        /**
         * Verifies whether an attribute with the type of the given item is included in this list.
         *
         * @param {Attribute} attribute AttributeType that should be verified.
         */
        AttributeList.prototype.containsTypeOf = function(attribute) {
            if (attribute.constructor === Attribute) {
                for (var index in this.getItems()) {
                    var theAttribute = this.getItems()[index];
                    if (theAttribute.equalsTypeOf(attribute)) {
                        return true;
        /**
         * Verifies whether the given item is included in the list.
         *
         * @param {Attribute} attribute AttributeValue that should be verified.
         */
        AttributeList.prototype.containsValueOf = function(attribute) {
            if (attribute.constructor === Attribute) {
                for (var index in this._items) {
                    var theAttribute = this._items[index];
                    if (theAttribute.equalsValueOf(attribute)) {
                        return true;
         * @deprecated Use equalsTypesIn or equalsValuesIn instead.
         * @param {AttributeList} attributeList
         * @param {Boolean} typeOnly
         */
        AttributeList.prototype.equals = function(attributeList, typeOnly) {
            typeOnly = typeof typeOnly == "undefined" ? false : typeOnly;
            return typeOnly ? this.equalsTypesIn(attributeList) : this.equalsValuesIn(attributeList);
        };
        /**
         * Compare the specified AttributeList with this instance.
         *
         * @param {AttributeList} attributeList AttributeList that should be compared.
         * @returns {boolean}
         */
        AttributeList.prototype.equalsTypesIn = function(attributeList) {
            if (attributeList.constructor === AttributeList  && attributeList.size() == this.size()) {
                for (var index in attributeList.getItems()) {
                    var theAttribute = attributeList.getItems()[index];
                    if (!this.containsTypeOf(theAttribute)) return false;
                return true;
            }
            return false;
        };
        /**
         * Compare the specified AttributeList with this instance.
         *
         * @param {AttributeList} attributeList AttributeList that should be compared.
         * @returns {boolean}
         */
        AttributeList.prototype.equalsValuesIn = function(attributeList) {
            if (attributeList.constructor === AttributeList && attributeList.size() == this.size()) {
                for (var index in attributeList.getItems()) {
                    var theAttribute = attributeList.getItems()[index];
                    if (!this.containsValueOf(theAttribute)) return false;
                return true;
            }
            return false;
        };
        /**
         * Returns only this values that matches to the given type.
         *
         * @param {(AttributeList|Array)} attributeList Attributes that should be returned.
         * @returns {AttributeList}
         */
        AttributeList.prototype.getSubset = function(attributeList) {
            var response = new AttributeList();
            var list = [];
            if (attributeList instanceof Array) {
                list = attributeList;
            } else if (attributeList.constructor === AttributeList) {
                list = attributeList.getItems();
            }
            for (var i in list) {
                var theAttribute = list[i];
                if (theAttribute.constructor === Attribute) {
                    var responseAttribute = this.getAttributeWithTypeOf(theAttribute);
                    if (typeof responseAttribute != "undefined") {
                        response.put(responseAttribute);
        /**
         * Returns a subset without the given types.
         *
         * @param {(AttributeList|Array)} attributeList AttributeTypes that should not be included
         * @returns {AttributeList}
         */
        AttributeList.prototype.getSubsetWithoutItems = function(attributeList) {
            var response = this;
            var list = [];
            if (attributeList instanceof Array) {
                list = attributeList;
            } else if (attributeList.constructor === AttributeList) {
                list = attributeList.getItems();
            }
            for (var i in list) {
                var attribute = list[i];
                if (attribute.constructor === Attribute) {
                    response.removeAttributeWithTypeOf(attribute);
        /**
         * Creates a clone of the current list.
         *
         * @param {Boolean} typeOnly
         * @returns {AttributeList}
         */
        AttributeList.prototype.clone = function(typeOnly) {
            var newList = new AttributeList();
            for (var index in this._items) {
                var oldAttribute = this._items[index];
                var newAttribute = new Attribute(true)
                    .withName(oldAttribute.getName())
                    .withType(oldAttribute.getType())
                    .withParameters(oldAttribute.getParameters())
                    .withSynonyms(oldAttribute.getSynonyms());
                if (!typeOnly) newAttribute.setValue(oldAttribute.getValue());
                newList.put(newAttribute);
            }
            return newList;
        };

        /**
         *
         * @param {Attribute} attribute
         * @param {Boolean} allOccurrences
         */
        AttributeList.prototype.removeAttributeWithTypeOf = function(attribute, allOccurrences) {
            allOccurrences = typeof allOccurrences == "undefined" ? false : allOccurrences;
            for (var index in this._items) {
                var theAttribute = this._items[index];
                if (theAttribute.equalsTypeOf(attribute)) {
                    this._items.splice(index, 1);
            }
            if (allOccurrences && this.contains(attribute)) this.removeAttributeWithTypeOf(attribute, allOccurrences);
        };
        /**
         *
         * @returns {boolean}
         */
        AttributeList.prototype.hasAttributesWithInputParameters = function() {
            for (var index in this._items) {
                var theAttribute = this._items[index];
                if (theAttribute.hasInputParameter()) return true;
            }
            return false;
        };
        /**
         *
         * @returns {AttributeList}
         */
        AttributeList.prototype.getAttributesWithInputParameters = function() {
            var list = new AttributeList();
            for (var index in this._items) {
                var theAttribute = this._items[index];
                if (theAttribute.hasInputParameter()) list.put(theAttribute);
            }
            return list;
        };
        /**
         * Returns the attribute value that matches the provided attribute type.
         *
         * @param {AttributeType} attribute
         * @returns {Attribute}
         */
        AttributeList.prototype.getValueForAttributeWithTypeOf = function(attribute) {
            return this.getAttributeWithTypeOf(attribute).getValue();
        };

        /**
         *
         * @param {Attribute} attribute
         * @returns {Attribute}
         */
        AttributeList.prototype.getAttributeWithTypeOf = function(attribute) {
            for (var index in this.getItems()) {
                var theAttribute = this.getItems()[index];
                if (theAttribute.equalsTypeOf(attribute)) return theAttribute;
        /**
         *
         * @param {Attribute} attribute
         */
        AttributeList.prototype.updateValue = function(attribute) {
            for (var index in this._items) {
                var theAttribute = this._items[index];
                if (theAttribute.equalsTypeOf(attribute)) this._items[index] = attribute;
            }
        };