jactive_support 1.0.0-universal-java-1.6

Sign up to get free protection for your applications and to get access to all the features.
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 +25 -0
  7. data/lib/jactive_support/constantize.rb +19 -0
  8. data/lib/jactive_support/core_ext/enum.rb +43 -0
  9. data/lib/jactive_support/core_ext/hash/assert_valid_params.rb +19 -0
  10. data/lib/jactive_support/core_ext/hash/with_keys_values.rb +9 -0
  11. data/lib/jactive_support/core_ext/hash.rb +2 -0
  12. data/lib/jactive_support/core_ext/locale.rb +28 -0
  13. data/lib/jactive_support/core_ext/module.rb +9 -0
  14. data/lib/jactive_support/core_ext/numeric.rb +7 -0
  15. data/lib/jactive_support/core_ext/ordering.rb +17 -0
  16. data/lib/jactive_support/core_ext/to_java.rb +9 -0
  17. data/lib/jactive_support/core_ext/to_java_date.rb +25 -0
  18. data/lib/jactive_support/core_ext/to_java_list.rb +11 -0
  19. data/lib/jactive_support/core_ext/to_java_map.rb +11 -0
  20. data/lib/jactive_support/core_ext.rb +4 -0
  21. data/lib/jactive_support/java_ext/date/calculations.rb +43 -0
  22. data/lib/jactive_support/java_ext/date/conversions.rb +135 -0
  23. data/lib/jactive_support/java_ext/date.rb +37 -0
  24. data/lib/jactive_support/java_ext/enum.rb +19 -0
  25. data/lib/jactive_support/java_ext/iterable.rb +74 -0
  26. data/lib/jactive_support/java_ext/iterator.rb +8 -0
  27. data/lib/jactive_support/java_ext/list.rb +65 -0
  28. data/lib/jactive_support/java_ext/list_iterator.rb +8 -0
  29. data/lib/jactive_support/java_ext/locale.rb +32 -0
  30. data/lib/jactive_support/java_ext/map/constructor.rb +35 -0
  31. data/lib/jactive_support/java_ext/map/hash.rb +166 -0
  32. data/lib/jactive_support/java_ext/map.rb +6 -0
  33. data/lib/jactive_support/java_ext/number.rb +91 -0
  34. data/lib/jactive_support/java_ext/sql_date/conversions.rb +33 -0
  35. data/lib/jactive_support/java_ext/sql_date.rb +7 -0
  36. data/lib/jactive_support/java_ext.rb +4 -0
  37. data/lib/jactive_support/json/encoders/collection.rb +12 -0
  38. data/lib/jactive_support/json/encoders/locale.rb +6 -0
  39. data/lib/jactive_support/json/encoders/map.rb +55 -0
  40. data/lib/jactive_support/json/encoding.rb +5 -0
  41. data/lib/jactive_support/json.rb +1 -0
  42. data/lib/jactive_support/rescuable.rb +62 -0
  43. data/lib/jactive_support/version.rb +3 -0
  44. data/lib/jactive_support.rb +5 -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
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rvmrc ADDED
@@ -0,0 +1,63 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # This is an RVM Project .rvmrc file, used to automatically load the ruby
4
+ # development environment upon cd'ing into the directory
5
+
6
+ # First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
7
+ # Only full ruby name is supported here, for short names use:
8
+ # echo "rvm use jruby" > .rvmrc
9
+ environment_id="jruby-1.6.7@jactive_support"
10
+
11
+ # Uncomment the following lines if you want to verify rvm version per project
12
+ # rvmrc_rvm_version="1.10.3" # 1.10.1 seams as a safe start
13
+ # eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
14
+ # echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
15
+ # return 1
16
+ # }
17
+
18
+ # Uncomment following line if you want options to be set only for given project.
19
+ # PROJECT_JRUBY_OPTS=( --1.9 )
20
+ # The variable PROJECT_JRUBY_OPTS requires the following to be run in shell:
21
+ # chmod +x ${rvm_path}/hooks/after_use_jruby_opts
22
+
23
+ # First we attempt to load the desired environment directly from the environment
24
+ # file. This is very fast and efficient compared to running through the entire
25
+ # CLI and selector. If you want feedback on which environment was used then
26
+ # insert the word 'use' after --create as this triggers verbose mode.
27
+ if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
28
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
29
+ then
30
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
31
+ [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]] &&
32
+ \. "${rvm_path:-$HOME/.rvm}/hooks/after_use" || true
33
+ else
34
+ # If the environment file has not yet been created, use the RVM CLI to select.
35
+ rvm --create "$environment_id" || {
36
+ echo "Failed to create RVM environment '${environment_id}'."
37
+ return 1
38
+ }
39
+ fi
40
+
41
+ # If you use an RVM gemset file to install a list of gems (*.gems), you can have
42
+ # it be automatically loaded. Uncomment the following and adjust the filename if
43
+ # necessary.
44
+ #
45
+ # filename=".gems"
46
+ # if [[ -s "$filename" ]]
47
+ # then
48
+ # rvm gemset import "$filename" | grep -v already | grep -v listed | grep -v complete | sed '/^$/d'
49
+ # fi
50
+
51
+ # If you use bundler, this might be useful to you:
52
+ # if [[ -s Gemfile ]] && {
53
+ # ! builtin command -v bundle >/dev/null ||
54
+ # builtin command -v bundle | grep $rvm_path/bin/bundle >/dev/null
55
+ # }
56
+ # then
57
+ # printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
58
+ # gem install bundler
59
+ # fi
60
+ # if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
61
+ # then
62
+ # bundle install | grep -vE '^Using|Your bundle is complete'
63
+ # fi
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in jactivesupport.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Brian Olsen <brian@maven-group.org>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,45 @@
1
+ require 'bundler/setup'
2
+ require 'rspec/core/rake_task'
3
+
4
+ def install_tasks(opts = nil)
5
+ dir = caller.find{|c| /Rakefile:/}[/^(.*?)\/Rakefile:/, 1]
6
+ h = Bundler::GemHelper.new(dir, opts && opts[:name])
7
+ h.install
8
+ h
9
+ end
10
+ helper = install_tasks
11
+ spec = helper.gemspec
12
+
13
+ require 'rake/clean'
14
+ CLEAN.include 'pkg'
15
+
16
+ task :git_local_check do
17
+ sh "git diff --no-ext-diff --ignore-submodules --quiet --exit-code" do |ok, _|
18
+ raise "working directory is unclean" if !ok
19
+ sh "git diff-index --cached --quiet --ignore-submodules HEAD --" do |ok, _|
20
+ raise "git index is unclean" if !ok
21
+ end
22
+ end
23
+ end
24
+ task :release => :git_local_check
25
+ task :build => :git_local_check
26
+
27
+ class MyRSpec < RSpec::Core::RakeTask
28
+ def task(*args,&block)
29
+ super(*args) do
30
+ old, ENV['RUBYOPT'] = ENV['RUBYOPT'], "#{ENV['RUBYOPT']} #{ruby_opts}"
31
+ yield
32
+ ENV['RUBYOPT'] = old
33
+ end
34
+ end
35
+ end
36
+
37
+ MyRSpec.new("spec:1.8") do |t|
38
+ t.ruby_opts = '--1.8'
39
+ end
40
+ MyRSpec.new("spec:1.9") do |t|
41
+ t.ruby_opts = '--1.9'
42
+ end
43
+ task :spec => ["spec:1.8", "spec:1.9"]
44
+ task :test => :spec
45
+ task :default => :spec
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "jactive_support/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "jactive_support"
7
+ s.platform = Gem::Platform::CURRENT
8
+ s.version = JActiveSupport::VERSION
9
+ s.author = "Brian Olsen"
10
+ s.email = "brian@maven-group.org"
11
+ s.homepage = "http://github.com/griff/jactive_support"
12
+ s.summary = "Extensions to add some activesupport flavor to java classes"
13
+ s.description = %q{Extensions to add some activesupport flavor to java classes}
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_dependency 'activesupport', "~> 2.3.8"
21
+ s.add_development_dependency "bundler", ">= 1.0.0"
22
+ s.add_development_dependency "rspec", "~> 2.6.0"
23
+ s.add_development_dependency "rake", ">= 0.9.2"
24
+ s.add_development_dependency "jruby-openssl", ">= 0"
25
+ end
@@ -0,0 +1,19 @@
1
+ require 'active_support/inflector'
2
+
3
+ module ActiveSupport
4
+ module Inflector
5
+ def constantize_with_jruby(camel_cased_word)
6
+ names = camel_cased_word.split('::')
7
+ names.shift if names.empty? || names.first.empty?
8
+
9
+ if names.first == "Java" && names.size == 3
10
+ last = names.pop
11
+ constantize_without_jruby(names.join('::')).__send__ last
12
+ else
13
+ constantize_without_jruby(camel_cased_word)
14
+ end
15
+ end
16
+ alias :constantize_without_jruby :constantize
17
+ alias :constantize :constantize_with_jruby
18
+ end
19
+ end
@@ -0,0 +1,43 @@
1
+ require 'jactive_support/java_ext/enum'
2
+
3
+ module JactiveSupport #:nodoc:
4
+ module CoreExtensions #:nodoc:
5
+ module Enum
6
+ def self.included(other_mod)
7
+ other_mod.send :alias_method, :flash_without_enum, :===
8
+ other_mod.send :alias_method, :===, :flash_with_enum
9
+
10
+ other_mod.send :alias_method, :to_java_without_enum, :to_java
11
+ other_mod.send :alias_method, :to_java, :to_java_with_enum
12
+ end
13
+
14
+ def flash_with_enum(other)
15
+ if other.java_kind_of? java::lang::Enum
16
+ other.name == self.to_s || other.name == self.to_s.camelize
17
+ else
18
+ flash_without_enum(other)
19
+ end
20
+ end
21
+
22
+ def to_java_with_enum(other=nil)
23
+ if other.superclass == java::lang::Enum
24
+ begin
25
+ other.find_value_of(self)
26
+ rescue java::lang::IllegalArgumentException => e
27
+ raise TypeError, e.cause.message
28
+ end
29
+ else
30
+ to_java_without_enum(other)
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ class Symbol
38
+ include ::JactiveSupport::CoreExtensions::Enum
39
+ end
40
+
41
+ class String
42
+ include ::JactiveSupport::CoreExtensions::Enum
43
+ end
@@ -0,0 +1,19 @@
1
+ module JactiveSupport
2
+ class InvalidParameter < ArgumentError
3
+ end
4
+ end
5
+
6
+ class Hash
7
+ # Validate all keys in a hash match *valid keys, raising JactiveSupport::InvalidParameter on a mismatch.
8
+ # Note that keys are NOT treated indifferently, meaning if you use strings for keys but assert symbols
9
+ # as keys, this will fail.
10
+ #
11
+ # ==== Examples
12
+ # { :name => "Rob", :years => "28" }.assert_valid_params(:name, :age) # => raises "InvalidParameter: Unknown key(s): years"
13
+ # { :name => "Rob", :age => "28" }.assert_valid_params("name", "age") # => raises "InvalidParameter: Unknown key(s): name, age"
14
+ # { :name => "Rob", :age => "28" }.assert_valid_params(:name, :age) # => passes, raises nothing
15
+ def assert_valid_params(*valid_keys)
16
+ unknown_keys = keys.map{|key| key.to_sym} - [valid_keys].flatten
17
+ raise(::JactiveSupport::InvalidParameter, "Unknown key(s): #{unknown_keys.join(", ")}") unless unknown_keys.empty?
18
+ end
19
+ end
@@ -0,0 +1,9 @@
1
+ class Hash
2
+ def Hash.with_keys_values(keys, values)
3
+ raise ArgumentError, "Unmatching sizes #{keys.size} != #{values.size}" unless keys.size == values.size
4
+ #Hash[keys.zip(values).flatten]
5
+ ret = Hash.new
6
+ keys.each_with_index{|key, idx| ret[key] = values[idx]}
7
+ ret
8
+ end
9
+ end
@@ -0,0 +1,2 @@
1
+ require 'jactive_support/core_ext/hash/assert_valid_params'
2
+ require 'jactive_support/core_ext/hash/with_keys_values'
@@ -0,0 +1,28 @@
1
+ require 'jactive_support/java_ext/locale'
2
+
3
+ Locale = java::util::Locale
4
+
5
+ class NilClass
6
+ def to_locale
7
+ java::util::Locale.current_locale
8
+ end
9
+ end
10
+
11
+ class String
12
+ # Returns the java.util.Locale that maches this string
13
+ def to_locale
14
+ if self =~ /^(.+)[-_](.+)[-_](.+)$/
15
+ java::util::Locale.new($1, $2, $3)
16
+ elsif self =~ /^(.+)[-_](.+)$/
17
+ java::util::Locale.new($1, $2)
18
+ else
19
+ java::util::Locale.new(self)
20
+ end
21
+ end
22
+ end
23
+
24
+ class Symbol
25
+ def to_locale
26
+ to_s.to_locale
27
+ end
28
+ end
@@ -0,0 +1,9 @@
1
+ class Module
2
+ def define_class(name, base)
3
+ module_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
4
+ class #{name} < ::#{base.name}
5
+ end
6
+ RUBY_EVAL
7
+ const_get(name)
8
+ end
9
+ end
@@ -0,0 +1,7 @@
1
+ require 'jactive_support/java_ext/number'
2
+
3
+ class Numeric
4
+ def to_formatted_s(format=:default, locale=nil)
5
+ self.to_java.to_formatted_s(format, locale)
6
+ end
7
+ end
@@ -0,0 +1,17 @@
1
+ class TrueClass
2
+ def <=>(o)
3
+ o.is_a?(TrueClass) ? 0 : -1
4
+ end
5
+ end
6
+
7
+ class FalseClass
8
+ def <=>(o)
9
+ o.is_a?(FalseClass) ? 0 : 1
10
+ end
11
+ end
12
+
13
+ class NilClass #:nodoc:
14
+ def <=>(other)
15
+ other.nil? ? 0 : -1;
16
+ end
17
+ end
@@ -0,0 +1,9 @@
1
+ class Object
2
+ def to_java(cls=nil)
3
+ if cls.is_a?(Symbol) && respond_to?("to_java_#{cls}")
4
+ send "to_java_#{cls}"
5
+ else
6
+ super
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,25 @@
1
+ class Date
2
+ def to_java_date
3
+ java::util::Date.new((to_time.tv_sec*1000).to_i)
4
+ end
5
+ end
6
+ class Time
7
+ def to_java_date
8
+ java::util::Date.new((tv_sec*1000).to_i)
9
+ end
10
+ end
11
+ class ActiveSupport::TimeWithZone
12
+ def to_java_date
13
+ java::util::Date.new((tv_sec*1000).to_i)
14
+ end
15
+ end
16
+ class java::util::Date
17
+ def to_java_date
18
+ self
19
+ end
20
+ end
21
+ class String
22
+ def to_java_date
23
+ to_datetime.to_java_date
24
+ end
25
+ end
@@ -0,0 +1,11 @@
1
+ class Array
2
+ def to_java_list
3
+ self
4
+ end
5
+ end
6
+
7
+ module java::util::List
8
+ def to_java_list
9
+ self
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ class Hash
2
+ def to_java_map
3
+ self
4
+ end
5
+ end
6
+
7
+ module java::util::Map #:nodoc:
8
+ def to_java_map
9
+ self
10
+ end
11
+ end
@@ -0,0 +1,4 @@
1
+ Dir[File.dirname(__FILE__) + "/core_ext/*.rb"].sort.each do |path|
2
+ filename = File.basename(path)
3
+ require "jactive_support/core_ext/#{filename}"
4
+ end
@@ -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") }
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,37 @@
1
+ require 'jactive_support/java_ext/date/calculations'
2
+ require 'jactive_support/java_ext/date/conversions'
3
+
4
+ class java::util::Date
5
+ include JactiveSupport::JavaExtensions::Date::Calculations
6
+ include JactiveSupport::JavaExtensions::Date::Conversions
7
+
8
+ FULL_STYLE = java::text::DateFormat::FULL
9
+ LONG_STYLE = java::text::DateFormat::LONG
10
+ MEDIUM_STYLE = java::text::DateFormat::MEDIUM
11
+ SHORT_STYLE = java::text::DateFormat::SHORT
12
+ DEFAULT_STYLE = java::text::DateFormat::DEFAULT
13
+
14
+ STYLE = {
15
+ :full => FULL_STYLE,
16
+ :long => LONG_STYLE,
17
+ :medium => MEDIUM_STYLE,
18
+ :short => SHORT_STYLE,
19
+ :default => DEFAULT_STYLE
20
+ }
21
+
22
+ def acts_like_time?
23
+ true
24
+ end
25
+
26
+ def self.date_time_instance(date_style=:default, time_style=:default, locale=nil)
27
+ java::text::DateFormat.getDateTimeInstance(STYLE[date_style], STYLE[time_style], locale.to_locale)
28
+ end
29
+
30
+ def self.time_instance(time_style=:default, locale=nil)
31
+ java::text::DateFormat.getTimeInstance(STYLE[time_style], locale.to_locale)
32
+ end
33
+
34
+ def self.date_instance(date_style=:default, locale=nil)
35
+ java::text::DateFormat.getDateInstance(STYLE[date_style], locale.to_locale)
36
+ end
37
+ 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