Skip to content

Instantly share code, notes, and snippets.

View bdkosher's full-sized avatar

Joe Wolf bdkosher

View GitHub Profile
@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) {
/*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();