ConstantProperty.js 3.3 KB

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