Skip to content

Instantly share code, notes, and snippets.

View SwingGuy1024's full-sized avatar

Miguel Muñoz SwingGuy1024

View GitHub Profile
@SwingGuy1024
SwingGuy1024 / UuidGen.java
Created January 23, 2019 02:23
UUID Generator
package com.tillster.tools;
import java.util.UUID;
/**
* Add this to your ~/.bash_profile
* alias uuid="java -cp ~/Tools/out/production/Tools com.tillster.tools.UuidGen"
*/
public final class UuidGen {
private UuidGen() { }
@SwingGuy1024
SwingGuy1024 / SysProps.java
Last active January 22, 2019 22:23
Show System Properties smartly
package com.tillster.tools;
import java.io.File;
import java.util.*;
/**
* Add this to your ~/.bash_profile
* alias sysprops="java -cp ~/Tools/out/production/Tools com.tillster.tools.SysProps"
*/
public final class SysProps {
@SwingGuy1024
SwingGuy1024 / temp.md
Last active January 4, 2019 23:31
temp.md

Loyalty Campaign Manager

Loyalty Campaign Manager is an application to manage user loyalty, including points, rewards and coupons.

Requirements

  • MySQL database built for loyalty-mobilem8-components
  • Tomcat running with mobilem8-loyalty-admin and loyalty-services apps

Check out and build loyalty-mobilem8-components and database

@SwingGuy1024
SwingGuy1024 / WhyOptional.md
Last active December 15, 2018 03:31
Brian Goetz Blog Post

Taken from http://mail.openjdk.java.net/pipermail/lambda-dev/2012-September/005952.html

java.util.Optional fields

Brian Goetz brian.goetz at oracle.com

Fri Sep 21 07:51:18 PDT 2012

An alternative of throwing NoSuchElementException was replaced by returning an Optional. >

@SwingGuy1024
SwingGuy1024 / MisuseOfOptional.md
Last active July 10, 2019 03:20
Misuse of Optional

Java Optional, Java's Nullness Flaw, and the Kotlin Language

This is a work-in-progress. There's a lot here that I plan to rewrite, but the opening section, including and ending with the five examples, is very close to its final form.

Many people, including me, were excited that an Optional class was introduced in Java 8. As soon as I saw it, I was disappointed by how verbose it was, and puzzled by the strange and long-winded approach. At the time, I misunderstood its purpose, as did a lot of other people. Many people began to use it extensively. It's now very widespread, and it has led to some amazingly bad code, written by people who don't understand what it's for. In certain limited situations it's actually very useful, although I rarely actually use myself. Here's my take on Java's Optional class. (Some of this is taken from a StackOverflow question that I answered. This expands on my answer.)

Optional'

@SwingGuy1024
SwingGuy1024 / BadOptional vs NullnessChecker.md
Last active January 30, 2024 00:34
Examples of bad usages of Optional that I've seen in production code.

Bad Use of Optional: Code Examples

I was disappointed by the introduction of Optional to the Java language, because I've been using @Nullable and @NotNull annotations for years, and it's a much simpler solution. The big advantage of this approach is that it makes null pointers a compile-time error rather than a run-time bug. Now that I've worked with Optional and seen it in action in production code, I've only strengthened my earlier view. Here are some examples, all taken from production code, of bad usages of Optional, with notes on what it would look like with the NullnessChecker annotation processor instead.

Example 1: Needless Verbosity

01 private void someMethod(Widget widget, ... <other parameters>) {
02   Optional<Widget> widgetOpt = Optional.of(widget);
03   if (!widgetOpt.isPresent) {
04     throw new BusinessException("Missing Widget");

05 }

@SwingGuy1024
SwingGuy1024 / OptFun.md
Created October 6, 2018 12:17
Getting Rid of Optional

Getting Rid of Optional

In my Java Optional, Java's Nullness Flaw, and the Kotlin Language blog post, I give lots of examples of the misuse of the Optional class. Here I'm going to discuss a case where it's clearly being used correctly, but still (and surprisingly) isn't quite necessary.

A useful blog post by Brian Goetz shows how Optional was intended to get used. Briefly, he says that it's because this:

return Arrays.asList(enclosingInfo.getEnclosingClass().getDeclaredMethods())
    .stream()
    .filter(m -> Objects.equals(m.getName(), enclosingInfo.getName())

.filter(m -> Arrays.equals(m.getParameterTypes(), parameterClasses))

@SwingGuy1024
SwingGuy1024 / .bash_profile
Last active September 18, 2018 07:15
Bash Profile with path preservation
#Note: Since this file exists, the .profile file will not execute.
#Note 2: This file executes at the beginning of each terminal window
#Note 3: The .bash_login file executes only on login.
#Save original path. This lets me rerun this file without adding the same thing to the PATH over and over again.
#This is especially useful if I need to modify the path and need to experiment a bit to get it right.
FIRST_TIME=false
if [ "x$ORIG_PATH" == "x" ]
then
ORIG_PATH=$PATH
@SwingGuy1024
SwingGuy1024 / PacificTime.java
Created September 5, 2018 03:53
Clock for System Tray.
package com.mm.exp.clock;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.Arc2D;
import java.awt.geom.Line2D;
import java.awt.image.BufferedImage;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
@SwingGuy1024
SwingGuy1024 / App.java
Created September 5, 2018 03:52
Expense App
package com.mm.exp;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
/**
* Expense Reports.
* max is maximum to spend
* source is list of expenses, exceeding maximum.