Skip to content

Instantly share code, notes, and snippets.

View bdkosher's full-sized avatar

Joe Wolf bdkosher

View GitHub Profile

Say I've got a controller class, that effectively delegates to a service, retrieves some domain objects, and converts them to another data type that better serializes to JSON and contains some additional metadata.

@Data 
public class RawFoo {
  private long id;
  private String name;
}

@Data
@bdkosher
bdkosher / nullFieldSort.md
Created March 18, 2020 14:00
Sorting on a potentially null field

Say I have a class with a potentially-null field and I want to sort a List of class instances on that field first (nulls last), then fall back on a second non-null field.

@Data
class Foo {
    String bar;
    int baz
}

Calling Collect with Groovy

To be clear, this post is not about using Groovy to make collect calls (Are collect phone calls still a thing? Do people nowadays even know what they are?). No, I am talking about calling methods named collect, which may seem banal but actually warrants attention, especially in light of Groovy 3's long-awaited and much-anticipated release.

Groovy has long provided Closures and extension methods on Iterable and Iterator to implement Java 8 Stream-like capabilities, even when running on Java 7 or earlier.

Using these capabilities to, say, square all even numbers between 1 and 10, would look akin to this:

def evenSquares = (1..10)
class Man {
String name
Man father
int birthYear
int deathYear
int begatSonAtYear
static named(String name) {
new Man(name: name)
}
  • Use Spring Boot
  • Use Lombok
  • Apply the @RequiredArgsConstructor pattern to use constructor injection that feels like field injection
  • Use Java 8 Date/Time API -- do not use java.util.Date or Joda
  • Use Spring Web -- do not attempt to use JAX-RS
  • Do not apply inheritance with your Spring controllers
  • Do not package by layer (and definitely do not put different layers into separate maven sub-modules)
  • Minimize use of for loops. Should be able to get by with Stream API for 95% of use cases
  • Majority of classes should be 20 SLOC or less
  • Inheritance is an ultimately expensive mechanism for code reuse. Composition pattern is better.
@bdkosher
bdkosher / SomeBean.java
Last active October 1, 2019 16:30
Constructor Injection that feels like Field Injection. Lombok generates a constructor for all final fields.
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
@Component
@RequiredArgsConstructor
public class SomeBean {
private final SomeDependency someDependency;
private final OtherDependency otherDependency;
@bdkosher
bdkosher / blog20190919.md
Last active September 24, 2019 14:36
Single Item List Showdown

Single Item List Showdown: Collections::singletonList vs. List::of

How do you take a single Java object of type T and turn it into a single-element List<T>?

One way, of course, is to instantiate some List implementation like ArrayList or LinkedList and add the item, but where's the fun in that? Saavy developers like us want to do such banal things in a single line of code. The good news is that JavaSE provides multiple single-line-of-code approaches to address this problem.

(I'm going to ignore the so-called "double brace" instantiation approach because even though you can create the single-item list and assign a reference in one statement, it uses two lines of code: one line to instantiate the anonymous List subtype and one line inside the initializer block to add the item.)

Java 8 and Earlier Approaches

@bdkosher
bdkosher / SlowQuerySimulatorPlugin.java
Created June 3, 2019 19:04
mybatis plugin for slowing down specific queries
/**
* For dev purposes only. Intercepts queries and inserts delay to help simulate slow queries.
*/
@Intercepts(
@Signature(
type = StatementHandler.class,
method = "query",
args = {Statement.class, ResultHandler.class}
)
)
@bdkosher
bdkosher / blog20190207.md
Last active February 15, 2019 15:27
Welcome to the New Era of Java

Welcome to the New Era of Java

As a resident Java Subject Matter Expert within my organization, I've been paying close attention to the happenings surrounding Java over the past two years: the modular JDK, the six month release cadence, the growing significance of OpenJDK, and Oracle JDK 11's new license.

I hope to help my organization, which is highly invested in Java, appropriately navigate these changes by not falling woefully behind in the versions of Java used, by ensuring our JVMs are secure and can be patched, and by not unintentionally violating any software licenses. Achieving this aim starts with educating other stakeholders, especially upper management. I've found it helpful to explain these recent happenings to a less technical audience by framing Java's changes in a historical context--namely, in terms of "eras."

The Era of Stability

The Java 5.0 release in September of 2004 ushered in an important era in Java's history. It brought significant changes to the language and its Standard Ed

@bdkosher
bdkosher / blog20190103.md
Created January 3, 2019 14:42
Looks like field injection, behaves like constructor injection.

Spring (and likely other IoC frameworks) offer three dependency injection approaches:

  1. Constructor-based - Spring injects dependencies as constructor arguments when instantiating a bean Developers using Lombok with Spring can create annotation-configured beans easily with the Lombok @RequiredArgsConstructor annotation.
  2. Setter-based - Spring creates a bean through its default calls setter methods to
@Component
@RequiredArgsConstructor
public class SomeComponent {

 private final SomeDependency dependency;