RingJavafor Ring

Ring code that Java runs

Where Java expects a function or a listener, write the name of a Ring function, or a piece of Ring code.

What you will learn#

The program#

samples/UsingJava/tutorials/ch08_callbacks.ring
# Tutorial 8: Ring code that Java runs - callbacks, comparators, streams and timers
load "java.ring"

using {
	"java.lang"
	"java.util"
	"java.util.stream"
}

Collectors = new Collectors
Collections = new Collections

# 1. a text where Java wants a function: a Ring function name ...
oWords = new ArrayList(["banana","fig","cherry","apple","kiwi"])
oWords.sort("byLength")
? oWords.toString()

# ... or a piece of Ring code that sets xJavaResult
oWords.sort("xJavaResult = strcmp(aJavaArgs[1],aJavaArgs[2])")
? oWords.toString()

# 2. forEach: the function is called once for each item; aJavaArgs[1] is the item
nTotal = 0
oWords.@forEach("nTotal += len(aJavaArgs[1])")
? "letters: " + nTotal

# 3. streams
oLong = oWords.stream().filter("isLong").collect(Collectors.@toList())
? oLong.toString()
oLengths = oWords.stream().map("xJavaResult = len(aJavaArgs[1])").collect(Collectors.@toList())
? oLengths.toString()
? oWords.stream().anyMatch("xJavaResult = aJavaArgs[1] = 'fig'")

# 4. the same function can serve an interface with several methods: cJavaMethod has the name that Java called
oThread = new Thread(jCallback("work"))
oThread.start()
jJoin(oThread)             # the Ring program serves the callbacks of other threads in jJoin, jSleep and jPump
? "thread done"

# 5. an object that Java creates from your function: a Comparator
oComparator = jCallback("byLength")
Collections.sort([ :list = oWords , :c = oComparator ])      # a plain list would be the one parameter of sort(List)
? oWords.toString()

# 6. a Java function that returns a value to Java: Supplier
oOptional = (new Optional).empty()
? oOptional.orElseGet(jCallback("makeDefault"))

func byLength aArgs
	return len(aArgs[1]) - len(aArgs[2])

func isLong aArgs
	return len(aArgs[1]) > 4

func work aArgs
	? "working in a Java thread, the method is " + cJavaMethod

func makeDefault aArgs
	return "default from Ring"
Output
[fig, kiwi, apple, banana, cherry]
[apple, banana, cherry, fig, kiwi]
letters: 24
[apple, banana, cherry]
[5, 6, 6, 3, 4]
1
working in a Java thread, the method is run
thread done
[fig, kiwi, apple, banana, cherry]
default from Ring

How it works#

Where Java wants a functional interface (a Runnable, a Comparator, a Function...), write the name of a Ring function as a text: Java calls it. The arguments are in aJavaArgs (or the first parameter), the value for Java is returned, and cJavaMethod tells which method was called when one function serves an interface with several. A text with Ring code is run, and it sets xJavaResult.

Exercises#

  1. Sort a list of words by length with a Ring function.
  2. Use forEach to print the squares of a list.
  3. Make a Supplier that returns a counter.

Next#

Threads

RingJava - use Java classes and methods from Ring programs.