text stringlengths 1 330k |
|---|
Fortunately, he and Gerry had hit it off well, unlikely as that was at the beginning. |
“When Chuck and I first met, I looked him over, and then I said, ‘I sure hope you’re squeaky clean, kid.'” She smiled over at the young-looking detective in her passenger seat. |
“‘Yeah, I am,’ he told me without a second’s hesitation. ‘Are you?'” Gerry laughed heartily and Chuck turned towards her, nodding his head and smiling more broadly now. |
She was a trim woman, well manicured and dressed in a beige wool business jacket and skirt. Her lips were parted slightly, she was breathing rapidly and the fingers of her left hand gripped the heavy metal rail at the side of the car. Spattered erratically across her left sleeve and hand were heavy dark red drops of dr... |
Website design by The Brain Mill, Inc. |
how to sell household items and things I don't need on ebay |
How To Use eBay To Sell Unwanted Items |
I have been selling on eBay since 2004. I was in college and had never heard of eBay until I was looking for some cheat codes for a video game. I soon realized that there were just regular people selling almost anything on the site…. |
Groovy is still better than Java |
January 25, 2017 |
The idea for this post started last week when a gifted teammate, whose experience lies outside JVM languages, asked me if it was still worth it to write Groovy over Java. Good question… |
For the last five years, when it comes to writing for the JVM, it’s been mostly Groovy for me. |
Crap, it’s been almost ten years. dan feels his bones creaking… |
When I first saw Venkat Subramaniam gliding through his beautiful java to groovy idioms at a conference, I was impressed. |
Little things were nice, like being able to use println 'hello' instead of System.out.println("hello");. |
Even better was reading a file line by line. It went from this: |
BufferedReader br = new BufferedReader(new FileReader("file.txt")); |
try { |
StringBuilder sb = new StringBuilder(); |
String line = br.readLine(); |
while (line != null) { |
line = br.readLine(); |
System.out.println("line = " + line); |
} finally { |
if (br != null) { |
to this: |
new File("file.txt").eachLine { println "line = $it" } |
Look Ma, a one-liner! That got me hooked. |
The biggest thing though, which I’ve grown to appreciate even more over the years, is the Collection interface. The intrinsic power it gives to collection-like objects is immense. And hey, c’mon over here Map - you’re iterable too as far as we’re concerned. |
There’s basic stuff like 10.times { println it }, and someCollection.each { println it }, which shaves a few lines of code from the existing java iteration techniques. |
Then there’s commonly useful but more powerful improvements like collect(), where you iterate through a collection of things, creating a new collection based on a function. e.g. going from this pre-Java 8 code: |
List<Integer> input = Arrays.asList(1, 2, 3); |
List results = new ArrayList(); |
for (Iterator<Integer> iterator = input.iterator(); iterator.hasNext(); ) { |
Integer integer =; |
results.add(integer * 2); |
List results = [1, 2, 3].collect { it * 2 } |
Filtering a collection is a similar improvement, where you start with a collection and filter it down based on a condition applied to each element. e.g. going from (once again, pre-Java 8): |
List<Integer> input = Arrays.asList(1, 2, 3, 4); |
List evenNumbers = new ArrayList(); |
Integer integer =; |
if (integer % 2 == 0) { |
List evenNumbers = [1, 2, 3, 4].findAll { it % 2 == 0 } |
It gets even more concise and powerful with methods like collectMany and inject. They’re slightly more complicated, but worth the couple minutes of time it takes to understand. If I were to give one piece of advice to people new to Groovy, it would be to thoroughly explore that interface. |
Lispers may smugly remark, “oh, how cute; you’re learning functional programming”. And they’re right! The pragmatic functional features of Groovy provide a gentle transition from object-oriented to more pure functional programming for many developers. |
Fast-forward a bunch of years, and things have changed. Even though Java hasn’t been progressing as fast as many would like, they’ve made some significant changes to include features introduced by Groovy, and pioneered by other languages; most notably lambdas and streaming. For example, the above Java examples can now ... |
// read each line in a file |
try (Stream<String> stream = Files.lines(Paths.get("file.txt"))) { |
} catch (IOException e) { |
// collect |
List<Integer> results = Stream.of(1, 2, 3).map(v -> v * 2).collect(Collectors.toList()); |
// filter |
List<Integer> evenNumbers = Stream.of(1, 2, 3, 4).filter(v -> v % 2 == 0).collect(Collectors.toList()); |
much improved over the old Java syntax. Still not as concise or elegant as Groovy, but fairly close. |
Back to the original question… |
Is it still worth it to write groovy? Fortunately, I had a chance to make a direct comparison that same week. |
It started with a little bake-off we’re doing to compare a couple different application design techniques. I wrote a small service in plain groovy, and my teammate wrote one in java. Our apps are running in a container that will have their environment-specific overrides specified as environment variables. I strongly pr... |
My teammate initially used the typesafe/config project to do his configuration overrides. But its environment overrides are a little verbose IMO, we didn’t need its additional complexity, and solving the same problem with different tools adds to the maintenance costs for the team. So I created a pure java version of th... |
I wasn’t excited about writing the java version, but: |
• A pure java implementation can be used by other JVM languages |
• It seemed like a useful tool for the community |
• I’d be able to have a current and relevant opinion on Groovy’s usefulness compared to Java |
An example line in the Groovy code is this: |
Map<String, String> envOverridesMap = getenv().findAll { it.key.startsWith(environmentPrefix) } |
The above line populates a Map from all system environment variables that match a given environment prefix. I wanted to avoid dependencies if possible to limit the transitive dependencies needed by consumers of the library; the only compile dependency is logback-classic. |
On the java side, I also tried building the tool without any dependencies, but it got ugly pretty fast. Replicating the above Groovy wasn’t too bad; you’ve basically seen what it looks like in the earlier collect example: |
Map<String, String> envOverridesMap = new HashMap<>(); |
for (String envKey : envVars.keySet()) { |
if (envKey.startsWith(environmentPrefix)) { |
envOverridesMap.put(envKey, envVars.get(envKey)); |
Isn’t there a more concise way? IntelliJ suggested this alternative syntax: |
envVars.keySet().stream().filter(envKey -> |
envKey.startsWith(environmentPrefix)).forEach(envKey -> |
envOverridesMap.put(envKey, envVars.get(envKey))); |
…but that just made me sad. I enjoy the new streaming capabilities, but it didn’t add value in for me in this case. It could’ve been much cleaner if Map were iterable in java, but it’s not and probably won’t ever be. |
Things got ugly when I tried cloning an arbitrary object, and also dynamically setting properties on an object. In Java clone() is a protected method, so you can’t call it unless you extend from that object. I tried to hack something together like this: |
private static <T> T cloneObject(T obj) { |
try { |
T clone = (T) obj.getClass().newInstance(); |
for (Field field : obj.getClass().getDeclaredFields()) { |
if (Modifier.isFinal(field.getModifiers())) { |
field.set(clone, field.get(obj)); |
return clone; |
} catch (Exception e) { |
throw new RuntimeException(e); |
but there were exceptional cases I hadn’t handled yet, so eventually gave up and added the commons BeanUtils library so I could use the cloneBean() method. Even uglier was going down the path of dynamic property value overrides. I had started to toy with this monstrosity: |
private static boolean setProperty(Object object, String fieldName, String fieldValueString) { |
Class<?> clazz = object.getClass(); |
try { |
Field field = clazz.getDeclaredField(fieldName); |
Class fieldType = field.getType(); |
if (fieldType.equals(Integer.class) || fieldType.equals(Integer.TYPE)) { |
field.setInt(object, Integer.parseInt(fieldValueString)); |
} else if (fieldType.equals(Long.class) || fieldType.equals(Long.TYPE)) { |
field.setLong(object, Long.parseLong(fieldValueString)); |
} else if (fieldType.equals(Boolean.class) || fieldType.equals(Boolean.TYPE)) { |
field.setBoolean(object, Boolean.parseBoolean(fieldValueString)); |
field.set(object, Long.parseLong(fieldValueString)); |
} else { |
field.set(object, field.getType().getConstructor(String.class).newInstance(fieldValueString)); |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.