jactive_support 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (133) hide show
  1. data/.gitignore +4 -0
  2. data/.rvmrc +63 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +20 -0
  5. data/Rakefile +45 -0
  6. data/jactive_support.gemspec +24 -0
  7. data/lib/jactive_support.rb +6 -0
  8. data/lib/jactive_support/constantize.rb +19 -0
  9. data/lib/jactive_support/core_ext.rb +4 -0
  10. data/lib/jactive_support/core_ext/enum.rb +43 -0
  11. data/lib/jactive_support/core_ext/hash.rb +2 -0
  12. data/lib/jactive_support/core_ext/hash/assert_valid_params.rb +19 -0
  13. data/lib/jactive_support/core_ext/hash/with_keys_values.rb +9 -0
  14. data/lib/jactive_support/core_ext/locale.rb +28 -0
  15. data/lib/jactive_support/core_ext/module.rb +9 -0
  16. data/lib/jactive_support/core_ext/numeric.rb +7 -0
  17. data/lib/jactive_support/core_ext/ordering.rb +17 -0
  18. data/lib/jactive_support/core_ext/to_java.rb +9 -0
  19. data/lib/jactive_support/core_ext/to_java_date.rb +25 -0
  20. data/lib/jactive_support/core_ext/to_java_list.rb +11 -0
  21. data/lib/jactive_support/core_ext/to_java_map.rb +11 -0
  22. data/lib/jactive_support/java_ext.rb +4 -0
  23. data/lib/jactive_support/java_ext/date.rb +37 -0
  24. data/lib/jactive_support/java_ext/date/calculations.rb +43 -0
  25. data/lib/jactive_support/java_ext/date/conversions.rb +135 -0
  26. data/lib/jactive_support/java_ext/enum.rb +19 -0
  27. data/lib/jactive_support/java_ext/iterable.rb +74 -0
  28. data/lib/jactive_support/java_ext/iterator.rb +8 -0
  29. data/lib/jactive_support/java_ext/list.rb +65 -0
  30. data/lib/jactive_support/java_ext/list_iterator.rb +8 -0
  31. data/lib/jactive_support/java_ext/locale.rb +32 -0
  32. data/lib/jactive_support/java_ext/map.rb +6 -0
  33. data/lib/jactive_support/java_ext/map/constructor.rb +35 -0
  34. data/lib/jactive_support/java_ext/map/hash.rb +166 -0
  35. data/lib/jactive_support/java_ext/number.rb +91 -0
  36. data/lib/jactive_support/java_ext/sql_date.rb +7 -0
  37. data/lib/jactive_support/java_ext/sql_date/conversions.rb +33 -0
  38. data/lib/jactive_support/json.rb +1 -0
  39. data/lib/jactive_support/json/encoders/collection.rb +12 -0
  40. data/lib/jactive_support/json/encoders/locale.rb +6 -0
  41. data/lib/jactive_support/json/encoders/map.rb +55 -0
  42. data/lib/jactive_support/json/encoding.rb +5 -0
  43. data/lib/jactive_support/rescuable.rb +62 -0
  44. data/lib/jactive_support/version.rb +3 -0
  45. data/spec/constantize_spec.rb +57 -0
  46. data/spec/core_ext/hash_spec.rb +34 -0
  47. data/spec/core_ext/module_spec.rb +19 -0
  48. data/spec/core_ext/ordering_spec.rb +12 -0
  49. data/spec/enum_spec.rb +67 -0
  50. data/spec/java_ext/iterable/clear_spec.rb +56 -0
  51. data/spec/java_ext/iterable/delete_if_spec.rb +71 -0
  52. data/spec/java_ext/iterable/empty_spec.rb +10 -0
  53. data/spec/java_ext/iterable/reject_spec.rb +76 -0
  54. data/spec/java_ext/iterable/shared/enumeratorize.rb +12 -0
  55. data/spec/java_ext/iterable/shared/fixtures.rb +90 -0
  56. data/spec/java_ext/iterable/shift_spec.rb +160 -0
  57. data/spec/java_ext/list/clear_spec.rb +56 -0
  58. data/spec/java_ext/list/collect_spec.rb +11 -0
  59. data/spec/java_ext/list/last_spec.rb +87 -0
  60. data/spec/java_ext/list/map_spec.rb +11 -0
  61. data/spec/java_ext/list/push_spec.rb +44 -0
  62. data/spec/java_ext/list/shared/collect.rb +133 -0
  63. data/spec/java_ext/list/shared/fixtures.rb +34 -0
  64. data/spec/java_ext/map/assoc_spec.rb +43 -0
  65. data/spec/java_ext/map/clear_spec.rb +49 -0
  66. data/spec/java_ext/map/clone_spec.rb +13 -0
  67. data/spec/java_ext/map/compare_by_identity_spec.rb +111 -0
  68. data/spec/java_ext/map/constructor_spec.rb +54 -0
  69. data/spec/java_ext/map/delete_if_spec.rb +54 -0
  70. data/spec/java_ext/map/delete_spec.rb +38 -0
  71. data/spec/java_ext/map/each_key_spec.rb +29 -0
  72. data/spec/java_ext/map/each_pair_spec.rb +38 -0
  73. data/spec/java_ext/map/each_spec.rb +9 -0
  74. data/spec/java_ext/map/each_value_spec.rb +28 -0
  75. data/spec/java_ext/map/element_reference_spec.rb +119 -0
  76. data/spec/java_ext/map/element_set_spec.rb +7 -0
  77. data/spec/java_ext/map/empty_spec.rb +15 -0
  78. data/spec/java_ext/map/eql_spec.rb +19 -0
  79. data/spec/java_ext/map/equal_value_spec.rb +18 -0
  80. data/spec/java_ext/map/fetch_spec.rb +35 -0
  81. data/spec/java_ext/map/fixtures/classes.rb +36 -0
  82. data/spec/java_ext/map/flatten_spec.rb +64 -0
  83. data/spec/java_ext/map/has_key_spec.rb +8 -0
  84. data/spec/java_ext/map/has_value_spec.rb +8 -0
  85. data/spec/java_ext/map/hash_spec.rb +53 -0
  86. data/spec/java_ext/map/include_spec.rb +7 -0
  87. data/spec/java_ext/map/index_spec.rb +7 -0
  88. data/spec/java_ext/map/indexes_spec.rb +9 -0
  89. data/spec/java_ext/map/indices_spec.rb +9 -0
  90. data/spec/java_ext/map/initialize_copy_spec.rb +13 -0
  91. data/spec/java_ext/map/initialize_spec.rb +64 -0
  92. data/spec/java_ext/map/inspect_spec.rb +9 -0
  93. data/spec/java_ext/map/invert_spec.rb +27 -0
  94. data/spec/java_ext/map/keep_if_spec.rb +33 -0
  95. data/spec/java_ext/map/key_spec.rb +14 -0
  96. data/spec/java_ext/map/keys_spec.rb +39 -0
  97. data/spec/java_ext/map/length_spec.rb +7 -0
  98. data/spec/java_ext/map/member_spec.rb +7 -0
  99. data/spec/java_ext/map/merge_spec.rb +69 -0
  100. data/spec/java_ext/map/rassoc_spec.rb +39 -0
  101. data/spec/java_ext/map/reject_spec.rb +123 -0
  102. data/spec/java_ext/map/replace_spec.rb +7 -0
  103. data/spec/java_ext/map/select_spec.rb +98 -0
  104. data/spec/java_ext/map/shared/each.rb +77 -0
  105. data/spec/java_ext/map/shared/eql.rb +224 -0
  106. data/spec/java_ext/map/shared/equal.rb +94 -0
  107. data/spec/java_ext/map/shared/index.rb +29 -0
  108. data/spec/java_ext/map/shared/iteration.rb +28 -0
  109. data/spec/java_ext/map/shared/key.rb +43 -0
  110. data/spec/java_ext/map/shared/length.rb +12 -0
  111. data/spec/java_ext/map/shared/replace.rb +82 -0
  112. data/spec/java_ext/map/shared/store.rb +68 -0
  113. data/spec/java_ext/map/shared/to_s.rb +68 -0
  114. data/spec/java_ext/map/shared/update.rb +95 -0
  115. data/spec/java_ext/map/shared/value.rb +16 -0
  116. data/spec/java_ext/map/shared/values_at.rb +12 -0
  117. data/spec/java_ext/map/shift_spec.rb +44 -0
  118. data/spec/java_ext/map/size_spec.rb +7 -0
  119. data/spec/java_ext/map/sort_spec.rb +17 -0
  120. data/spec/java_ext/map/store_spec.rb +7 -0
  121. data/spec/java_ext/map/to_a_spec.rb +29 -0
  122. data/spec/java_ext/map/to_hash_spec.rb +11 -0
  123. data/spec/java_ext/map/to_s_spec.rb +20 -0
  124. data/spec/java_ext/map/try_convert_spec.rb +32 -0
  125. data/spec/java_ext/map/update_spec.rb +7 -0
  126. data/spec/java_ext/map/value_spec.rb +8 -0
  127. data/spec/java_ext/map/values_at_spec.rb +7 -0
  128. data/spec/java_ext/map/values_spec.rb +10 -0
  129. data/spec/locale_spec.rb +77 -0
  130. data/spec/shared/fixtures.rb +7 -0
  131. data/spec/shared/version.rb +123 -0
  132. data/spec/spec_helper.rb +50 -0
  133. metadata +332 -0
