Skip to content

Instantly share code, notes, and snippets.

View bdkosher's full-sized avatar

Joe Wolf bdkosher

View GitHub Profile
/*def imewi = Iterable.metaClass.eachWithIndex
def lmewi = List.metaClass.eachWithIndex
println imewi
println lmewi
def iewi = Iterable.&eachWithIndex
def lewi = List.&eachWithIndex
*/
def eachWithLast = { Closure c ->
int size = delegate.size()
@bdkosher
bdkosher / SadCompiler.java
Created April 29, 2020 00:39
Some of this is now irrelevant in our Java 14+ world
package gov.uspto.iqs;
import java.time.DayOfWeek;
import java.util.Arrays;
public class SadCompiler {
int sadInstanceOf(Object o) {
if (o instanceof List) {
return ((List) o).size();

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)

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

Poker Variations

TBD

Dammit Boy

Texas Hold'em except that one extra card dealt to each player on the deal and per flop. Players must discard a card from their hand each round so that they have only two cards before betting.

Rules

  1. Deal three cards face down to each player. Each player discards a card from their hand and there's a round of betting.
  2. Deal three community cards face up in the middle of the table and one card dealt face down to each player. Each player discards a card from their hand and there's another round of betting.
@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
}
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