|
| 1 | +package com.concurrent_ruby.ext; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import org.jruby.Ruby; |
| 5 | +import org.jruby.RubyClass; |
| 6 | +import org.jruby.RubyModule; |
| 7 | +import org.jruby.RubyObject; |
| 8 | +import org.jruby.anno.JRubyClass; |
| 9 | +import org.jruby.anno.JRubyMethod; |
| 10 | +import org.jruby.runtime.ObjectAllocator; |
| 11 | +import org.jruby.runtime.builtin.IRubyObject; |
| 12 | +import org.jruby.runtime.load.Library; |
| 13 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 14 | +import org.jruby.RubyBoolean; |
| 15 | +import org.jruby.RubyNil; |
| 16 | +import org.jruby.runtime.ThreadContext; |
| 17 | + |
| 18 | +public class JavaAtomicBooleanLibrary implements Library { |
| 19 | + |
| 20 | + public void load(Ruby runtime, boolean wrap) throws IOException { |
| 21 | + RubyModule concurrentMod = runtime.defineModule("Concurrent"); |
| 22 | + RubyClass atomicCls = concurrentMod.defineClassUnder("JavaAtomicBoolean", runtime.getObject(), JRUBYREFERENCE_ALLOCATOR); |
| 23 | + atomicCls.defineAnnotatedMethods(JavaAtomicBoolean.class); |
| 24 | + } |
| 25 | + |
| 26 | + private static final ObjectAllocator JRUBYREFERENCE_ALLOCATOR = new ObjectAllocator() { |
| 27 | + public IRubyObject allocate(Ruby runtime, RubyClass klazz) { |
| 28 | + return new JavaAtomicBoolean(runtime, klazz); |
| 29 | + } |
| 30 | + }; |
| 31 | + |
| 32 | + @JRubyClass(name = "JavaAtomicBoolean", parent = "Object") |
| 33 | + public static class JavaAtomicBoolean extends RubyObject { |
| 34 | + |
| 35 | + private AtomicBoolean atomicBoolean; |
| 36 | + private ThreadContext context; |
| 37 | + |
| 38 | + public JavaAtomicBoolean(Ruby runtime, RubyClass metaClass) { |
| 39 | + super(runtime, metaClass); |
| 40 | + } |
| 41 | + |
| 42 | + @JRubyMethod |
| 43 | + public IRubyObject initialize(ThreadContext context, IRubyObject value) { |
| 44 | + atomicBoolean = new AtomicBoolean(convertRubyBooleanToJavaBoolean(value)); |
| 45 | + this.context = context; |
| 46 | + return context.nil; |
| 47 | + } |
| 48 | + |
| 49 | + @JRubyMethod |
| 50 | + public IRubyObject initialize(ThreadContext context) { |
| 51 | + atomicBoolean = new AtomicBoolean(); |
| 52 | + this.context = context; |
| 53 | + return context.nil; |
| 54 | + } |
| 55 | + |
| 56 | + @JRubyMethod(name = "value") |
| 57 | + public IRubyObject value() { |
| 58 | + return RubyBoolean.newBoolean(getRuntime(), atomicBoolean.get()); |
| 59 | + } |
| 60 | + |
| 61 | + @JRubyMethod(name = "true?") |
| 62 | + public IRubyObject isAtomicTrue() { |
| 63 | + return RubyBoolean.newBoolean(getRuntime(), atomicBoolean.get()); |
| 64 | + } |
| 65 | + |
| 66 | + @JRubyMethod(name = "false?") |
| 67 | + public IRubyObject isAtomicFalse() { |
| 68 | + return RubyBoolean.newBoolean(getRuntime(), (atomicBoolean.get() == false)); |
| 69 | + } |
| 70 | + |
| 71 | + @JRubyMethod(name = "value=") |
| 72 | + public IRubyObject setAtomic(IRubyObject newValue) { |
| 73 | + atomicBoolean.set(convertRubyBooleanToJavaBoolean(newValue)); |
| 74 | + return context.nil; |
| 75 | + } |
| 76 | + |
| 77 | + @JRubyMethod(name = "make_true") |
| 78 | + public IRubyObject makeTrue() { |
| 79 | + return RubyBoolean.newBoolean(getRuntime(), atomicBoolean.compareAndSet(false, true)); |
| 80 | + } |
| 81 | + |
| 82 | + @JRubyMethod(name = "make_false") |
| 83 | + public IRubyObject makeFalse() { |
| 84 | + return RubyBoolean.newBoolean(getRuntime(), atomicBoolean.compareAndSet(true, false)); |
| 85 | + } |
| 86 | + |
| 87 | + private boolean convertRubyBooleanToJavaBoolean(IRubyObject newValue) { |
| 88 | + if (newValue instanceof RubyBoolean.False || newValue instanceof RubyNil) { |
| 89 | + return false; |
| 90 | + } else { |
| 91 | + return true; |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
0 commit comments