Skip to content

Instantly share code, notes, and snippets.

@h1ddengames
Last active December 21, 2020 07:18
Show Gist options
  • Save h1ddengames/7a14b73ca47477940b8357bf589e1c73 to your computer and use it in GitHub Desktop.
Save h1ddengames/7a14b73ca47477940b8357bf589e1c73 to your computer and use it in GitHub Desktop.
Java Techniques

Java Techniques

Reference sheet of my commonly used Java techniques.

Creating threads

Thread t1 = new Thread(() -> { 
                        doSomeTask(); 
                        maybeAnotherTaskHere(); 
                        });
                        
Thread t2 = new Thread(() -> doAnotherTask());
t1.start();
t2.start();
// Explicit casting to Runnable for contents of the 'of' method.
Stream.of((Runnable)
            () -> doTask1(),
            () -> doTask2())
       .parallel()
       .forEach(Runnable::run);

// Another way of casting for generic methods.
Stream.<Runnable> of(
            () -> doTask1(),
            () -> doTask2())
        .parallel()
        .forEach(Runnable::run);

Using Optional

// Note: Fields and parameters should NOT be wrapped in Optional.
// Only return values that can be null should be wrapped.

private String itemName;
public Optional<String> getItemName() { return Optional.ofNullable(itemName); }
public void setItemName(String itemName) { this.itemName = itemName; }
...

// OUTPUT: Could not find item name.
itemName = null;
getItemName().ifPresentOrElse(System.out::println, () -> System.out.println("Could not find item name."));

// OUTPUT: Greatsword
itemName = "Greatsword"
getItemName().ifPresentOrElse(System.out::println, () -> System.out.println("Could not find item name."));

(Fluent) Builder Design Pattern

// Note: No enforcement of a certain order that methods need to be called in.
// You would need to add additional logic before allowing build() to return the QuickTask object.
// For example: QuickTaskBuilder.start(); should be called before QuickTaskBuilder.build();

public class QuickTask {
    String name;
    String time;

    public static class QuickTaskBuilder {
        private QuickTask quickTask;

        public QuickTaskBuilder start() {
            quickTask = new QuickTask();
            return this;
        }

        public QuickTaskBuilder name(String name) {
            Objects.requireNonNull(name);
            quickTask.name = name;
            return this;
        }

        public QuickTaskBuilder time(String time) {
            Objects.requireNonNull(time);
            quickTask.time = time;
            return this;
        }

        public QuickTask build() {
            return quickTask;
        }
    }
}


// Note: All interfaces should be in their own .java file otherwise 
// you will be unable to make those interfaces public and method chaining will not be available.
// Usage: QuickTask quickTask = new QuickTask.QuickTaskBuilder().start().name("Wake up").time("At 8:00AM PST").build();

public interface QuickTaskStart { QuickTaskName start(); } // QuickTaskStart.java
public interface QuickTaskName { QuickTaskTime name(String name); } // QuickTaskName.java
public interface QuickTaskTime { QuickTaskEnd time(String time); } // QuickTaskTime.java
public interface QuickTaskEnd { QuickTask build(); } // QuickTaskEnd.java

public class QuickTask {
    String name;
    String time;

    public static class QuickTaskBuilder implements QuickTaskStart, QuickTaskName, QuickTaskTime, QuickTaskEnd {
        private QuickTask quickTask;

        @Override
        public QuickTaskName start() {
            quickTask = new QuickTask();
            return this;
        }

        @Override
        public QuickTaskTime name(String name) {
            Objects.requireNonNull(name);
            quickTask.name = name;
            return this;
        }

        @Override
        public QuickTaskEnd time(String time) {
            Objects.requireNonNull(time);
            quickTask.time = time;
            return this;
        }

        @Override
        public QuickTask build() {
            return quickTask;
        }
    }
}

// Note: All interfaces should be in their own .java file otherwise
// you will be unable to make those interfaces public and method chaining will not be available.
// Using the following implementation will shorten the calling function.
// Usage: QuickTask quickTask = new QuickTask.QuickTaskBuilder().name("Wake up").time("At 8:00AM PST");

public interface QuickTaskName { QuickTaskTime name(String name); }
public interface QuickTaskTime { QuickTask time(String time); }

public class QuickTask {
    String name;
    String time;

    public static class QuickTaskBuilder implements QuickTaskName, QuickTaskTime {
        private QuickTask quickTask;

        public QuickTaskBuilder() {
            quickTask = new QuickTask();
        }

        @Override
        public QuickTaskTime name(String name) {
            Objects.requireNonNull(name);
            quickTask.name = name;
            return this;
        }

        @Override
        public QuickTask time(String time) {
            Objects.requireNonNull(time);
            quickTask.time = time;
            return quickTask;
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment