Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package pl.mperor.lab.java.design.pattern.creational.singleton;

public class LazyInitializedSingleton {

private static LazyInitializedSingleton instance;
private final long time = System.currentTimeMillis();

private LazyInitializedSingleton() {
}

public static synchronized LazyInitializedSingleton getInstance() {
if (instance == null) {
instance = new LazyInitializedSingleton();
}
return instance;
}

public long getTime() {
return time;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package pl.mperor.lab.java.design.pattern.creational.singleton;

public class Singleton {

private static final Singleton instance = new Singleton();
private final long time = System.currentTimeMillis();

private Singleton() {
}

public static Singleton getInstance() {
return instance;
}

public long getTime() {
return time;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package pl.mperor.lab.java.design.pattern.behavioral.iterator;

import org.junit.jupiter.api.Test;

import java.util.List;

public class IteratorTest {

private final List<String> names = List.of("Adam", "Bob", "Conor");

@Test
public void testIteratorImperatively() {
findName("Bob");
}

public boolean findName(String n) {
for (int i = 0; i < names.size(); i++) {
if (names.get(i).equals(n)) {
return true;
}
}

// same as foreach
for (String name : names) {
if (name.equals(n)) {
return true;
}
}

return false;
}

@Test
public void testIteratorFunctionally() {
// internal iterator

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package pl.mperor.lab.java.design.pattern.creational.singleton;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.concurrent.CompletableFuture;

public class LazyInitializedSingletonTest {

@Test
public void shouldOnlyAllowToLazyCreateOneInstanceOfSingleton() {
var first = CompletableFuture.supplyAsync(() -> {
System.out.println("First ...");
return LazyInitializedSingleton.getInstance();
});
var second = CompletableFuture.supplyAsync(() -> {
System.out.println("Second ...");
return LazyInitializedSingleton.getInstance();
});

LazyInitializedSingleton firstResult = first.join();
LazyInitializedSingleton secondResult = second.join();

Assertions.assertSame(firstResult, secondResult);
Assertions.assertEquals(firstResult.getTime(), secondResult.getTime());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package pl.mperor.lab.java.design.pattern.creational.singleton;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.concurrent.TimeUnit;

public class SingletonTest {

@Test
public void shouldOnlyAllowToCreateOneInstanceOfSingleton() throws InterruptedException {
var instance = Singleton.getInstance();
long time = instance.getTime();

TimeUnit.MILLISECONDS.sleep(50);
long timeAfterBreak = Singleton.getInstance().getTime();

Assertions.assertSame(instance, Singleton.getInstance());
Assertions.assertEquals(time, timeAfterBreak);
}

}
Loading