Skip to content

Instantly share code, notes, and snippets.

View bdkosher's full-sized avatar

Joe Wolf bdkosher

View GitHub Profile
@bdkosher
bdkosher / blog20160415.md
Last active February 15, 2023 21:45
My experiences with moving from Swagger Annotations to a single-file specification

Liberating an API Codebase from Swagger Annotations

When creating an API with accompanying Swagger documentation, two general paths can be taken:

  1. Build First: Implement the API --> add Swagger annotations --> generate the UI and clients from the annotations
  2. Design First: Design the API spec in Swagger YAML or JSON --> generate the UI, clients, and server stubs from the spec --> implement the server stubs

On my recent project, we had embarked down the "Build First" path. After implementing the API using the mighty Spring Boot, we integrated Swagger using the slick SpringFox library, as widely demonstrated in numerous blog posts.

But all w

@bdkosher
bdkosher / FederalHolidays.java
Created March 7, 2014 16:30
Java class for determining the dates of U.S. Federal Holidays, along with Spock test.
package bdkosher.datetime;
import java.util.Calendar;
import static java.util.Calendar.*;
import java.util.Date;
import java.util.Locale;
import java.util.SortedSet;
import java.util.TimeZone;
import java.util.TreeSet;
@bdkosher
bdkosher / probabilityNowak.groovy
Created April 10, 2021 01:53
If it rains 2 out of 9 days, what is the probability the two days are consecutive.
// these 9 characters represent our days: 2 days of rain ('R') and 7 days of dryness ('D')
def days = 'RRDDDDDDD' as List
// number of trials to run; the bigger the number, the more accurate the probability
int trials = 10000
// counter for the number of consecutive rainy days we see
int consecutiveRainyDays = 0
// run the trials
@bdkosher
bdkosher / pdfMerge.groovy
Created August 26, 2020 00:23
Adobe's not getting $14 from me just so I can merge some PDF pages together
@Grab(group='org.apache.pdfbox', module='pdfbox', version='2.0.21')
import org.apache.pdfbox.multipdf.PDFMergerUtility
import org.apache.pdfbox.io.MemoryUsageSetting
def dir = new File('C:\\Users\\Joe\\Documents\\pdfs')
def merger = new PDFMergerUtility();
(1..3).each { merger.addSource(new File(dir, "document_page${it}.pdf")) }
merger.destinationFileName = new File(dir, "document.pdf").absolutePath
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
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)
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;
@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.*
println groovy.json.JsonOutput.prettyPrint(new File(args[0]).text)
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) {