@@ -0,0 +1,43 @@
1
+ module JactiveSupport #:nodoc:
2
+ module JavaExtensions #:nodoc:
3
+ module Date #:nodoc:
4
+ module Calculations
5
+ CALCULATION_FIELDS = {
6
+ :years => ::Java::JavaUtil::Calendar::YEAR,
7
+ :months => ::Java::JavaUtil::Calendar::MONTH,
8
+ :weeks => ::Java::JavaUtil::Calendar::WEEK_OF_YEAR,
9
+ :days => ::Java::JavaUtil::Calendar::DAY_OF_MONTH,
10
+ :hours => ::Java::JavaUtil::Calendar::HOUR_OF_DAY,
11
+ :minutes => ::Java::JavaUtil::Calendar::MINUTE,
12
+ :seconds => ::Java::JavaUtil::Calendar::SECOND,
13
+ :millis => ::Java::JavaUtil::Calendar::MILLISECOND
14
+ }
15
+
16
+ def advance(options)
17
+ cal = ::Java::JavaUtil::Calendar.getInstance
18
+ cal.setTime(self)
19
+ options.each do |field, value|
20
+ cal.add(CALCULATION_FIELDS[field], value)
21
+ end
22
+ cal.getTime
23
+ end
24
+
25
+ def age(since=java.util.Date.new)
26
+ difference(:years, since)
27
+ end
28
+
29
+ def difference(field=:years, since=java.util.Date.new)
30
+ local_date = ::Java::OrgJodaTime::LocalDate.fromDateFields(self)
31
+ since_date = ::Java::OrgJodaTime::LocalDate.fromDateFields(since)
32
+ if field == :days
33
+ return ::Java::OrgJodaTime::Days.daysBetween(local_date, since_date).getDays
34
+ elsif field == :months
35
+ return ::Java::OrgJodaTime::Months.monthsBetween(local_date, since_date).getMonths
36
+ else
37
+ return ::Java::OrgJodaTime::Years.yearsBetween(local_date, since_date).getYears
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,135 @@
1
+ module JactiveSupport #:nodoc:
2
+ module JavaExtensions #:nodoc:
3
+ module Date #:nodoc:
4
+ # Converting dates to formatted strings, times, and datetimes.
5
+ module Conversions
6
+ DATE_FORMATS = {
7
+ :db => "yyyy-MM-dd HH:mm:ss.SSS",
8
+ :i18n => lambda { |clazz, locale|
9
+ format = I18n.translate(:"formats.timestamp", :default=>'')
10
+ !format.blank? ? clazz.pattern_formatter(format) : clazz.date_time_instance(:default, :default, locale)
11
+ },
12
+ :number => "YYYMMddHHmmssSSS",
13
+ :time => "HH:mm",
14
+ :full => lambda { |clazz, locale| clazz.date_time_instance(:full, :full, locale) },
15
+ :long => lambda { |clazz, locale| clazz.date_time_instance(:long, :long, locale) },
16
+ :medium => lambda { |clazz, locale| clazz.date_time_instance(:medium, :medium, locale) },
17
+ :short => lambda { |clazz, locale| clazz.date_time_instance(:short, :short, locale) },
18
+ :default => lambda { |clazz, locale| clazz.date_time_instance(:default, :default, locale) },
19
+ :long_ordinal => lambda { |clazz| clazz.pattern_formatter("%B #{time.day.ordinalize}, %Y %H:%M") },
20
+ :rfc822 => lambda { |clazz| clazz.pattern_formatter("%a, %d %b %Y %H:%M:%S #{time.formatted_offset(false)}") },
21
+ :httpdate => lambda { |clazz| clazz.pattern_formatter("EEE, dd MMM yyyy HH:mm:ss z", "GMT", "EN") }
22
+ }
23
+
24
+ def self.included(other) #:nodoc:
25
+ other.class_eval do
26
+ alias_method :to_default_s, :to_s
27
+ alias_method :to_s, :to_formatted_s
28
+ end
29
+ other.extend ClassMethods
30
+ end
31
+
32
+ module ClassMethods
33
+ def format(format=:i18n, locale=nil)
34
+ return format(:default, locale) unless formatter = self::DATE_FORMATS[format]
35
+ locale = locale.to_locale
36
+ formatter = formatter.respond_to?(:call) ? (formatter.arity==2 ? formatter.call(self, locale) : formatter.call(self)) : self.pattern_formatter(formatter)
37
+ formatter.to_pattern
38
+ end
39
+
40
+ def formatter(format=:i18n, locale=nil)
41
+ return formatter(:default, locale) unless formatter = self::DATE_FORMATS[format]
42
+ locale = locale.to_locale
43
+ formatter.respond_to?(:call) ? (formatter.arity==2 ? formatter.call(self, locale) : formatter.call(self)) : self.pattern_formatter(formatter)
44
+ end
45
+
46
+ def pattern_formatter(pattern, timezone=nil, locale=nil)
47
+ formatter = ::Java::JavaText::SimpleDateFormat.new(pattern, locale.to_locale)
48
+ timezone = ::Java::JavaUtil::TimeZone.getTimeZone(timezone) unless timezone.nil? || timezone.is_a?(::Java::JavaUtil::TimeZone)
49
+ formatter.setTimeZone(timezone) if timezone
50
+ formatter
51
+ end
52
+ end
53
+
54
+ # Converts to a formatted string. See DATE_FORMATS for builtin formats.
55
+ #
56
+ # This method is aliased to <tt>to_s</tt>.
57
+ #
58
+ # time = Time.now # => Thu Jan 18 06:10:17 CST 2007
59
+ #
60
+ # time.to_formatted_s(:time) # => "06:10:17"
61
+ # time.to_s(:time) # => "06:10:17"
62
+ #
63
+ # time.to_formatted_s(:db) # => "2007-01-18 06:10:17"
64
+ # time.to_formatted_s(:number) # => "20070118061017"
65
+ # time.to_formatted_s(:short) # => "18 Jan 06:10"
66
+ # time.to_formatted_s(:long) # => "January 18, 2007 06:10"
67
+ # time.to_formatted_s(:long_ordinal) # => "January 18th, 2007 06:10"
68
+ # time.to_formatted_s(:rfc822) # => "Thu, 18 Jan 2007 06:10:17 -0600"
69
+ #
70
+ # == Adding your own time formats to +to_formatted_s+
71
+ # You can add your own formats to the Time::DATE_FORMATS hash.
72
+ # Use the format name as the hash key and either a strftime string
73
+ # or Proc instance that takes a time argument as the value.
74
+ #
75
+ # # config/initializers/time_formats.rb
76
+ # Time::DATE_FORMATS[:month_and_year] = "%B %Y"
77
+ # Time::DATE_FORMATS[:short_ordinal] = lambda { |time| time.strftime("%B #{time.day.ordinalize}") }
78
+ def to_formatted_s(format=:i18n, locale=nil)
79
+ return to_default_s unless formatter = self.class::DATE_FORMATS[format]
80
+ locale = locale.to_locale
81
+ formatter = formatter.respond_to?(:call) ? (formatter.arity==2 ? formatter.call(self.class, locale) : formatter.call(self.class)) : self.class.pattern_formatter(formatter)
82
+ (formatter.respond_to?(:format) ? formatter.format(self) : formatter).to_s
83
+ end
84
+
85
+ def format(format=:i18n, locale=nil)
86
+ self.class.format(format, locale)
87
+ end
88
+
89
+ def formatter(format=:i18n, locale=nil)
90
+ self.class.formatter(format, locale)
91
+ end
92
+
93
+ def pattern_formatter(pattern, timezone=nil, locale=nil)
94
+ self.class.pattern_formatter(pattern, timezone, locale)
95
+ end
96
+
97
+ # Converts a java.util.Date object to a ruby Date, dropping hour, minute, and second precision.
98
+ #
99
+ # my_time = java.util.Date.new # => Mon Nov 12 22:59:51 -0500 2007
100
+ # my_time.to_date # => Mon, 12 Nov 2007
101
+ def to_date
102
+ to_time.to_date
103
+ end
104
+
105
+ # Converts a java.util.Date instance to a Time.
106
+ #
107
+ # ==== Examples
108
+ # date = java.util.Date.new # => Mon Nov 12 22:59:51 -0500 2007
109
+ # date.to_time # => Mon Nov 12 22:59:51 -0500 2007
110
+ def to_time
111
+ ::Time.at(self.getTime/1000)
112
+ end
113
+
114
+ # Converts a java.util.Date instance to a Ruby DateTime instance, preserving UTC offset.
115
+ #
116
+ # my_time = Time.now # => Mon Nov 12 23:04:21 -0500 2007
117
+ # my_time.to_datetime # => Mon, 12 Nov 2007 23:04:21 -0500
118
+ #
119
+ # your_time = Time.parse("1/13/2009 1:13:03 P.M.") # => Tue Jan 13 13:13:03 -0500 2009
120
+ # your_time.to_datetime # => Tue, 13 Jan 2009 13:13:03 -0500
121
+ def to_datetime
122
+ to_time.to_datetime
123
+ end
124
+
125
+ def to_java_sqldate
126
+ java.sql.Date.new(getTime)
127
+ end
128
+
129
+ def httpdate
130
+ to_formatted_s(:httpdate)
131
+ end
132
+ end
133
+ end
134
+ end
135
+ end
@@ -0,0 +1,19 @@
1
+ class java::lang::Enum
2
+ def self.find_value_of(value)
3
+ value = value.to_s
4
+ camel_name = value.camelize
5
+ if(camel_name == value)
6
+ self.valueOf(value)
7
+ else
8
+ ret = self.java_class.to_java.getEnumConstants.find {|e| e.name == value || e.name == camel_name}
9
+ #raise java::lang::IllegalArgumentException.new("No enum const class #{java_class.name}.#{value}") unless ret
10
+ self.valueOf(value) unless ret # Above does not throw the exception the same way as java code
11
+ ret
12
+ end
13
+ end
14
+
15
+ def ===(other)
16
+ other = other.to_s
17
+ self.name == other || self.name == other.camelize
18
+ end
19
+ end
@@ -0,0 +1,74 @@
1
+ require 'jactive_support/java_ext/iterator'
2
+
3
+ module java::lang::Iterable
4
+ if RUBY_VERSION >= '1.9'
5
+ FrozenError = RuntimeError
6
+ else
7
+ FrozenError = TypeError
8
+ end
9
+
10
+ def iterate
11
+ return enum_for(:iterate) unless block_given?
12
+ it = iterator
13
+ while it.next?
14
+ ob = it.next
15
+ yield(it,ob)
16
+ end
17
+ end
18
+
19
+ def delete_if
20
+ raise FrozenError, "can't modify frozen iterable" if frozen?
21
+ return enum_for(:delete_if) unless block_given?
22
+ iterate do |it,ob|
23
+ it.remove if yield(ob)
24
+ end
25
+ self
26
+ end
27
+
28
+ def reject!
29
+ raise FrozenError, "can't modify frozen iterable" if frozen?
30
+ return enum_for(:reject!) unless block_given?
31
+ ret = nil
32
+ iterate do |it,ob|
33
+ if yield(ob)
34
+ it.remove
35
+ ret = self
36
+ end
37
+ end
38
+ ret
39
+ end
40
+
41
+ def shift(*args)
42
+ raise ArgumentError, "wrong # of arguments(#{args.size} for 1)" if args.size > 1
43
+ raise FrozenError, "can't modify frozen iterable" if frozen?
44
+ it = iterator
45
+ if args.size == 1
46
+ n = args.first
47
+ raise TypeError, "Can't convert #{n} into Integer" unless n.respond_to?(:to_int)
48
+ n = n.to_int
49
+ raise TypeError, "#to_int should return Integer" unless n.kind_of?(Integer)
50
+ raise ArgumentError, "negative array size" if n < 0
51
+ ret = []
52
+ while n > 0 && it.next?
53
+ ret << it.next
54
+ it.remove
55
+ n -= 1
56
+ end
57
+ ret
58
+ elsif it.next?
59
+ ret = it.next
60
+ it.remove
61
+ ret
62
+ end
63
+ end
64
+
65
+ def empty?
66
+ !iterator.next?
67
+ end
68
+
69
+ def clear
70
+ raise FrozenError, "can't modify frozen iterable" if frozen?
71
+ iterate {|it, o| it.remove}
72
+ self
73
+ end
74
+ end
@@ -0,0 +1,8 @@
1
+ module java::util::Iterator
2
+ def has_next?
3
+ hasNext
4
+ end
5
+ def next?
6
+ hasNext
7
+ end
8
+ end
@@ -0,0 +1,65 @@
1
+ require 'jactive_support/java_ext/list_iterator'
2
+
3
+ module java::util::List
4
+ if RUBY_VERSION >= '1.9'
5
+ FrozenError = RuntimeError
6
+ else
7
+ FrozenError = TypeError
8
+ end
9
+
10
+ def inspect
11
+ content = map{|e| e.inspect}.join(', ')
12
+ "JavaList[#{content}]"
13
+ end
14
+
15
+ def iterate
16
+ return enum_for(:iterate) unless block_given?
17
+ it = listIterator
18
+ while it.next?
19
+ ob = it.next
20
+ yield(it,ob)
21
+ end
22
+ end
23
+
24
+ def map!
25
+ raise FrozenError, "can't modify frozen iterable" if frozen?
26
+ return enum_for(:map!) unless block_given?
27
+ iterate{|it, ob| it.set(yield(ob))}
28
+ self
29
+ end
30
+ alias :collect! :map!
31
+
32
+ def push(*args)
33
+ raise FrozenError, "can't modify frozen iterable" if frozen? && ( RUBY_VERSION >= '1.9' || args.size > 0 )
34
+ args.each{|a| add(a)}
35
+ self
36
+ end
37
+
38
+ def last(*args)
39
+ raise ArgumentError, "wrong # of arguments(#{args.size} for 1)" if args.size > 1
40
+ if args.size == 1
41
+ n = args.first
42
+ raise TypeError, "Can't convert #{n} into Integer" unless n.respond_to?(:to_int)
43
+ n = n.to_int
44
+ raise TypeError, "#to_int should return Integer" unless n.kind_of?(Integer)
45
+ raise ArgumentError, "negative array size" if n < 0
46
+
47
+ it = listIterator(size)
48
+ ret = []
49
+ while n > 0 && it.previous?
50
+ ret.unshift it.previous
51
+ n -= 1
52
+ end
53
+ ret
54
+ elsif size > 0
55
+ self.get(size-1)
56
+ end
57
+ end
58
+
59
+ def clear
60
+ raise FrozenError, "can't modify frozen list" if frozen?
61
+ super
62
+ self
63
+ end
64
+
65
+ end
@@ -0,0 +1,8 @@
1
+ module java::util::ListIterator
2
+ def has_previous?
3
+ hasPrevious
4
+ end
5
+ def previous?
6
+ hasPrevious
7
+ end
8
+ end
@@ -0,0 +1,32 @@
1
+ class java::util::Locale
2
+ def self.current_locale
3
+ self.getDefault
4
+ end
5
+
6
+ def self.current_locale=(l)
7
+ self.setDefault(l.to_locale)
8
+ end
9
+
10
+ def to_locale
11
+ self
12
+ end
13
+
14
+ def human_name
15
+ l = Locale.current_locale
16
+ display_country = country.blank? ? "" : " (#{getDisplayCountry(l)})"
17
+ display_variant = variant.blank? ? "" : " - #{getDisplayVariant(l)}"
18
+ "#{getDisplayLanguage(l)}#{display_country}#{display_variant}"
19
+ end
20
+
21
+ def inspect
22
+ "Locale[#{human_name}]"
23
+ end
24
+
25
+ def to_str
26
+ to_s
27
+ end
28
+
29
+ def to_sym
30
+ to_s.to_sym
31
+ end
32
+ end
@@ -0,0 +1,6 @@
1
+ require 'jactive_support/java_ext/map/hash'
2
+
3
+ module java::util::Map #:nodoc:
4
+ include JactiveSupport::JavaExtensions::Map::Hash
5
+ Hash.included_modules.find_all {|m| m.name.start_with?('ActiveSupport::CoreExtensions::') }.each {|m| include m}
6
+ end
@@ -0,0 +1,35 @@
1
+ module JactiveSupport #:nodoc:
2
+ module JavaExtensions #:nodoc:
3
+ module Map #:nodoc:
4
+ module Constructor
5
+ def self.included(other_mod)
6
+ other_mod.extend(ClassExtensions)
7
+ end
8
+
9
+ module ClassExtensions
10
+ def [](*args)
11
+ r = self.new
12
+ if args.size == 1 && ( args.first.respond_to?(:to_ary) || args.first.respond_to?(:to_hash) )
13
+ args = args.first
14
+ if args.respond_to?(:to_ary)
15
+ args = args.to_ary
16
+ elsif args.respond_to?(:to_hash)
17
+ args = args.to_hash
18
+ end
19
+ args.each do |e|
20
+ next unless e.respond_to? :to_ary
21
+ e = e.to_ary
22
+ r.put(e.shift, e.shift) if (1..2).include?(e.size)
23
+ end
24
+ else
25
+ raise ArgumentError, "odd number of arguments for constructor" unless (args.size % 2) == 0
26
+ args.each_slice(2) {|key,val| r.put(key,val)}
27
+ end
28
+
29
+ r
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,166 @@
1
+ module JactiveSupport #:nodoc:
2
+ module JavaExtensions #:nodoc:
3
+ module Map #:nodoc:
4
+ module Hash
5
+ def self.included(other_mod)
6
+ other_mod.send :alias_method, :has_key?, :contains_key?
7
+ other_mod.send :alias_method, :key?, :has_key?
8
+ other_mod.send :alias_method, :include?, :has_key?
9
+ other_mod.send :alias_method, :member?, :has_key?
10
+ other_mod.send :alias_method, :has_value?, :contains_value?
11
+ other_mod.send :alias_method, :value?, :has_value?
12
+ other_mod.send :alias_method, :update, :merge!
13
+ end
14
+
15
+ def [](key)
16
+ get(key)
17
+ end
18
+
19
+ def []=(key,val)
20
+ put(key,val)
21
+ end
22
+
23
+ def each
24
+ entry_set.each do |e|
25
+ yield [e.key, e.value]
26
+ end
27
+ self
28
+ end
29
+
30
+ def each_pair
31
+ entry_set.each do |e|
32
+ yield e.key, e.value
33
+ end
34
+ self
35
+ end
36
+
37
+ def contains_key?(key)
38
+ containsKey(key)
39
+ end
40
+
41
+ def contains_value?(value)
42
+ containsValue(value)
43
+ end
44
+
45
+ def length
46
+ size
47
+ end
48
+
49
+ def delete_if(&block)
50
+ entry_set.iterate do |it, entry|
51
+ it.remove if yield(entry.key, entry.value)
52
+ end
53
+ self
54
+ end
55
+
56
+ def each_key(&block)
57
+ key_set.each(&block)
58
+ self
59
+ end
60
+
61
+ def each_value(&block)
62
+ values.each(&block)
63
+ self
64
+ end
65
+
66
+ def delete(key)
67
+ if block_given? && !key?(key)
68
+ yield key
69
+ else
70
+ remove(key)
71
+ end
72
+ end
73
+
74
+ def fetch(key,default=nil)
75
+ return get(key) if key?(key)
76
+ raise IndexError unless default || block_given?
77
+ return default if default
78
+ yield key
79
+ end
80
+
81
+ def index(value)
82
+ ret = entry_set.find {|entry| entry.value == value}
83
+ return nil unless ret
84
+ ret.key
85
+ end
86
+
87
+ def invert
88
+ ret = nil
89
+ begin
90
+ ret = dup
91
+ ret.clear
92
+ ret
93
+ rescue
94
+ ret = ::Hash.new
95
+ end
96
+ each_pair {|key,value| ret[value] = key}
97
+ ret
98
+ end
99
+
100
+ def keys
101
+ key_set.to_a
102
+ end
103
+
104
+ def merge(other_hash,&block)
105
+ dup.merge!(other_hash,&block)
106
+ end
107
+
108
+ def merge!(other_hash)
109
+ if block_given?
110
+ other_hash.each_pair do |key, newval|
111
+ if has_key?(key)
112
+ oldval = get(key)
113
+ newval = yield(key,oldval,newval)
114
+ end
115
+ put(key, newval)
116
+ end
117
+ else
118
+ if other_hash.respond_to? :to_java_map
119
+ other_hash = other_hash.to_java_map
120
+ elsif other_hash.respond_to? :to_hash
121
+ other_hash = other_hash.to_hash
122
+ end
123
+ put_all(other_hash)
124
+ end
125
+ self
126
+ end
127
+
128
+ def to_hash
129
+ ret = ::Hash.new
130
+ each_pair {|key,value| ret[key] = value}
131
+ ret
132
+ end
133
+
134
+ def reject(&block)
135
+ dup.delete_if(&block)
136
+ end
137
+
138
+ def reject!
139
+ ret = nil
140
+ entry_set.iterate do |it, entry|
141
+ if yield(entry.key, entry.value)
142
+ it.remove
143
+ ret = self
144
+ end
145
+ end
146
+ ret
147
+ end
148
+
149
+ def replace(other_hash)
150
+ clear
151
+ merge!(other_hash)
152
+ end
153
+
154
+ def values_at(*keys)
155
+ keys.inject([]) do |memo,key|
156
+ memo.push(get(key))
157
+ end
158
+ end
159
+
160
+ def dup
161
+ clone
162
+ end
163
+ end
164
+ end
165
+ end
166
+ end