Groovy sample application using queue and linked list - groovy

Can you please give me a sample application using Groovy which makes use of queue and linked list?

An exemple with a queue:
def q = ["elem1", "elem2"] as Queue
q.offer("inserted string")
assert q.peek() == "elem1"
assert q.poll() == "elem1"
assert q.poll() == "elem2"
assert q.poll() == "inserted string"
Here is an exemple with a linked list:
def l = [1, 2, 3] as LinkedList
assert l[0] == 1
l.add(4)
assert l.last() == 4

Related

How to pass by value XML Groovy nodes to a variable

I'm trying to duplicate te result of this instruction:
def xmlEntrada = new File("input.txt").text
def entrada = new XmlParser().parseText(xmlEntrada)
def usuarios = entrada.cust_PS_SF_compensation.findAll{ e-> e.cust_userId.toString() == codigoActual }
def usuariosWithEndDate = entrada.cust_PS_SF_compensation.findAll{ e-> e.cust_userId.toString() == codigoActual }
but all the nodes of the variable usuariosWithEndDate are with the same reference and i need a copy by value of this operation result for edit in parallel.
usuariosWithEndDate is an arrayList with a different reference repect to usuarios but the content (the nodes) have the same reference, help please
From what I can tell, we want clone() here.
Given this XML:
<entrada>
<cust_PS_SF_compensation>
<cust_userId>5150</cust_userId>
</cust_PS_SF_compensation>
<cust_PS_SF_compensation>
<cust_userId>6160</cust_userId>
</cust_PS_SF_compensation>
<cust_PS_SF_compensation>
<cust_userId>7170</cust_userId>
</cust_PS_SF_compensation>
</entrada>
Here is the Groovy code with assert statements acting as a form of specification (if I understand the question):
def xmlEntrada = new File("input.xml").text
def entrada = new XmlParser().parseText(xmlEntrada)
def codigoActual = "5150"
def usuarios = entrada.cust_PS_SF_compensation.findAll{ e ->
e.cust_userId.text() == codigoActual
}
assert 1 == usuarios.size()
def usuariosWithEndDate = entrada.cust_PS_SF_compensation.findAll{ e ->
e.cust_userId.text() == codigoActual
}.collect { node ->
node.clone()
}
assert 1 == usuariosWithEndDate.size()
assert ! usuarios[0].is(usuariosWithEndDate[0])
assert codigoActual == usuarios[0].cust_userId.text()
assert codigoActual == usuariosWithEndDate[0].cust_userId.text()

What kind of syntax is this in Groovy?

(postUrl, config, isLoggedIn) = getSpringSecurityLoginConfig()
I couldn't find in the official documentation what kind of syntax is this?
That's multiple assignment.
There's a blog post here, and documentation here:
Groovy supports multiple assignment, i.e. where multiple variables can be assigned at once, e.g.:
def (a, b, c) = [10, 20, 'foo']
assert a == 10 && b == 20 && c == 'foo'
You can provide types as part of the declaration if you wish:
def (int i, String j) = [10, 'foo']
assert i == 10 && j == 'foo'
As well as used when declaring variables it also applies to existing variables:
def nums = [1, 3, 5]
def a, b, c
(a, b, c) = nums
assert a == 1 && b == 3 && c == 5
The syntax works for arrays as well as lists, as well as methods that return either of these:
def (_, month, year) = "18th June 2009".split()
assert "In $month of $year" == 'In June of 2009'

Why a Groovy List (of maps) arithmetic doesn't work as expected

I was trying to remove from a list of maps the elements that are present in another list. But found out that it doesn't work as i expected.
I can't understand the reason why.
def p1 = [1,2,3,4,5]
def p2 = [1, 3, 5]
assert p1[0] == p2[0]
assert p1[2] == p2[1]
assert p1[4] == p2[2]
assert [2,4] == p1-p2 // all ok
def q1 = [[1],[2],[3],[4],[5]]
def q2 = [[1], [3], [5]]
assert q1[0] == q2[0]
assert q1[2] == q2[1]
assert q1[4] == q2[2]
assert [[2], [4]] == q1-q2 // all ok
def t1 = [[1,2,3], [4,5,6], [7,8,9], [10,11,12], [13,14,15]]
def t2 = [[1,2,3], [7,8,9], [13,14,15]]
assert t1[0] == t2[0]
assert t1[2] == t2[1]
assert t1[4] == t2[2]
assert [[4,5,6], [10,11,12]] == t1-t2 // all ok
def r1 = [[a:1, b:1], [a:2,b:2], [a:3,b:3],[a:4,b:4], [a:5,b:5]]
def r2 = [[a:1, b:1], [a:3,b:3], [a:5,b:5]]
assert r1[0] == r2[0]
assert r1[2] == r2[1]
assert r1[4] == r2[2]
assert [[a:2,b:2], [a:4,b:4]] == r1-r2 // this is what I expected but fails
assert [] == r1-r2 // this is totally unexpected but it's ok
I can't figure out why. Can someone shed some light on this.
Thanks

