Skip to content

Instantly share code, notes, and snippets.

Joe Wolf bdkosher

  • Alexandria, VA
Block or report user

Report or block bdkosher

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@bdkosher
bdkosher / SomeBean.java
Last active Oct 1, 2019
Constructor Injection that feels like Field Injection. Lombok generates a constructor for all final fields.
View SomeBean.java
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 Sep 24, 2019
Single Item List Showdown
View blog20190919.md

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 Jun 3, 2019
mybatis plugin for slowing down specific queries
View SlowQuerySimulatorPlugin.java
/**
* 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 Feb 15, 2019
Welcome to the New Era of Java
View blog20190207.md

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 Jan 3, 2019
Looks like field injection, behaves like constructor injection.
View blog20190103.md

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;
@bdkosher
bdkosher / squidS2699.java
Last active Dec 20, 2018
Simple yet kludgy way to get around "Tests should include assertions" false positives reported by Sonar, perhaps due to AssertJ.
View squidS2699.java
assertThat("Avoid squid:S2699 false positive.").isNotBlank();
View blog20181004.md

Tap that Assignment with Java

When working with mutable objects, I often instantiate some object and subsequently call mutator methods to populate its state. I find this both boring and smelly.

Consider the following (admittedly contrived) unit test:

@Test
public void primary_org_is_most_precise() {
    Set<Org> orgs = new HashSet<>();
@bdkosher
bdkosher / blog20180722.md
Last active Jul 22, 2018
Examples of when Java's compiler makes me sad
View blog20180722.md

Sad Compiler

Overall, Java's compiler is an impressive piece of software (not nearly as impressive as the JVM, though) that has no doubt saved me from countless hours hunting down runtime issues. Nevertheless, there are times when the compiler makes me sad.

Lack of Flow Typing

if (obj instanceof Foo)  {
	((Foo) obj).foo();
}
View poker.md

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.
You can’t perform that action at this time.