ConstantProperty.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import defined from '../Core/defined.js';
  2. import defineProperties from '../Core/defineProperties.js';
  3. import Event from '../Core/Event.js';
  4. /**
  5. * A {@link Property} whose value does not change with respect to simulation time.
  6. *
  7. * @alias ConstantProperty
  8. * @constructor
  9. *
  10. * @param {*} [value] The property value.
  11. *
  12. * @see ConstantPositionProperty
  13. */
  14. function ConstantProperty(value) {
  15. this._value = undefined;
  16. this._hasClone = false;
  17. this._hasEquals = false;
  18. this._definitionChanged = new Event();
  19. this.setValue(value);
  20. }
  21. defineProperties(ConstantProperty.prototype, {
  22. /**
  23. * Gets a value indicating if this property is constant.
  24. * This property always returns <code>true</code>.
  25. * @memberof ConstantProperty.prototype
  26. *
  27. * @type {Boolean}
  28. * @readonly
  29. */
  30. isConstant : {
  31. value : true
  32. },
  33. /**
  34. * Gets the event that is raised whenever the definition of this property changes.
  35. * The definition is changed whenever setValue is called with data different
  36. * than the current value.
  37. * @memberof ConstantProperty.prototype
  38. *
  39. * @type {Event}
  40. * @readonly
  41. */
  42. definitionChanged : {
  43. get : function() {
  44. return this._definitionChanged;
  45. }
  46. }
  47. });
  48. /**
  49. * Gets the value of the property.
  50. *
  51. * @param {JulianDate} [time] The time for which to retrieve the value. This parameter is unused since the value does not change with respect to time.
  52. * @param {Object} [result] The object to store the value into, if omitted, a new instance is created and returned.
  53. * @returns {Object} The modified result parameter or a new instance if the result parameter was not supplied.
  54. */
  55. ConstantProperty.prototype.getValue = function(time, result) {
  56. return this._hasClone ? this._value.clone(result) : this._value;
  57. };
  58. /**
  59. * Sets the value of the property.
  60. *
  61. * @param {*} value The property value.
  62. */
  63. ConstantProperty.prototype.setValue = function(value) {
  64. var oldValue = this._value;
  65. if (oldValue !== value) {
  66. var isDefined = defined(value);
  67. var hasClone = isDefined && typeof value.clone === 'function';
  68. var hasEquals = isDefined && typeof value.equals === 'function';
  69. var changed = !hasEquals || !value.equals(oldValue);
  70. if (changed) {
  71. this._hasClone = hasClone;
  72. this._hasEquals = hasEquals;
  73. this._value = !hasClone ? value : value.clone(this._value);
  74. this._definitionChanged.raiseEvent(this);
  75. }
  76. }
  77. };
  78. /**
  79. * Compares this property to the provided property and returns
  80. * <code>true</code> if they are equal, <code>false</code> otherwise.
  81. *
  82. * @param {Property} [other] The other property.
  83. * @returns {Boolean} <code>true</code> if left and right are equal, <code>false</code> otherwise.
  84. */
  85. ConstantProperty.prototype.equals = function(other) {
  86. return this === other || //
  87. (other instanceof ConstantProperty && //
  88. ((!this._hasEquals && (this._value === other._value)) || //
  89. (this._hasEquals && this._value.equals(other._value))));
  90. };
  91. /**
  92. * Gets this property's value.
  93. *
  94. * @returns {*} This property's value.
  95. */
  96. ConstantProperty.prototype.valueOf = function() {
  97. return this._value;
  98. };
  99. /**
  100. * Creates a string representing this property's value.
  101. *
  102. * @returns {String} A string representing the property's value.
  103. */
  104. ConstantProperty.prototype.toString = function() {
  105. return String(this._value);
  106. };
  107. export default ConstantProperty;