Why does groovy map key evaluation behave differently in similar circumstances when using GStrings?

I would appreciate someone explaining to me why the following is true:
def t = "test"
assert [test: 1] == ["test": 1] // 1. expected
assert ["$t": 1] != ["test": 1] // 2. unexpected
assert ["$t": 1] != [test: 1] // 3. unexpected
assert ["$t": 1] == ["$t": 1] // 4. expected
println ["$t": 1] // output: [test: 1]
println ["test": 1] // output: [test: 1]
I don't understand why there is the inequality for results #2 and #3.
I ran into this in writing a test where the key get dynamically created in the code, but given the test conditions I know it should be the string "test". The problem is that the returned "appears" correct but is not considered equal. And I don't understand why.
Further, the following "works":
def t = "test"
def odd = ["$t": 1]
assert !odd["$t"]
assert !odd.test
assert !odd["test"]
assert !odd."$t"
println odd // output: [test: 1]
def d = new Date()
def t2 = "$t"
def odd2 = [(t2): 1, (d): 2]
assert odd2[d] == 2
assert !odd2[d.toString()]
assert !odd2[t2] // expected 1
odd2.put(t2, 3)
println odd2 // output: [test: 3, /* date#toString */: 2]
assert odd.getAt(d) == 2
assert !odd2.getAt(t2) // expected 3
Add these 2 lines
assert "$t".class.simpleName == 'GStringImpl'
assert t.class.simpleName == 'String'
or just
println "$t".class
println t.class
after the first line, you will be able to understand why. :)
If you actually want to use the value of t then you should use as:
assert [(t): 1] == ["test": 1] //use (t) to use the variable value as key
assert [(t): 1] == [test: 1]
assert [(t): 1] != ["$t": 1]
UPDATE
//String key as before, hence encouraged to use (t) instead of GStringImpl
def odd = [("$t".toString()): 1]
assert odd["$t"]
assert odd.test
assert odd["test"]
assert odd."$t"
//Equality by reference and by value in Groovy
assert "$t" == "test" //Value Equality == overridden in Groovy
assert !"$t".is("test") //Object Reference equality equivalent to == in Java

Adding curried closure as static property with expando metaclass loses default parameter value

I encountered this in both Groovy 1.8.6 and 2.0.0.
So these scenarios all work as expected:
def ay = { one, two=[:] -> [one, two] }
def be = { one, two, three=[:] -> [one,two,three] }
def ayprime = ay.curry('PRIME')
def beprime = be.curry('PRIME')
def beprimer = be.curry('PRIME', 'PRIMER')
assert ay(1,2) == [1,2]
assert ay(1) == [1,[:]]
assert be(1,2,3) == [1,2,3]
assert be(1,2) == [1,2,[:]]
assert ayprime(1) == ['PRIME', 1]
assert ayprime() == ['PRIME', [:]]
assert beprime(1,2) == ['PRIME', 1, 2]
assert beprime(1) == ['PRIME', 1, [:]]
assert beprimer(1) == ['PRIME', 'PRIMER', 1]
assert beprimer() == ['PRIME', 'PRIMER', [:]]
As does this:
class Klass {
static def smethod = { one, two=[:] -> [one, two] }
}
assert Klass.smethod(1,2) == [1, 2]
assert Klass.smethod(1) == [1, [:]]
This also works, as expected:
Klass.metaClass.static.aymethod << ay
assert Klass.aymethod(1) == [1, [:]]
The default parameter to the uncurried closure survives the assignment to Klass.
However, this fails:
Klass.metaClass.static.ayprimemethod << ayprime
assert Klass.ayprimemethod() == ['PRIME', [:]]
thusly:
assert Klass.ayprimemethod() == ['PRIME', [:]]
| |
[PRIME, null] false
and similarly, this fails:
Klass.metaClass.static.beprimermethod << beprimer
assert Klass.beprimermethod() == ['PRIME', 'PRIMER', [:]]
thusly:
assert Klass.beprimermethod() == ['PRIME', 'PRIMER', [:]]
| |
| false
[PRIME, PRIMER, null]
With the curried closures, the default parameter value works directly, but is lost when the closure is assigned as a static member of Klass.
This seems like a bug. I couldn't find this behavior documented anywhere. Am I missing something?
If the problem is still bothering you, i think this might be a workaround, until it gets fixed in groovy trunk. The python way to curry stuff:
def ayprime = { x -> x ? ay('PRIME', x) : ay('PRIME') }
def beprime = be.curry('PRIME')
def beprimer = { x -> x ? be('PRIME', 'PRIMER', x) : be('PRIME', 'PRIMER') }

Resources