<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: ouryperd</title>
    <description>The latest articles on DEV Community by ouryperd (@ou_ryperd).</description>
    <link>https://dev.to/ou_ryperd</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F159986%2Fdb178496-39d9-4520-9b92-3073b815ab46.png</url>
      <title>DEV Community: ouryperd</title>
      <link>https://dev.to/ou_ryperd</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ou_ryperd"/>
    <language>en</language>
    <item>
      <title>Remove items matching a pattern from a list in Groovy</title>
      <dc:creator>ouryperd</dc:creator>
      <pubDate>Tue, 18 Apr 2023 06:18:14 +0000</pubDate>
      <link>https://dev.to/ou_ryperd/remove-items-matching-a-pattern-from-a-list-in-groovy-13n</link>
      <guid>https://dev.to/ou_ryperd/remove-items-matching-a-pattern-from-a-list-in-groovy-13n</guid>
      <description>&lt;p&gt;Here is how you can remove items from a list that match a text pattern.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;myList = ["lice",
          "mice",
          "dice",
          "ice",
          "mosquito",
          "rice",
          "apple",
          "thrice"]

myList.removeAll {it -&amp;gt; it.contains("ice")}

assert myList == ["mosquito", "apple"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>groovy</category>
    </item>
    <item>
      <title>Processing in parallel with Groovy</title>
      <dc:creator>ouryperd</dc:creator>
      <pubDate>Mon, 03 Apr 2023 13:52:57 +0000</pubDate>
      <link>https://dev.to/ou_ryperd/processing-in-parallel-with-groovy-203c</link>
      <guid>https://dev.to/ou_ryperd/processing-in-parallel-with-groovy-203c</guid>
      <description>&lt;p&gt;I had to place thousands of messages on a queue and collect each response by an ID. This is how I used multiprocessing to do that, so that messages were processed in parallel to make it complete faster:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//lists to keep the thread instances
List threads1 = []
List threads2 = []

//first task (e.g. putting on Q)
def doThis = { i, j -&amp;gt;
    sleep(randNum(100))
    println "doThis(${i}) thread: ${j}"
}

//second task (e.g. getting from Q)
def doThat = { i, j -&amp;gt;
    sleep(randNum(20000))
    println "doThat(${i}) thread: ${j}"
}

//call first task (e.g. for eachRow from DB, put msg on Q)
20.times { i -&amp;gt;
    def t1
    t1 = Thread.start { doThis(i, t1.name) }
    threads1.add(t1)
}

//wait for all task 1 threads to end
for (thread in threads1) {
    thread.join()
    println "doThis joined: ${thread.name}"
}

//call second task (e.g. for each correlation ID, get msg from Q)
20.times { i -&amp;gt;
    def t2
    t2 = Thread.start { doThat(i, t2.name) }
    threads2.add(t2)
}

//wait for all task 2 threads to end
for (thread in threads2) {
    thread.join()
    println "doThat joined: ${thread.name}"
}

println "All threads done"

def randNum(maxSize) { //create random wait times
    return Math.abs(new Random().nextInt() % maxSize) + 1 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>groovy</category>
    </item>
    <item>
      <title>Passage of time calculations in Groovy</title>
      <dc:creator>ouryperd</dc:creator>
      <pubDate>Wed, 08 Mar 2023 09:40:28 +0000</pubDate>
      <link>https://dev.to/ou_ryperd/passage-of-time-calculations-in-groovy-5g00</link>
      <guid>https://dev.to/ou_ryperd/passage-of-time-calculations-in-groovy-5g00</guid>
      <description>&lt;p&gt;The way to calculate time duration in groovy is with TimeDuration:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import groovy.time.TimeCategory
import groovy.time.TimeDuration
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Short durations&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Date timeStart = new Date()
sleep(randNum(10000))
Date timeStop = new Date()

TimeDuration durationShort = TimeCategory.minus(timeStop, timeStart)
println "duration: ${durationShort}"
println "duration in ms    : ${durationShort.toMilliseconds()}"
println "duration in millis: ${durationShort.getMillis()}"  //drops the seconds
println "duration in seconds: ${durationShort.getSeconds()}"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;code&gt;outputs&amp;gt;&amp;gt; &lt;br&gt;
duration: 9.911 seconds&lt;br&gt;
duration in ms    : 9911&lt;br&gt;
duration in millis: 911&lt;br&gt;
duration in seconds: 9&lt;/code&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Date earlier = Date.parse('yyyy-MM-dd', "1974-08-11") //converts String to Date object
Date now = new Date()

TimeDuration durationLong = TimeCategory.minus(now, earlier)
println "duration in days: ${durationLong.getDays()}"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;code&gt;outputs&amp;gt;&amp;gt; duration in days: 17741&lt;/code&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;duration = TimeCategory.minus(now, earlier)
println duration
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;code&gt;outputs&amp;gt;&amp;gt; 17741 days, 11 hours, 34 minutes, 5.824 seconds&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The last two outputs will differ if you run it because time moves on.&lt;/p&gt;

&lt;p&gt;This is just to get a random number of seconds, used in the first example:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def randNum(maxSize) {
    return Math.abs(new Random().nextInt() % maxSize) + 1 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>groovy</category>
    </item>
    <item>
      <title>Write a method like Python's string.printable in Groovy</title>
      <dc:creator>ouryperd</dc:creator>
      <pubDate>Wed, 08 Mar 2023 09:03:14 +0000</pubDate>
      <link>https://dev.to/ou_ryperd/write-a-method-like-pythons-stringprintable-in-groovy-5a48</link>
      <guid>https://dev.to/ou_ryperd/write-a-method-like-pythons-stringprintable-in-groovy-5a48</guid>
      <description>&lt;p&gt;To find out if a string has only printable characters, Python has a function called string.printable. We can make our own &lt;code&gt;String.isPrintable()&lt;/code&gt; using metaprogramming in Groovy.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import java.nio.charset.Charset

String.metaClass.isPrintable = { return Charset.forName("US-ASCII")
                                           .newEncoder()
                                           .canEncode(delegate) 
                           }

assert 'Abc'.isPrintable() == true
assert 'àé'.isPrintable() == false
assert 'môre'.isPrintable() == false
assert 'de076'.isPrintable() == true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>groovy</category>
    </item>
    <item>
      <title>Neat time and date manipulation with Groovy</title>
      <dc:creator>ouryperd</dc:creator>
      <pubDate>Wed, 08 Mar 2023 08:52:26 +0000</pubDate>
      <link>https://dev.to/ou_ryperd/neat-time-and-date-manipulation-with-groovy-20a9</link>
      <guid>https://dev.to/ou_ryperd/neat-time-and-date-manipulation-with-groovy-20a9</guid>
      <description>&lt;p&gt;You can use &lt;code&gt;TimeCategory&lt;/code&gt; for simple offset calculations:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import groovy.time.TimeCategory

use( TimeCategory ) {
    twelveSecondsLater = new Date() + 12.seconds
    twelveSecondsAgo = new Date() - 12.seconds
    oneMinuteLater = new Date() + 1.hours
    oneMinuteAgo = new Date() - 1.hours
    halfAnHourLater = new Date() + 30.minutes
    halfAnHourAgo = new Date() - 30.minutes
    twoHoursLater = new Date() + 2.hours
    twoHoursAgo = new Date() - 2.hours
    fiveDaysLater = new Date() + 5.days
    fiveDaysAgo = new Date() - 5.days
    oneWeekLater = new Date() + 1.weeks
    oneWeekAgo = new Date() - 1.weeks
    oneMonthLater = new Date() + 1.months
    oneMonthAgo = new Date() - 1.months
    oneYearLater = new Date() + 1.years
    oneYearAgo = new Date() - 1.years
}

println twelveSecondsLater.format("yyyy-MM-dd HH.mm.ss")
println twelveSecondsAgo.format("yyyy-MM-dd HH.mm.ss")
println oneMinuteLater.format("yyyy-MM-dd HH.mm.ss")
println oneMinuteAgo.format("yyyy-MM-dd HH.mm.ss")
println halfAnHourLater.format("yyyy-MM-dd HH.mm")
println halfAnHourAgo.format("yyyy-MM-dd HH.mm")
println twoHoursLater.format("yyyy-MM-dd HH.mm")
println twoHoursAgo.format("yyyy-MM-dd HH.mm")
println fiveDaysLater.format("yyyy-MM-dd")
println fiveDaysAgo.format("yyyy-MM-dd")
println oneWeekLater.format("yyyy-MM-dd")
println oneWeekAgo.format("yyyy-MM-dd")
println oneMonthLater.format("yyyy-MM-dd")
println oneMonthAgo.format("yyyy-MM-dd")
println oneYearLater.format("yyyy-MM-dd")
println oneYearAgo.format("yyyy-MM-dd")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>groovy</category>
    </item>
    <item>
      <title>Find common items in two lists with Groovy</title>
      <dc:creator>ouryperd</dc:creator>
      <pubDate>Wed, 08 Mar 2023 08:35:04 +0000</pubDate>
      <link>https://dev.to/ou_ryperd/find-common-items-in-two-lists-3jb5</link>
      <guid>https://dev.to/ou_ryperd/find-common-items-in-two-lists-3jb5</guid>
      <description>&lt;p&gt;You can easily find items that are common in two lists using two ways.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def list1 = ["apple", "pear", "banana", "tomato", 1, 2]
def list2 = ["kale", "cabbage", 2, 3, "tomato", "carrot"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The first itearates over one list, looking for each item in the second list:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def commonItems = []
list1.each { item -&amp;gt;
    if (list2.contains(item))
        commonItems.add(item)
}
println commonItems

output&amp;gt;&amp;gt; [tomato, 2]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The more idiomatic way is to use &lt;code&gt;intersect()&lt;/code&gt;:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def common = list1.intersect list2
println common

output&amp;gt;&amp;gt; [2, tomato]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>groovy</category>
    </item>
    <item>
      <title>Use SQLite in-memory with Groovy</title>
      <dc:creator>ouryperd</dc:creator>
      <pubDate>Thu, 28 Apr 2022 14:46:21 +0000</pubDate>
      <link>https://dev.to/ou_ryperd/use-sqlite-in-memory-with-groovy-2aj9</link>
      <guid>https://dev.to/ou_ryperd/use-sqlite-in-memory-with-groovy-2aj9</guid>
      <description>&lt;p&gt;I have in the past needed to do computations with a data set that I didn't want to keep. An easy way is to create an in-memory SQLite database. This is how to do it in Groovy.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import groovy.sql.Sql
import org.sqlite.JDBC

def sql = Sql.newInstance("jdbc:sqlite::memory:", "org.sqlite.JDBC")

sql.execute("CREATE TABLE data (id INTEGER PRIMARY KEY, col2 TEXT, col3 TEXT);")

def data = sql.dataSet("data")

1.upto(5) { id -&amp;gt;
    data.add(id:id, col2:randNum(9999), col3:randStr(15))
}

data.add(id:6,col2:null,col3:'should be null')

sql.rows('select * from data').each { row -&amp;gt;
    println "${row[0]} | ${row[1]} | ${row[2]}"
}

sql.close()

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Bonus - two methods that create random data, used in the example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def randStr(length) {
    return new Random().with { (1..length)
                    .collect { (('A'..'Z')+('0'..'9')+('a'..'z'))
                .join()[ nextInt( (('A'..'Z')+('0'..'9')+('a'..'z'))
                .join().length() ) ] }
                .join() }
}

def randNum(maxSize) {
    return Math.abs(new Random().nextInt() % maxSize) + 1
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Sample output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 | 1108 | il4qmhfUh1uvAmW
2 | 2707 | L3faFu2a5ISc8nc
3 | 7014 | a7JfVfwjZRIp4bW
4 | 8574 | tTL96UCbkOOyTBl
5 | 2011 | vXjH88yNLiaTvdV
6 | null | should be null
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Neat! Remember: at the end of your script, your database is gone, which is what my use case required.&lt;/p&gt;

</description>
      <category>groovy</category>
      <category>sqlite</category>
    </item>
    <item>
      <title>A self-dismissing dialog in Groovy</title>
      <dc:creator>ouryperd</dc:creator>
      <pubDate>Sun, 17 Apr 2022 13:26:27 +0000</pubDate>
      <link>https://dev.to/ou_ryperd/a-self-dismissing-dialog-in-groovy-4pbo</link>
      <guid>https://dev.to/ou_ryperd/a-self-dismissing-dialog-in-groovy-4pbo</guid>
      <description>&lt;p&gt;During the running of your program, you may want to display a dialog. It is easy with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import javax.swing.JOptionPane
JOptionPane.showMessageDialog(null, "Message", "Title", JOptionPane.INFORMATION_MESSAGE)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, it needs manual intervention to be dismissed. If you are running an automated test and want to display some kind of progress, the following works well:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import javax.swing.JFrame
import javax.swing.JLabel
import javax.swing.JPanel
import javax.swing.SwingConstants

(1..5).each { testCase -&amp;gt;
    popup("Attention", "test case ${testCase} is currently running", 3)
    // call test case
}


def popup(String title, String message, int duration) {
    String markup = "&amp;lt;html&amp;gt;&amp;lt;font color='blue'&amp;gt;&amp;lt;b&amp;gt;Progress: &amp;lt;b&amp;gt;&amp;lt;i&amp;gt;" + message + "&amp;lt;/i&amp;gt;&amp;lt;/font&amp;gt;&amp;lt;/html&amp;gt;"
    JLabel l = new JLabel(markup)
    l.setHorizontalAlignment(SwingConstants.CENTER)
    JPanel p = new JPanel(new java.awt.GridLayout(0, 1))
    p.add(l)
    JFrame f = new JFrame(title)
    f.setContentPane(p)
    f.setSize(300, 100)
    f.setLocationRelativeTo(null)
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
    long startTime = System.currentTimeMillis()
    while (System.currentTimeMillis() &amp;lt; (startTime + (duration * 1000))) {
        f.setVisible(true)
    }
    f.setVisible(false)
    f.dispose()
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The message is in HTML and can be edited however you want with HTML tags.&lt;/p&gt;

</description>
      <category>groovy</category>
    </item>
    <item>
      <title>Add avg() to List in Groovy</title>
      <dc:creator>ouryperd</dc:creator>
      <pubDate>Mon, 11 Apr 2022 10:35:23 +0000</pubDate>
      <link>https://dev.to/ou_ryperd/add-avg-to-list-in-groovy-1jnc</link>
      <guid>https://dev.to/ou_ryperd/add-avg-to-list-in-groovy-1jnc</guid>
      <description>&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;List.metaClass.avg = {
    return delegate.size() ? delegate.sum() / delegate.size() : 0
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;assert [1, 2, 3].avg() == 2&lt;/code&gt;&lt;/p&gt;

</description>
      <category>groovy</category>
    </item>
    <item>
      <title>Add left and right trim to String in Groovy</title>
      <dc:creator>ouryperd</dc:creator>
      <pubDate>Mon, 11 Apr 2022 10:32:29 +0000</pubDate>
      <link>https://dev.to/ou_ryperd/add-left-and-right-trim-to-string-58jp</link>
      <guid>https://dev.to/ou_ryperd/add-left-and-right-trim-to-string-58jp</guid>
      <description>&lt;p&gt;Left trim:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;String.metaClass.ltrim = { return delegate.replaceAll(/^* /, '') }&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Right trim:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;String.metaClass.rtrim = { return delegate.replaceAll(/ *$/, '') }&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Usage:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;assert ' Groovy'.ltrim() == 'Groovy'&lt;/code&gt;&lt;br&gt;
&lt;code&gt;assert 'Groovy '.rtrim() == 'Groovy'&lt;/code&gt;&lt;br&gt;
&lt;code&gt;assert '      Groovy'.ltrim() == 'Groovy'&lt;/code&gt;&lt;br&gt;
&lt;code&gt;assert 'Groovy       '.rtrim() == 'Groovy'&lt;/code&gt;&lt;/p&gt;

</description>
      <category>groovy</category>
    </item>
    <item>
      <title>My own capitalizeFully() in Groovy</title>
      <dc:creator>ouryperd</dc:creator>
      <pubDate>Mon, 11 Apr 2022 10:27:59 +0000</pubDate>
      <link>https://dev.to/ou_ryperd/my-own-capitalizefully-2ah9</link>
      <guid>https://dev.to/ou_ryperd/my-own-capitalizefully-2ah9</guid>
      <description>&lt;p&gt;I used to use &lt;code&gt;org.apache.commons.lang3.text.WordUtils.capitalizeFully()&lt;/code&gt; but I have been having problems. I made my own method in Groovy.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def capitalizeFully(String str, String delim=" ") { //optional delimiter
    def li = []
    str.split(delim).each { word -&amp;gt;
        li.add(word.capitalize())
    }
    return li.join(delim)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;assert capitalizeFully("A silly string with words") == "A Silly String With Words"&lt;/code&gt;&lt;/p&gt;

</description>
      <category>groovy</category>
    </item>
    <item>
      <title>Easiest way to truncate a string in Groovy</title>
      <dc:creator>ouryperd</dc:creator>
      <pubDate>Mon, 28 Mar 2022 15:25:27 +0000</pubDate>
      <link>https://dev.to/ou_ryperd/easiest-way-to-truncaste-a-string-in-groovy-2cco</link>
      <guid>https://dev.to/ou_ryperd/easiest-way-to-truncaste-a-string-in-groovy-2cco</guid>
      <description>&lt;p&gt;The fastest way to truncate a string to a certain length:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;assert "abcdefghij".take(5) == "abcde"&lt;/code&gt;&lt;/p&gt;

</description>
      <category>groovy</category>
    </item>
  </channel>
</rss>
