Skip to content

Instantly share code, notes, and snippets.

@andrebreves
andrebreves / BrazilianHoliday.java
Last active January 2, 2024 13:02
Date calculations considering Brazilian Holidays
import java.time.LocalDate;
import static java.time.Month.APRIL;
import static java.time.Month.DECEMBER;
import static java.time.Month.JANUARY;
import static java.time.Month.MAY;
import static java.time.Month.NOVEMBER;
import static java.time.Month.OCTOBER;
import static java.time.Month.SEPTEMBER;
import java.time.MonthDay;
import java.time.Year;
@andrebreves
andrebreves / LazyOptional.java
Last active January 11, 2022 01:43
A type following the Optional API that computes its value once and only after a "terminal operation" (like Stream).
import java.util.Objects;
import java.util.Optional;
import java.util.Spliterator;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
@andrebreves
andrebreves / vpn.sh
Last active August 7, 2020 20:02
Bash script to deactivate and activate the Endpoint Security VPN on macOS
#!/usr/bin/env bash
get_plist_key() {
local value
if value="$(set -o pipefail; plutil -extract "${2}" xml1 -o - "${1}" | grep -Ev "^<(\?|\!|/?plist)")"; then
printf "%s" "${value}"
fi
}
plist_status() {
@andrebreves
andrebreves / open-keg
Last active March 26, 2022 15:20
Make homebrew keg-only formulae available inside a interactive subshell, by setting some PATH variables
#!/usr/bin/env bash
set -Eeuo pipefail
# Formatting
if [[ -t 1 && $(tput colors) -ge 8 ]]; then
_rst_="$(tput sgr0)" ; _bol_="$(tput bold)" ; _und_="$(tput smul)"
_red_="$(tput setaf 1)"; _grn_="$(tput setaf 2)"; _ylw_="$(tput setaf 3)"; _blu_="$(tput setaf 4)"
else
_rst_=""; _bol_=""; _und_=""
_red_=""; _grn_=""; _ylw_=""; _blu_=""
@andrebreves
andrebreves / PercentageDistribution.java
Last active November 22, 2023 16:43
Calculates correctly distributed percentages (that sums to 100%) of arrays, collections and maps of numbers.
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import static java.util.stream.Collectors.toMap;
import static java.util.stream.Collectors.toUnmodifiableList;
@andrebreves
andrebreves / Lazy.java
Last active March 3, 2020 02:59
Thread safe lazy computation of final values using lambda.
import java.util.Objects;
import java.util.function.Supplier;
public class Lazy<T> {
private volatile T value;
private final Supplier<T> supplier;
private volatile boolean valueComputed = false;
private Lazy(Supplier<T> supplier) { this.supplier = Objects.requireNonNull(supplier); }