openhab-scripting 4.47.0 → 5.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (281) hide show
  1. checksums.yaml +4 -4
  2. data/lib/openhab/core/actions/audio.rb +47 -0
  3. data/lib/openhab/core/actions/ephemeris.rb +39 -0
  4. data/lib/openhab/core/actions/exec.rb +51 -0
  5. data/lib/openhab/core/actions/http.rb +80 -0
  6. data/lib/openhab/core/actions/ping.rb +30 -0
  7. data/lib/openhab/core/actions/transformation.rb +32 -0
  8. data/lib/openhab/core/actions/voice.rb +36 -0
  9. data/lib/openhab/core/actions.rb +82 -0
  10. data/lib/openhab/core/dependency_tracking.rb +34 -0
  11. data/lib/openhab/core/dto/item_channel_link.rb +33 -0
  12. data/lib/openhab/core/dto/thing.rb +27 -0
  13. data/lib/openhab/core/dto.rb +11 -0
  14. data/lib/openhab/core/entity_lookup.rb +152 -70
  15. data/lib/openhab/core/events/abstract_event.rb +18 -0
  16. data/lib/openhab/core/events/abstract_item_registry_event.rb +36 -0
  17. data/lib/openhab/core/events/abstract_thing_registry_event.rb +40 -0
  18. data/lib/openhab/core/events/item_command_event.rb +78 -0
  19. data/lib/openhab/core/events/item_event.rb +22 -0
  20. data/lib/openhab/core/events/item_state_changed_event.rb +75 -0
  21. data/lib/openhab/core/events/item_state_event.rb +79 -0
  22. data/lib/openhab/core/events/thing_status_info_event.rb +55 -0
  23. data/lib/openhab/core/events.rb +10 -0
  24. data/lib/openhab/core/items/accepted_data_types.rb +29 -0
  25. data/lib/openhab/core/items/color_item.rb +52 -0
  26. data/lib/openhab/core/items/contact_item.rb +52 -0
  27. data/lib/openhab/core/items/date_time_item.rb +59 -0
  28. data/lib/openhab/core/items/dimmer_item.rb +148 -0
  29. data/lib/openhab/core/items/generic_item.rb +292 -0
  30. data/lib/openhab/core/items/group_item.rb +176 -0
  31. data/lib/openhab/{dsl → core}/items/image_item.rb +35 -29
  32. data/lib/openhab/core/items/item.rb +273 -0
  33. data/lib/openhab/core/items/location_item.rb +34 -0
  34. data/lib/openhab/core/items/metadata/hash.rb +433 -0
  35. data/lib/openhab/core/items/metadata/namespace_hash.rb +475 -0
  36. data/lib/openhab/core/items/metadata/provider.rb +48 -0
  37. data/lib/openhab/core/items/metadata.rb +11 -0
  38. data/lib/openhab/core/items/number_item.rb +62 -0
  39. data/lib/openhab/core/items/numeric_item.rb +22 -0
  40. data/lib/openhab/core/items/persistence.rb +416 -0
  41. data/lib/openhab/core/items/player_item.rb +66 -0
  42. data/lib/openhab/core/items/provider.rb +44 -0
  43. data/lib/openhab/core/items/proxy.rb +136 -0
  44. data/lib/openhab/core/items/registry.rb +86 -0
  45. data/lib/openhab/core/items/rollershutter_item.rb +68 -0
  46. data/lib/openhab/core/items/semantics/enumerable.rb +177 -0
  47. data/lib/openhab/core/items/semantics.rb +473 -0
  48. data/lib/openhab/core/items/state_storage.rb +53 -0
  49. data/lib/openhab/core/items/string_item.rb +28 -0
  50. data/lib/openhab/core/items/switch_item.rb +78 -0
  51. data/lib/openhab/core/items.rb +108 -0
  52. data/lib/openhab/{dsl → core}/lazy_array.rb +9 -3
  53. data/lib/openhab/core/profile_factory.rb +132 -0
  54. data/lib/openhab/core/provider.rb +230 -0
  55. data/lib/openhab/core/proxy.rb +130 -0
  56. data/lib/openhab/core/registry.rb +40 -0
  57. data/lib/openhab/core/rules/module.rb +26 -0
  58. data/lib/openhab/core/rules/provider.rb +25 -0
  59. data/lib/openhab/core/rules/registry.rb +76 -0
  60. data/lib/openhab/core/rules/rule.rb +150 -0
  61. data/lib/openhab/core/rules.rb +25 -0
  62. data/lib/openhab/core/script_handling.rb +78 -20
  63. data/lib/openhab/core/things/channel.rb +48 -0
  64. data/lib/openhab/core/things/channel_uid.rb +51 -0
  65. data/lib/openhab/core/things/item_channel_link.rb +33 -0
  66. data/lib/openhab/core/things/links/provider.rb +78 -0
  67. data/lib/openhab/core/things/profile_callback.rb +52 -0
  68. data/lib/openhab/core/things/provider.rb +29 -0
  69. data/lib/openhab/core/things/proxy.rb +87 -0
  70. data/lib/openhab/core/things/registry.rb +73 -0
  71. data/lib/openhab/core/things/thing.rb +194 -0
  72. data/lib/openhab/core/things.rb +22 -0
  73. data/lib/openhab/core/timer.rb +148 -0
  74. data/lib/openhab/{dsl → core}/types/comparable_type.rb +5 -3
  75. data/lib/openhab/{dsl → core}/types/date_time_type.rb +55 -127
  76. data/lib/openhab/{dsl → core}/types/decimal_type.rb +50 -48
  77. data/lib/openhab/{dsl → core}/types/hsb_type.rb +35 -83
  78. data/lib/openhab/core/types/increase_decrease_type.rb +34 -0
  79. data/lib/openhab/core/types/next_previous_type.rb +34 -0
  80. data/lib/openhab/{dsl → core}/types/numeric_type.rb +20 -7
  81. data/lib/openhab/core/types/on_off_type.rb +46 -0
  82. data/lib/openhab/core/types/open_closed_type.rb +41 -0
  83. data/lib/openhab/{dsl → core}/types/percent_type.rb +19 -20
  84. data/lib/openhab/core/types/play_pause_type.rb +38 -0
  85. data/lib/openhab/core/types/point_type.rb +117 -0
  86. data/lib/openhab/core/types/quantity_type.rb +325 -0
  87. data/lib/openhab/core/types/raw_type.rb +26 -0
  88. data/lib/openhab/core/types/refresh_type.rb +27 -0
  89. data/lib/openhab/core/types/rewind_fastforward_type.rb +38 -0
  90. data/lib/openhab/core/types/stop_move_type.rb +34 -0
  91. data/lib/openhab/{dsl → core}/types/string_type.rb +17 -28
  92. data/lib/openhab/{dsl → core}/types/type.rb +42 -40
  93. data/lib/openhab/core/types/un_def_type.rb +38 -0
  94. data/lib/openhab/core/types/up_down_type.rb +50 -0
  95. data/lib/openhab/core/types.rb +82 -0
  96. data/lib/openhab/{dsl → core}/uid.rb +4 -23
  97. data/lib/openhab/core/value_cache.rb +188 -0
  98. data/lib/openhab/core.rb +98 -0
  99. data/lib/openhab/core_ext/between.rb +32 -0
  100. data/lib/openhab/core_ext/ephemeris.rb +53 -0
  101. data/lib/openhab/core_ext/java/class.rb +34 -0
  102. data/lib/openhab/core_ext/java/duration.rb +142 -0
  103. data/lib/openhab/core_ext/java/list.rb +436 -0
  104. data/lib/openhab/core_ext/java/local_date.rb +104 -0
  105. data/lib/openhab/core_ext/java/local_time.rb +118 -0
  106. data/lib/openhab/core_ext/java/map.rb +66 -0
  107. data/lib/openhab/core_ext/java/month.rb +71 -0
  108. data/lib/openhab/core_ext/java/month_day.rb +119 -0
  109. data/lib/openhab/core_ext/java/period.rb +103 -0
  110. data/lib/openhab/core_ext/java/temporal_amount.rb +34 -0
  111. data/lib/openhab/core_ext/java/time.rb +62 -0
  112. data/lib/openhab/core_ext/java/unit.rb +15 -0
  113. data/lib/openhab/core_ext/java/zoned_date_time.rb +213 -0
  114. data/lib/openhab/core_ext/ruby/array.rb +21 -0
  115. data/lib/openhab/core_ext/ruby/date.rb +96 -0
  116. data/lib/openhab/core_ext/ruby/date_time.rb +55 -0
  117. data/lib/openhab/core_ext/ruby/module.rb +15 -0
  118. data/lib/openhab/core_ext/ruby/numeric.rb +195 -0
  119. data/lib/openhab/core_ext/ruby/range.rb +70 -0
  120. data/lib/openhab/core_ext/ruby/symbol.rb +7 -0
  121. data/lib/openhab/core_ext/ruby/time.rb +108 -0
  122. data/lib/openhab/core_ext.rb +18 -0
  123. data/lib/openhab/dsl/debouncer.rb +259 -0
  124. data/lib/openhab/dsl/events/watch_event.rb +18 -0
  125. data/lib/openhab/dsl/events.rb +9 -0
  126. data/lib/openhab/dsl/gems.rb +1 -1
  127. data/lib/openhab/dsl/items/builder.rb +578 -0
  128. data/lib/openhab/dsl/items/ensure.rb +73 -82
  129. data/lib/openhab/dsl/items/timed_command.rb +214 -159
  130. data/lib/openhab/dsl/rules/automation_rule.rb +126 -115
  131. data/lib/openhab/dsl/rules/builder.rb +1935 -0
  132. data/lib/openhab/dsl/rules/guard.rb +51 -114
  133. data/lib/openhab/dsl/rules/name_inference.rb +66 -25
  134. data/lib/openhab/dsl/rules/property.rb +48 -75
  135. data/lib/openhab/dsl/rules/rule_triggers.rb +22 -27
  136. data/lib/openhab/dsl/rules/terse.rb +58 -14
  137. data/lib/openhab/dsl/rules/triggers/changed.rb +48 -94
  138. data/lib/openhab/dsl/rules/triggers/channel.rb +9 -40
  139. data/lib/openhab/dsl/rules/triggers/command.rb +14 -63
  140. data/lib/openhab/dsl/rules/triggers/conditions/duration.rb +34 -69
  141. data/lib/openhab/dsl/rules/triggers/conditions/proc.rb +6 -14
  142. data/lib/openhab/dsl/rules/triggers/cron/cron.rb +48 -82
  143. data/lib/openhab/dsl/rules/triggers/cron/cron_handler.rb +30 -47
  144. data/lib/openhab/dsl/rules/triggers/trigger.rb +7 -28
  145. data/lib/openhab/dsl/rules/triggers/updated.rb +21 -45
  146. data/lib/openhab/dsl/rules/triggers/watch/watch_handler.rb +257 -102
  147. data/lib/openhab/dsl/rules/triggers.rb +12 -0
  148. data/lib/openhab/dsl/rules.rb +8 -0
  149. data/lib/openhab/dsl/things/builder.rb +299 -0
  150. data/lib/openhab/{core → dsl}/thread_local.rb +27 -17
  151. data/lib/openhab/dsl/timer_manager.rb +204 -0
  152. data/lib/openhab/dsl/version.rb +9 -0
  153. data/lib/openhab/dsl.rb +979 -0
  154. data/lib/openhab/log.rb +355 -0
  155. data/lib/openhab/osgi.rb +68 -0
  156. data/lib/openhab/rspec/configuration.rb +56 -0
  157. data/lib/openhab/rspec/example_group.rb +132 -0
  158. data/lib/openhab/rspec/helpers.rb +458 -0
  159. data/lib/openhab/rspec/hooks.rb +113 -0
  160. data/lib/openhab/rspec/jruby.rb +46 -0
  161. data/lib/openhab/rspec/karaf.rb +851 -0
  162. data/lib/openhab/rspec/mocks/bundle_install_support.rb +25 -0
  163. data/lib/openhab/rspec/mocks/bundle_resolver.rb +30 -0
  164. data/lib/openhab/rspec/mocks/event_admin.rb +146 -0
  165. data/lib/openhab/rspec/mocks/instance_method_stasher.rb +22 -0
  166. data/lib/openhab/rspec/mocks/persistence_service.rb +155 -0
  167. data/lib/openhab/rspec/mocks/safe_caller.rb +40 -0
  168. data/lib/openhab/rspec/mocks/space.rb +23 -0
  169. data/lib/openhab/rspec/mocks/synchronous_executor.rb +63 -0
  170. data/lib/openhab/rspec/mocks/thing_handler.rb +76 -0
  171. data/lib/openhab/rspec/mocks/timer.rb +134 -0
  172. data/lib/openhab/rspec/openhab/core/actions.rb +38 -0
  173. data/lib/openhab/rspec/openhab/core/items/proxy.rb +15 -0
  174. data/lib/openhab/rspec/openhab/core/things/proxy.rb +27 -0
  175. data/lib/openhab/rspec/shell.rb +31 -0
  176. data/lib/openhab/rspec/suspend_rules.rb +50 -0
  177. data/lib/openhab/rspec.rb +26 -0
  178. data/lib/openhab/yard/base_helper.rb +19 -0
  179. data/lib/openhab/yard/cli/stats.rb +23 -0
  180. data/lib/openhab/yard/code_objects/group_object.rb +23 -0
  181. data/lib/openhab/yard/code_objects/java/base.rb +31 -0
  182. data/lib/openhab/yard/code_objects/java/class_object.rb +11 -0
  183. data/lib/openhab/yard/code_objects/java/field_object.rb +15 -0
  184. data/lib/openhab/yard/code_objects/java/interface_object.rb +15 -0
  185. data/lib/openhab/yard/code_objects/java/package_object.rb +11 -0
  186. data/lib/openhab/yard/code_objects/java/proxy.rb +23 -0
  187. data/lib/openhab/yard/coderay.rb +17 -0
  188. data/lib/openhab/yard/handlers/jruby/base.rb +58 -0
  189. data/lib/openhab/yard/handlers/jruby/class_handler.rb +18 -0
  190. data/lib/openhab/yard/handlers/jruby/constant_handler.rb +18 -0
  191. data/lib/openhab/yard/handlers/jruby/java_import_handler.rb +30 -0
  192. data/lib/openhab/yard/handlers/jruby/mixin_handler.rb +23 -0
  193. data/lib/openhab/yard/html_helper.rb +78 -0
  194. data/lib/openhab/yard/markdown_helper.rb +148 -0
  195. data/lib/openhab/yard/tags/constant_directive.rb +20 -0
  196. data/lib/openhab/yard/tags/group_directive.rb +24 -0
  197. data/lib/openhab/yard/tags/library.rb +3 -0
  198. data/lib/openhab/yard.rb +38 -0
  199. metadata +475 -106
  200. data/lib/openhab/core/item_proxy.rb +0 -29
  201. data/lib/openhab/core/load_path.rb +0 -19
  202. data/lib/openhab/core/openhab_setup.rb +0 -29
  203. data/lib/openhab/core/osgi.rb +0 -58
  204. data/lib/openhab/core/services.rb +0 -24
  205. data/lib/openhab/dsl/actions.rb +0 -114
  206. data/lib/openhab/dsl/between.rb +0 -25
  207. data/lib/openhab/dsl/channel.rb +0 -43
  208. data/lib/openhab/dsl/dsl.rb +0 -59
  209. data/lib/openhab/dsl/group.rb +0 -54
  210. data/lib/openhab/dsl/imports.rb +0 -21
  211. data/lib/openhab/dsl/items/color_item.rb +0 -76
  212. data/lib/openhab/dsl/items/comparable_item.rb +0 -62
  213. data/lib/openhab/dsl/items/contact_item.rb +0 -41
  214. data/lib/openhab/dsl/items/date_time_item.rb +0 -65
  215. data/lib/openhab/dsl/items/dimmer_item.rb +0 -65
  216. data/lib/openhab/dsl/items/generic_item.rb +0 -229
  217. data/lib/openhab/dsl/items/group_item.rb +0 -127
  218. data/lib/openhab/dsl/items/item_equality.rb +0 -59
  219. data/lib/openhab/dsl/items/item_registry.rb +0 -54
  220. data/lib/openhab/dsl/items/items.rb +0 -109
  221. data/lib/openhab/dsl/items/location_item.rb +0 -59
  222. data/lib/openhab/dsl/items/metadata.rb +0 -326
  223. data/lib/openhab/dsl/items/number_item.rb +0 -17
  224. data/lib/openhab/dsl/items/numeric_item.rb +0 -87
  225. data/lib/openhab/dsl/items/persistence.rb +0 -307
  226. data/lib/openhab/dsl/items/player_item.rb +0 -58
  227. data/lib/openhab/dsl/items/rollershutter_item.rb +0 -51
  228. data/lib/openhab/dsl/items/semantics/enumerable.rb +0 -91
  229. data/lib/openhab/dsl/items/semantics.rb +0 -227
  230. data/lib/openhab/dsl/items/string_item.rb +0 -51
  231. data/lib/openhab/dsl/items/switch_item.rb +0 -70
  232. data/lib/openhab/dsl/monkey_patch/actions/actions.rb +0 -4
  233. data/lib/openhab/dsl/monkey_patch/actions/script_thing_actions.rb +0 -39
  234. data/lib/openhab/dsl/monkey_patch/events/events.rb +0 -7
  235. data/lib/openhab/dsl/monkey_patch/events/item_command.rb +0 -85
  236. data/lib/openhab/dsl/monkey_patch/events/item_event.rb +0 -28
  237. data/lib/openhab/dsl/monkey_patch/events/item_state.rb +0 -61
  238. data/lib/openhab/dsl/monkey_patch/events/item_state_changed.rb +0 -60
  239. data/lib/openhab/dsl/monkey_patch/events/thing_status_info.rb +0 -33
  240. data/lib/openhab/dsl/monkey_patch/java/java.rb +0 -4
  241. data/lib/openhab/dsl/monkey_patch/java/local_time.rb +0 -44
  242. data/lib/openhab/dsl/monkey_patch/java/time_extensions.rb +0 -50
  243. data/lib/openhab/dsl/monkey_patch/java/zoned_date_time.rb +0 -45
  244. data/lib/openhab/dsl/monkey_patch/ruby/number.rb +0 -104
  245. data/lib/openhab/dsl/monkey_patch/ruby/ruby.rb +0 -6
  246. data/lib/openhab/dsl/monkey_patch/ruby/string.rb +0 -47
  247. data/lib/openhab/dsl/monkey_patch/ruby/time.rb +0 -61
  248. data/lib/openhab/dsl/openhab.rb +0 -30
  249. data/lib/openhab/dsl/persistence.rb +0 -27
  250. data/lib/openhab/dsl/rules/item_event.rb +0 -19
  251. data/lib/openhab/dsl/rules/rule.rb +0 -160
  252. data/lib/openhab/dsl/rules/rule_config.rb +0 -147
  253. data/lib/openhab/dsl/rules/triggers/generic.rb +0 -31
  254. data/lib/openhab/dsl/rules/triggers/triggers.rb +0 -11
  255. data/lib/openhab/dsl/rules/triggers/watch/watch.rb +0 -81
  256. data/lib/openhab/dsl/states.rb +0 -89
  257. data/lib/openhab/dsl/things.rb +0 -147
  258. data/lib/openhab/dsl/time/month_day.rb +0 -180
  259. data/lib/openhab/dsl/time/time_of_day.rb +0 -235
  260. data/lib/openhab/dsl/timers/manager.rb +0 -119
  261. data/lib/openhab/dsl/timers/reentrant_timer.rb +0 -38
  262. data/lib/openhab/dsl/timers/timer.rb +0 -132
  263. data/lib/openhab/dsl/timers.rb +0 -77
  264. data/lib/openhab/dsl/types/increase_decrease_type.rb +0 -23
  265. data/lib/openhab/dsl/types/next_previous_type.rb +0 -23
  266. data/lib/openhab/dsl/types/on_off_type.rb +0 -28
  267. data/lib/openhab/dsl/types/open_closed_type.rb +0 -29
  268. data/lib/openhab/dsl/types/play_pause_type.rb +0 -27
  269. data/lib/openhab/dsl/types/point_type.rb +0 -180
  270. data/lib/openhab/dsl/types/quantity_type.rb +0 -265
  271. data/lib/openhab/dsl/types/refresh_type.rb +0 -18
  272. data/lib/openhab/dsl/types/rewind_fastforward_type.rb +0 -33
  273. data/lib/openhab/dsl/types/stop_move_type.rb +0 -23
  274. data/lib/openhab/dsl/types/types.rb +0 -83
  275. data/lib/openhab/dsl/types/un_def_type.rb +0 -22
  276. data/lib/openhab/dsl/types/up_down_type.rb +0 -32
  277. data/lib/openhab/dsl/units.rb +0 -45
  278. data/lib/openhab/log/configuration.rb +0 -21
  279. data/lib/openhab/log/logger.rb +0 -282
  280. data/lib/openhab/version.rb +0 -9
  281. data/lib/openhab.rb +0 -38
