Counter and Theme Toggle - Groovy Typing CST Test
Loading…
Counter and Theme Toggle — Groovy Code
Demonstrates a simple counter with theme toggling using Groovy variables and console output.
def count = 0
def isDark = false
def updateUI = {
println "Counter: $count"
println "Theme: ${isDark ? 'Dark' : 'Light'}"
}
def increment = { count++; updateUI() }
def decrement = { count--; updateUI() }
def reset = { count = 0; updateUI() }
def toggleTheme = { isDark = !isDark; updateUI() }
// Simulate actions
updateUI()
increment()
increment()
toggleTheme()
decrement()
reset()Groovy Language Guide
Groovy is a powerful, dynamic JVM language that integrates seamlessly with Java, offering concise syntax, scripting capabilities, functional programming patterns, and strong support for automation, DSL creation, and Gradle build scripts. It blends Java familiarity with Python/Ruby-like expressiveness.
Primary Use Cases
- ▸Gradle build scripts
- ▸Jenkins declarative & scripted pipelines
- ▸Automation & scripting on the JVM
- ▸Rapid backend prototyping
- ▸Testing frameworks (Spock)
- ▸Domain-specific language (DSL) creation
Notable Features
- ▸Java interoperability
- ▸Closures & functional constructs
- ▸Dynamic + optional static typing
- ▸Simplified syntax
- ▸Metaprogramming & AST transformations
Origin & Creator
Created by James Strachan in 2003; later developed under the Apache Software Foundation.
Industrial Note
Groovy is foundational to Gradle and Jenkins pipelines. It dominates CI/CD automation, DevOps pipelines, JVM scripting, and domain-specific language (DSL) development across enterprises.
Quick Explain
- ▸Groovy is a dynamic language that runs on the JVM and interoperates with Java.
- ▸It is used for scripting, automation, build tools (Gradle), and rapid development.
- ▸Supports dynamic typing, closures, metaprogramming, and DSL creation.
Core Features
- ▸Groovy scripts (.groovy)
- ▸GDK extensions
- ▸Safe navigation operator (?.)
- ▸String interpolation
- ▸Builders for DSLs
- ▸Closures & collection operators
Learning Path
- ▸Learn Groovy basics
- ▸Master closures & collections
- ▸Explore Java interop
- ▸Learn Gradle scripting
- ▸Build Jenkins pipelines & DSLs
Practical Examples
- ▸CI/CD pipelines in Jenkins
- ▸Gradle build automation
- ▸File processing scripts
- ▸Configuration-driven DSLs
- ▸Testing with Spock
Comparisons
- ▸More concise than Java
- ▸More dynamic than Kotlin
- ▸More DSL-friendly than Scala
- ▸More expressive than Python for Jenkins/Gradle
- ▸Slower runtime than statically typed JVM languages
Strengths
- ▸Concise compared to Java
- ▸Perfect for Gradle/Jenkins automation
- ▸Dynamic or static typing flexibility
- ▸Rich testing ecosystem (Spock)
- ▸Excellent for DSLs & config-driven systems
Limitations
- ▸Slower than pure Java due to dynamic typing
- ▸Performance overhead
- ▸Less mainstream than Java/Kotlin
- ▸Smaller ecosystem vs Java
- ▸Tooling sometimes less polished
When NOT to Use
- ▸High-performance applications
- ▸Large-scale microservices
- ▸Android development
- ▸Performance-critical computation
- ▸Apps requiring mainstream language adoption
Cheat Sheet
- ▸Closure: { param -> }
- ▸List: [1,2,3]
- ▸Map: [a:1, b:2]
- ▸Safe nav: obj?.prop
- ▸Elvis: x ?: default
FAQ
- ▸Is Groovy still relevant?
- ▸Yes - heavily used in Jenkins, Gradle, and scripts.
- ▸Is Groovy faster than Java?
- ▸No - dynamic nature makes it slower.
- ▸Is Groovy easy for Java developers?
- ▸Very - syntax is familiar and more concise.
- ▸Is Groovy good for CI/CD?
- ▸Yes - Groovy powers Jenkins pipelines and Gradle.
30-Day Skill Plan
- ▸Week 1: Syntax, closures, collections
- ▸Week 2: Java interop, classes, GDK
- ▸Week 3: Gradle build logic
- ▸Week 4: Jenkins & DSL development
Final Summary
- ▸Groovy is a dynamic JVM scripting and automation language.
- ▸It powers Jenkins pipelines and Gradle builds globally.
- ▸Great for fast development, DSLs, automation, and testing.
- ▸Flexible, expressive, and seamlessly integrated with Java.
Project Structure
- ▸src/main/groovy
- ▸src/test/groovy
- ▸gradle/ build scripts
- ▸Jenkinsfile
- ▸config/ DSL definitions
Monetization
- ▸DevOps automation roles
- ▸CI/CD engineering
- ▸Gradle consulting
- ▸Jenkins pipeline engineering
- ▸Automation scripting services
Productivity Tips
- ▸Use safe-navigation & Elvis operator
- ▸Leverage GDK extensions
- ▸Write DSLs for configs
- ▸Use closures effectively
- ▸Keep scripts modular
Basic Concepts
- ▸Dynamic typing
- ▸Closures
- ▸Lists, maps, ranges
- ▸GDK enhancements
- ▸Java interoperability
Official Docs
- ▸Groovy.apache.org documentation
- ▸Gradle docs
- ▸Jenkins Pipeline Groovy docs