Skip to content

Instantly share code, notes, and snippets.

Avatar

Joe Wolf bdkosher

  • Alexandria, VA
View GitHub Profile
View java_concrete_v_interface.txt
When concrete classes are preferrable to interfaces
String vs. CharSequence
- better relays immutability?
- so widespread that it can be an inconvenience to accept a CharSequence and have to do something with it, like pass to another 3rd party library method
Exception vs. Throwable
- error details object in REST application, don't want people to attempt to create these if there's an Error
LocalDate vs. ChronoLocalDate
View FederalField.java
import java.time.temporal.*;
import static java.time.Month.OCTOBER;
import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
import static java.time.temporal.ChronoField.YEAR;
/**
* Temporal fields that are commonly used within the Federal government.
*
* More fields may be added in the future (e.g. Leave Year, Bi-Weekly Pay Period)
View FederalHoliday.java
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;
import java.time.MonthDay;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
View SearchActiveDirectory.groovy
@Grab(group='org.springframework.security', module='spring-security-core', version='3.2.5.RELEASE')
@Grab(group='org.springframework.security', module='spring-security-ldap', version='3.2.5.RELEASE')
import groovy.transform.*
import javax.naming.NamingException
import javax.naming.directory.Attribute
import javax.naming.directory.Attributes
import org.springframework.ldap.core.*
import org.springframework.security.ldap.*
View JsonPrettyPrint.groovy
println groovy.json.JsonOutput.prettyPrint(new File(args[0]).text)
View FindNonUTF8Char.groovy
import groovy.transform.*
import java.nio.*
import java.nio.charset.*
@Field CharsetDecoder cs = Charset.forName('UTF-8').newDecoder()
InputStream.metaClass.eachChunk << { int preferredChunkSize, Closure closure ->
delegate.eachByte(preferredChunkSize) { buffer, bytesRead ->
if (bytesRead == preferredChunkSize) {
closure(buffer)
} else if (bytesRead > 0) {
View EachWithLast.groovy
/*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 Apr 29, 2020
Some of this is now irrelevant in our Java 14+ world
View SadCompiler.java
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();
View blog20200210.md

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 Mar 18, 2020
Sorting on a potentially null field
View nullFieldSort.md

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