@@ -0,0 +1,142 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OpenHAB
4
+ module CoreExt
5
+ module Java
6
+ Duration = java.time.Duration
7
+
8
+ # Extensions to {java.time.Duration Java Duration}
9
+ class Duration
10
+ include Between
11
+ # @!parse include TemporalAmount
12
+
13
+ #
14
+ # Convert to integer number of seconds
15
+ #
16
+ # @return [Integer]
17
+ #
18
+ alias_method :to_i, :seconds
19
+
20
+ #
21
+ # @!method zero?
22
+ # @return [true,false] Returns true if the duration is zero length.
23
+ #
24
+
25
+ #
26
+ # @!method negative?
27
+ # @return [true,false] Returns true if the duration is less than zero.
28
+ #
29
+
30
+ unless instance_methods.include?(:positive?)
31
+ #
32
+ # @return [true, false] Returns true if the duration is greater than zero.
33
+ #
34
+ def positive?
35
+ self > 0 # rubocop:disable Style/NumericPredicate
36
+ end
37
+ end
38
+
39
+ #
40
+ # Convert to number of seconds
41
+ #
42
+ # @return [Float]
43
+ #
44
+ def to_f
45
+ to_i + (nano / 1_000_000_000.0)
46
+ end
47
+
48
+ remove_method :==
49
+
50
+ # @return [Integer, nil]
51
+ def <=>(other)
52
+ return to_f <=> other if other.is_a?(Numeric)
53
+
54
+ super
55
+ rescue TypeError
56
+ nil
57
+ end
58
+
59
+ #
60
+ # Converts `other` to {Duration}, if possible.
61
+ #
62
+ # @param [Numeric, Period] other
63
+ # @return [Array, nil]
64
+ #
65
+ def coerce(other)
66
+ return [other.seconds, self] if other.is_a?(Numeric)
67
+ return [other.to_i.seconds, self] if other.is_a?(Period)
68
+ end
69
+
70
+ {
71
+ plus: :+,
72
+ minus: :-
73
+ }.each do |java_op, ruby_op|
74
+ # def +(other)
75
+ # if other.is_a?(Duration)
76
+ # plus(other)
77
+ # elsif other.is_a?(Integer)
78
+ # plus_seconds(other)
79
+ # elsif other.is_a?(Numeric)
80
+ # plus(other.seconds)
81
+ # elsif other.respond_to?(:coerce) && (rhs, lhs = other.coerce(self))
82
+ # lhs + rhs
83
+ # else
84
+ # raise TypeError, "#{other.class} can't be coerced into Duration"
85
+ # end
86
+ # end
87
+ class_eval <<~RUBY, __FILE__, __LINE__ + 1 # rubocop:disable Style/DocumentDynamicEvalDefinition
88
+ def #{ruby_op}(other)
89
+ if other.is_a?(Duration)
90
+ #{java_op}(other)
91
+ elsif other.is_a?(Integer)
92
+ #{java_op}_seconds(other)
93
+ elsif other.is_a?(Numeric)
94
+ #{java_op}(other.seconds)
95
+ elsif other.respond_to?(:coerce) && (rhs, lhs = other.coerce(self))
96
+ lhs #{ruby_op} rhs
97
+ else
98
+ raise TypeError, "\#{other.class} can't be coerced into Duration"
99
+ end
100
+ end
101
+ RUBY
102
+ end
103
+
104
+ {
105
+ multipliedBy: :*,
106
+ dividedBy: :/
107
+ }.each do |java_op, ruby_op|
108
+ # def *(other)
109
+ # if other.is_a?(Integer)
110
+ # multipliedBy(other)
111
+ # elsif other.is_a?(Numeric)
112
+ # Duration.of_seconds(to_f * other)
113
+ # elsif other.is_a?(Duration)
114
+ # Duration.of_seconds(to_f * other.to_f)
115
+ # elsif other.respond_to?(:coerce) && (rhs, lhs = other.coerce(self))
116
+ # lhs * rhs
117
+ # else
118
+ # raise TypeError, "#{other.class} can't be coerced into Duration"
119
+ # end
120
+ # end
121
+ class_eval <<~RUBY, __FILE__, __LINE__ + 1 # rubocop:disable Style/DocumentDynamicEvalDefinition
122
+ def #{ruby_op}(other)
123
+ if other.is_a?(Integer)
124
+ #{java_op}(other)
125
+ elsif other.is_a?(Numeric)
126
+ Duration.of_seconds(to_f #{ruby_op} other)
127
+ elsif other.is_a?(Duration)
128
+ Duration.of_seconds(to_f #{ruby_op} other.to_f)
129
+ elsif other.respond_to?(:coerce) && (rhs, lhs = other.coerce(self))
130
+ lhs #{ruby_op} rhs
131
+ else
132
+ raise TypeError, "\#{other.class} can't be coerced into Duration"
133
+ end
134
+ end
135
+ RUBY
136
+ end
137
+ end
138
+ end
139
+ end
140
+ end
141
+
142
+ # @!parse Duration = OpenHAB::CoreExt::Java::Duration
@@ -0,0 +1,436 @@
1
+ # frozen_string_literal: true
2
+
3
+ # @!visibility private
4
+ module Java::JavaUtil::List # rubocop:disable Style/ClassAndModuleChildren
5
+ extend Forwardable
6
+
7
+ def_delegators :to_a,
8
+ :&,
9
+ :*,
10
+ :|,
11
+ :bsearch_index,
12
+ :difference,
13
+ :flatten,
14
+ :intersection,
15
+ :pack,
16
+ :product,
17
+ :repeated_combination,
18
+ :repeated_permutation,
19
+ :reverse,
20
+ :rotate,
21
+ :sample,
22
+ :shelljoin,
23
+ :shuffle,
24
+ :transpose,
25
+ :union
26
+
27
+ def assoc(obj)
28
+ find { |v| (v.is_a?(Array) || v.is_a?(self.class)) && v[0] == obj }
29
+ end
30
+
31
+ def at(index)
32
+ self[index]
33
+ end
34
+
35
+ def bsearch(&block)
36
+ raise NotImplementedError unless block
37
+
38
+ r = bsearch_index(&block)
39
+ self[r] if r
40
+ end
41
+
42
+ def combination(n, &block) # rubocop:disable Naming/MethodParameterName
43
+ r = to_a.combination(n, &block)
44
+ block ? self : r
45
+ end
46
+
47
+ def compact
48
+ reject(&:nil?)
49
+ end
50
+
51
+ def compact!
52
+ reject!(&:nil?)
53
+ end
54
+
55
+ def concat(*other_arrays)
56
+ other_arrays.each { |array| add_all(array) }
57
+ self
58
+ end
59
+
60
+ def deconstruct
61
+ self
62
+ end
63
+
64
+ def delete(obj)
65
+ last = nil
66
+ found = false
67
+ loop do
68
+ i = index(obj)
69
+ break if i.nil?
70
+
71
+ found = true
72
+ last = remove(i)
73
+ end
74
+
75
+ return yield(obj) if !found && block_given?
76
+
77
+ last
78
+ end
79
+
80
+ def delete_at(index)
81
+ index = length + index if index.negative?
82
+ return if index.negative? || index >= length
83
+
84
+ remove(index)
85
+ end
86
+
87
+ def delete_if
88
+ raise NotImplementedError unless block_given?
89
+
90
+ it = list_iterator
91
+ while it.has_next? # rubocop:disable Style/WhileUntilModifier
92
+ it.remove if yield(it.next)
93
+ end
94
+ self
95
+ end
96
+
97
+ def dig(index, *identifiers)
98
+ return self[index] if identifiers.empty?
99
+
100
+ self[index]&.dig(*identifiers)
101
+ end
102
+
103
+ def each_index(&block)
104
+ r = (0...length)
105
+ return r.each unless block
106
+
107
+ r.each(&block)
108
+ self
109
+ end
110
+
111
+ def fetch(index, *default_value)
112
+ if default_value.length > 1
113
+ raise ArgumentError,
114
+ "wrong number of arguments calling `fetch` (given #{default_value.length - 1}, expected 1..2)"
115
+ end
116
+
117
+ original_index = index
118
+ index = length + index if index.negative?
119
+ return self[index] if index >= 0 && index < length
120
+
121
+ return default_value.first unless default_value.empty?
122
+ return yield(original_index) if block_given?
123
+
124
+ raise IndexError, "index #{index} out of list"
125
+ end
126
+
127
+ def fill(*args)
128
+ if block_given?
129
+ unless (0..2).cover?(args.length)
130
+ raise ArgumentError,
131
+ "wrong number of arguments calling `fill` (given #{args.length}, expected 1..3)"
132
+ end
133
+ else
134
+ unless (1..3).cover?(args.length)
135
+ raise ArgumentError,
136
+ "wrong number of arguments calling `fill` (given #{args.length}, expected 1..2)"
137
+ end
138
+ end
139
+
140
+ obj = args.shift unless block_given?
141
+
142
+ if args.length == 1 && args.first.is_a?(Range)
143
+ range = args.first
144
+
145
+ range = Range.new(length + range.begin, range.end, range.exclude_end?) if range.begin.negative?
146
+ range = Range.new(range.begin, length + range.end, range.exclude_end?) if range.end&.negative?
147
+ return self if range.begin.negative? || (range.end && range.end < range.begin)
148
+ else
149
+ first, count = *args
150
+ return self if count&.negative?
151
+
152
+ first ||= 0
153
+
154
+ first = length + first if first.negative?
155
+ range = count ? first...(first + count) : first..
156
+ end
157
+
158
+ add(nil) while length < range.begin && count
159
+
160
+ start = range.begin
161
+ start = 0 if start.negative?
162
+ start = length if start > length
163
+ it = list_iterator(start)
164
+
165
+ while range.cover?(it.next_index)
166
+ break if range.end.nil? && !it.has_next?
167
+
168
+ obj = yield(it.next_index) if block_given?
169
+ if it.has_next?
170
+ it.next
171
+ it.set(obj)
172
+ else
173
+ it.add(obj)
174
+ end
175
+ end
176
+
177
+ self
178
+ end
179
+
180
+ def flatten!(*args)
181
+ if args.length > 1
182
+ raise ArgumentError,
183
+ "wrong number of arguments calling `flatten` (given #{args.length}, expect 0..1)"
184
+ end
185
+
186
+ it = list_iterator
187
+
188
+ args = [args.first - 1] unless args.empty?
189
+ done = args.first == 0 # rubocop:disable Style/NumericPredicate
190
+
191
+ changed = false
192
+ while it.has_next?
193
+ element = it.next
194
+ next unless element.respond_to?(:to_ary)
195
+
196
+ changed = true
197
+ it.remove
198
+ arr = element.to_ary
199
+ arr = arr.flatten(*args) unless done
200
+ arr.each do |e|
201
+ it.add(e)
202
+ end
203
+ end
204
+ changed ? self : nil
205
+ end
206
+
207
+ def insert(index, *objects)
208
+ return self if objects.empty?
209
+
210
+ raise IndexError, "index #{index} too small for list, minimum: #{-length}" if index < -length
211
+
212
+ index = length + index + 1 if index.negative?
213
+
214
+ add(nil) while length < index
215
+ add_all(index, objects)
216
+ self
217
+ end
218
+
219
+ def intersect?(other_ary)
220
+ !(self & other_ary).empty?
221
+ end
222
+
223
+ def keep_if?
224
+ raise NotImplementedError unless block_given?
225
+
226
+ it = list_iterator
227
+ while it.has_next? # rubocop:disable Style/WhileUntilModifier
228
+ it.remove unless yield(it.next)
229
+ end
230
+ self
231
+ end
232
+
233
+ def map!(&block)
234
+ raise NotImplementedError unless block
235
+
236
+ replace_all(&block)
237
+ self
238
+ end
239
+
240
+ def permutation(n, &block) # rubocop:disable Naming/MethodParameterName
241
+ r = to_a.permutation(n, &block)
242
+ block ? self : r
243
+ end
244
+
245
+ def pop(*args)
246
+ if args.length > 1
247
+ raise ArgumentError,
248
+ "wrong number of arguments calling `pop` (given #{args.length}, expected 0..1)"
249
+ end
250
+
251
+ if args.empty?
252
+ return if empty?
253
+
254
+ return remove(length - 1)
255
+ end
256
+
257
+ count = args.first
258
+ start = [length - count, 0].max
259
+ result = self[start..-1].to_a # rubocop:disable Style/SlicingWithRange
260
+ it = list_iterator(start)
261
+ while it.has_next?
262
+ it.next
263
+ it.remove
264
+ end
265
+ result
266
+ end
267
+
268
+ def push(*objects)
269
+ add_all(objects)
270
+ self
271
+ end
272
+ alias_method :append, :push
273
+
274
+ def rassoc(obj)
275
+ find { |v| (v.is_a?(Array) || v.is_a?(self.class)) && v[1] == obj }
276
+ end
277
+
278
+ def reject!
279
+ raise NotImplementedError unless block_given?
280
+
281
+ it = list_iterator
282
+ changed = false
283
+ while it.has_next
284
+ if yield(it.next)
285
+ changed = true
286
+ it.remove
287
+ end
288
+ end
289
+ self if changed
290
+ end
291
+
292
+ def repeated_combination(n, &block) # rubocop:disable Naming/MethodParameterName
293
+ r = to_a.repeated_combination(n, &block)
294
+ block ? self : r
295
+ end
296
+
297
+ def repeated_permutation(n, &block) # rubocop:disable Naming/MethodParameterName
298
+ r = to_a.repeated_permutation(n, &block)
299
+ block ? self : r
300
+ end
301
+
302
+ def replace(other_array)
303
+ clear
304
+ add_all(other_array)
305
+ self
306
+ end
307
+
308
+ def reverse!
309
+ replace(reverse)
310
+ self
311
+ end
312
+
313
+ def rotate!(count = 1)
314
+ count = count % length
315
+ push(*shift(count))
316
+ self
317
+ end
318
+
319
+ def select!
320
+ raise NotImplementedError unless block_given?
321
+
322
+ it = list_iterator
323
+ changed = false
324
+ while it.has_next?
325
+ unless yield(it.next)
326
+ changed = true
327
+ it.remove
328
+ end
329
+ end
330
+ self if changed
331
+ end
332
+
333
+ def shift(*args)
334
+ if args.length > 1
335
+ raise ArgumentError,
336
+ "wrong number of arguments calling `shift` (given #{args.length}, expected 0..1)"
337
+ end
338
+
339
+ if args.empty?
340
+ return if empty?
341
+
342
+ return remove(0)
343
+ end
344
+
345
+ count = args.first
346
+ result = self[0...count].to_a
347
+ it = list_iterator
348
+ count.times do
349
+ break unless it.has_next?
350
+
351
+ it.next
352
+ it.remove
353
+ end
354
+ result
355
+ end
356
+
357
+ def shuffle!(*args)
358
+ replace(shuffle(*args))
359
+ end
360
+
361
+ def slice(*args)
362
+ self[*args]
363
+ end
364
+
365
+ def slice!(*args)
366
+ unless (1..2).cover?(args.length)
367
+ raise ArgumentError,
368
+ "wrong number of arguments calling `slice!` (given #{args.length}, expected 1..2)"
369
+ end
370
+
371
+ return delete_at(args.first) if args.length == 1 && !args.first.is_a?(Range)
372
+
373
+ start = args.first
374
+ start = start.begin if start.is_a?(Range)
375
+ start = length + start if start.negative?
376
+ return nil if start.negative? || start >= length
377
+
378
+ result = slice(*args).to_a
379
+
380
+ it = list_iterator(start)
381
+ result.length.times do
382
+ it.next
383
+ it.remove
384
+ end
385
+ result
386
+ end
387
+
388
+ def sort_by!
389
+ raise NotImplementedError unless block_given?
390
+
391
+ sort { |a, b| (yield a) <=> (yield b) }
392
+ self
393
+ end
394
+
395
+ def uniq!
396
+ seen = Set.new
397
+
398
+ it = list_iterator
399
+ changed = false
400
+ while it.has_next?
401
+ n = it.next
402
+ n = yield(n) if block_given?
403
+ if seen.include?(n)
404
+ changed = true
405
+ it.remove
406
+ end
407
+ seen << n
408
+ end
409
+ self if changed
410
+ end
411
+
412
+ def unshift(*objects)
413
+ add_all(0, objects)
414
+ self
415
+ end
416
+ alias_method :prepend, :unshift
417
+
418
+ def values_at(*indexes)
419
+ result = []
420
+ indexes.each do |index|
421
+ if index.is_a?(Range)
422
+ partial_result = self[index]
423
+ result.concat(partial_result)
424
+ result.fill(nil, result.length, index.count - partial_result.length)
425
+ else
426
+ index = length + index if index.negative?
427
+ result << if index.negative? || index >= length
428
+ nil
429
+ else
430
+ self[index]
431
+ end
432
+ end
433
+ end
434
+ result
435
+ end
436
+ end
@@ -0,0 +1,104 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "time"
4
+
5
+ module OpenHAB
6
+ module CoreExt
7
+ module Java
8
+ java_import java.time.LocalDate
9
+
10
+ # Extensions to {java.time.LocalDate}
11
+ class LocalDate
12
+ include Time
13
+ include Between
14
+ include Ephemeris
15
+
16
+ # @!scope class
17
+
18
+ # @!attribute [r] now
19
+ # @return [LocalDate]
20
+
21
+ # @!method parse(text, formatter=nil)
22
+ # Converts the given text into a LocalDate.
23
+ # @param [String] text The text to parse
24
+ # @param [java.time.format.DateTimeFormatter] formatter The formatter to use
25
+ # @return [LocalDate]
26
+
27
+ # @!scope instance
28
+
29
+ # @param [TemporalAmount, LocalDate, Numeric] other
30
+ # If other is a Numeric, it's interpreted as days.
31
+ # @return [LocalDate] If other is a TemporalAmount or Numeric
32
+ # @return [Period] If other is a LocalDate
33
+ def -(other)
34
+ case other
35
+ when Date
36
+ self - other.to_local_date
37
+ when MonthDay
38
+ self - other.at_year(year)
39
+ when LocalDate
40
+ Period.between(other, self)
41
+ when Duration
42
+ minus_days(other.to_days)
43
+ when Numeric
44
+ minus_days(other.ceil)
45
+ else
46
+ minus(other)
47
+ end
48
+ end
49
+
50
+ # @param [TemporalAmount, Numeric] other
51
+ # If other is a Numeric, it's interpreted as days.
52
+ # @return [LocalDate]
53
+ def +(other)
54
+ case other
55
+ when Duration
56
+ plus_days(other.to_days)
57
+ when Numeric
58
+ plus_days(other.to_i)
59
+ else
60
+ plus(other)
61
+ end
62
+ end
63
+
64
+ #
65
+ # Returns the next day
66
+ #
67
+ # @return [LocalDate]
68
+ #
69
+ def succ
70
+ plus_days(1)
71
+ end
72
+
73
+ # @return [Date]
74
+ def to_date
75
+ Date.new(year, month_value, day_of_month)
76
+ end
77
+
78
+ # @return [Month]
79
+ alias_method :to_month, :month
80
+
81
+ # @return [MonthDay]
82
+ def to_month_day
83
+ MonthDay.of(month, day_of_month)
84
+ end
85
+
86
+ # @return [self]
87
+ def to_local_date(_context = nil)
88
+ self
89
+ end
90
+
91
+ # @param [ZonedDateTime, nil] context
92
+ # A {ZonedDateTime} used to fill in missing fields
93
+ # during conversion. {ZonedDateTime.now} is assumed if not given.
94
+ # @return [ZonedDateTime]
95
+ def to_zoned_date_time(context = nil)
96
+ zone = context&.zone || java.time.ZoneId.system_default
97
+ at_start_of_day(zone)
98
+ end
99
+ end
100
+ end
101
+ end
102
+ end
103
+
104
+ LocalDate = OpenHAB::CoreExt::Java::LocalDate unless Object.const_defined?(:LocalDate)