Class: OpenHAB::CoreExt::Java::Duration

Inherits:
Object
  • Object
show all
Includes:
Between, TemporalAmount
Defined in:
lib/openhab/core_ext/java/duration.rb

Overview

Extensions to Java Duration

Ruby's Integer and Float classes are extended to allow convenient creation of Duration instances.

Examples:

5.seconds # => #<Duration PT5S>
2.5.hours # => #<Duration PT2H30M>

Instance Method Summary collapse

Methods included from TemporalAmount

#-@, #ago, #from_now, #inspect, #to_temporal_amount

Methods included from Between

#between?

Instance Method Details

#<=>(other) ⇒ Numeric, ...

Comparisons against other types may be done if supported by that type's coercion.

Returns:



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/openhab/core_ext/java/duration.rb', line 63

def <=>(other)
  logger.trace { "(#{self.class}) #{self} <=> #{other} (#{other.class})" }
  case other
  when Duration then super
  when Numeric then to_f <=> other
  when QuantityType then self <=> other.to_temporal_amount
  else
    if other.respond_to?(:coerce) && (lhs, rhs = other.coerce(self))
      lhs <=> rhs
    else
      super
    end
  end
rescue TypeError
  nil
end

#coerce(other) ⇒ Array?

Converts other to OpenHAB::CoreExt::Java::Duration, if possible.

Parameters:

Returns:



86
87
88
89
90
91
92
# File 'lib/openhab/core_ext/java/duration.rb', line 86

def coerce(other)
  return [other.seconds, self] if other.is_a?(Numeric)
  # We want to return the same type as other, e.g. QuantityType + Duration = QuantityType
  return [other, to_nanos | "ns"] if other.is_a?(QuantityType) && other.unit.compatible?(Units::SECOND)

  [other.to_i.seconds, self] if other.is_a?(Period)
end

#positive?true, false

Returns true if the duration is greater than zero.

Returns:

  • (true, false)

    Returns true if the duration is greater than zero.



42
43
44
# File 'lib/openhab/core_ext/java/duration.rb', line 42

def positive?
  self > 0 # rubocop:disable Style/NumericPredicate
end

#to_fFloat

Convert to number of seconds

Returns:



52
53
54
# File 'lib/openhab/core_ext/java/duration.rb', line 52

def to_f
  to_i + (nano / 1_000_000_000.0)
end

#to_iInteger

Convert to integer number of seconds

Returns:



26
# File 'lib/openhab/core_ext/java/duration.rb', line 26

alias_method :to_i, :seconds

#zero?true, false

Returns true if the duration is zero length.

Returns:

  • (true, false)

    Returns true if the duration is zero length.



# File 'lib/openhab/core_ext/java/duration.rb', line 28