The problem
Let's say we want to check the correct mimetype and file extension of the file being uploaded. In a typical Java way I would write something like this:
boolean isRightType = "image/jpeg".equals(contentType)
|| "image/png".equals(contentType)
|| "image/x-png".equals(contentType);
boolean isRightFileExt = filename.endsWith(".png")
|| filename.endsWith(".jpg")
|| filename.endsWith(".jpeg");
Groovy solution
The Groovy way is much simpler and more elegant.
private static CONTENT_TYPES = ["image/jpeg", "image/png", "image/x-png"]
private static FILE_EXTENTIONS = [".png", ".jpg", ".jpeg"]
...
boolean isRightType = CONTENT_TYPES.any { it.equals(contentType) }
boolean isRightFileExt = FILE_EXTENTIONS.any { filename.endsWith(it) }
This was just a small example. What I really find useful are some of the following constructs that make my programming life really pleasant
Groovy Iteration
each
Simple collection iteration
def fibList = [1, 1, 2, 3, 5, 8, 13]
fibList.each { println it } // prints all of the numbers in the list
any
If you need to find out if any element of the collection meets the condition.
def fibList = [1, 1, 2, 3, 5, 8, 13]
assert fibList.any { it == 3 }
assert fibList.any { it - 2 > 10 }
This is pretty much equivalent to a Java construct
boolean any(Listlist, Condition cond)
for (E e : list) {
if (cond.meets(e)) {
return true;
}
}
return false;
}
every
If you need to find out if any element of the collection meets the condition.
def fibList = [1, 1, 2, 3, 5, 8, 13]
assert fibList.every { it > 0 }
This is pretty much equivalent to a Java construct
boolean every(Listlist, Condition cond)
for (E e : list) {
if (!cond.meets(e)) {
return false;
}
}
return true;
}
collect
If you need to create a new collection that contains each element of the original collection transformed in some way.
def fibList = [1, 1, 2, 3, 5, 8, 13]
assert fibList.collect { it - 1 } == [0, 0, 1, 2, 4, 7, 12]
This is pretty much equivalent to a Java construct
Listevery(List list, Command command)
Listresult = new ArrayList (list.size());
for (E e : list) {
result.add(command(e));
}
return result;
}
findAll
If you need to create a new collection that contains all elements that meet the condition.
def fibList = [1, 1, 2, 3, 5, 8, 13]
assert fibList.findAll { it > 1 && it < 5 } == [2, 3]
This is pretty much equivalent to a Java construct
ListfindAll(List list, Condition cond)
Listresult = new ArrayList (list.size());
for (E e : list) {
if (!cond.meets(e)) {
result.add(e);
}
}
return result;
}
find
If you need to find first element that matches the condition.
def fibList = [1, 1, 2, 3, 5, 8, 13]
assert fibList.find { it > 1 } == 2
This is pretty much equivalent to a Java construct
E findAll(Listlist, Condition cond)
for (E e : list) {
if (!cond.meets(e)) {
return e;
}
}
return null;
}
References
- Groovy Makes Iteration Easy by Ted Naleid
