Skip to content

Commit

Permalink
prefer Atomic over volatile
Browse files Browse the repository at this point in the history
  • Loading branch information
Kapral67 committed Feb 11, 2024
1 parent f5b7e18 commit ec11292
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@

public
interface MemberEventHelper extends EventHelper {
String ROOT_ID = DdbUtils.ROOT_MEMBER_ID;
@NotNull String ROOT_ID = DdbUtils.ROOT_MEMBER_ID;

@NotNull
static
Expand Down Expand Up @@ -239,8 +239,12 @@ MemberRecord buildMemberRecord (final @NotNull UUID memberId, final @NotNull UUI
final LocalDate finalBirthday = birthday;
switch (param) {
case FIRST_NAME -> {
final String desc = ("Must match pattern: ^A-Za-z\\-_'$%nMust NOT match pattern: ^['_-]+[A-Za-z\\-'_]*$%n_ & - chars result in the immediate succeeding char being " +
"capitalized%n_ chars are removed, useful for names like McDonald (input: mc_donald)%n%n[Required] Please Enter %s:").formatted(param.jsonFieldName());
final String desc = """
Must match pattern: ^[A-Za-z]+[A-Za-z'_-]*$
_ & - chars result in the immediate succeeding char being capitalized
_ chars are removed, useful for names like McDonald (input: mc_donald)
[Required] Please Enter %s:""".formatted(param.jsonFieldName());
final TextInputDialogResultValidator validator = (content) -> {
try {
Member.builder()
Expand All @@ -254,8 +258,12 @@ MemberRecord buildMemberRecord (final @NotNull UUID memberId, final @NotNull UUI
memberBuilder.firstName(requireNonNull(dialog.showDialog(gui)));
}
case MIDDLE_NAME -> {
final String desc = ("Must match pattern: ^A-Za-z\\-_'$%nMust NOT match pattern: ^['_-]+[A-Za-z\\-'_]*$%n_ & - chars result in the immediate succeeding char being " +
"capitalized%n_ chars are removed, useful for names like McDonald (input: mc_donald)%n%n[Optional] Please Enter %s:").formatted(param.jsonFieldName());
final String desc = """
Must match pattern: ^[A-Za-z]+[A-Za-z'_-]*$
_ & - chars result in the immediate succeeding char being capitalized
_ chars are removed, useful for names like McDonald (input: mc_donald)
[Optional] Please Enter %s:""".formatted(param.jsonFieldName());
final TextInputDialogResultValidator validator = (content) -> {
try {
Member.builder()
Expand All @@ -269,9 +277,12 @@ MemberRecord buildMemberRecord (final @NotNull UUID memberId, final @NotNull UUI
memberBuilder.middleName(dialog.showDialog(gui));
}
case LAST_NAME -> {
final String desc = (
"Must match pattern: ^A-Za-z\\-_'$%nMust NOT match pattern: ^['_-]+[A-Za-z\\-'_]*$%n_ & - chars result in the immediate succeeding char being capitalized%n_ " +
"chars are removed, useful for names like McDonald (input: mc_donald)%n%n[Required] Please Enter %s:").formatted(param.jsonFieldName());
final String desc = """
Must match pattern: ^[A-Za-z]+[A-Za-z'_-]*$
_ & - chars result in the immediate succeeding char being capitalized
_ chars are removed, useful for names like McDonald (input: mc_donald)
[Required] Please Enter %s:""".formatted(param.jsonFieldName());
final TextInputDialogResultValidator validator = (content) -> {
try {
Member.builder()
Expand All @@ -289,7 +300,11 @@ MemberRecord buildMemberRecord (final @NotNull UUID memberId, final @NotNull UUI
memberBuilder.suffix(dialog.showDialog(gui));
}
case BIRTHDAY -> {
final String desc = ("Must be formatted like yyyy-MM-dd (e.g. 1970-12-31 -> Dec. 31, 1970)%n%n[Required] Please Enter %s:").formatted(param.jsonFieldName());
final String desc = """
Must be formatted like yyyy-MM-dd
e.g. 1970-01-01 -> Jan 1st 1970
[Required] Please Enter %s:""".formatted(param.jsonFieldName());
final TextInputDialogResultValidator validator = (content) -> {
try {
Member.builder()
Expand All @@ -304,7 +319,11 @@ MemberRecord buildMemberRecord (final @NotNull UUID memberId, final @NotNull UUI
memberBuilder.birthday(birthday);
}
case DEATHDAY -> {
final String desc = ("Must be formatted like yyyy-MM-dd (e.g. 1970-12-31 -> Dec. 31, 1970)%n%n[Optional] Please Enter %s:").formatted(param.jsonFieldName());
final String desc = """
Must be formatted like yyyy-MM-dd
e.g. 1970-01-01 -> Jan 1st 1970
[Optional] Please Enter %s:""".formatted(param.jsonFieldName());
final TextInputDialogResultValidator validator = (content) -> {
if (isNull(content)) {
return null;
Expand Down Expand Up @@ -349,7 +368,9 @@ MemberRecord buildMemberRecord (final @NotNull UUID memberId, final @NotNull UUI
if (msgDialog.showDialog(gui)
.equals(MessageDialogButton.Yes))
{
final String descPrefix = "For US numbers: '+' & Country Code are Optional%nFor Int'l Numbers: '+' & Country Code are Required".formatted();
final String descPrefix = """
For US numbers: '+' & Country Code are Optional
For Int'l Numbers: '+' & Country Code are Required""";
final TextInputDialogResultValidator validator = (content) -> {
try {
DdbUtils.normalizePhoneNumber(content);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
public final
class ToolkitCleanerEvent implements EventHelper {
private static final int HASH_LENGTH = 64;
private static final Pattern CFN_TEMPLATE_HASHES_PATTERN = Pattern.compile("[a-f0-9]{%d}".formatted(HASH_LENGTH));
private static final String ASSET_BUCKET_PREFIX = "cdk";
private static final @NotNull Pattern CFN_TEMPLATE_HASHES_PATTERN = Pattern.compile("[a-f0-9]{%d}".formatted(HASH_LENGTH));
private static final @NotNull String ASSET_BUCKET_PREFIX = "cdk";
private final long[] deletedItems = {0L};
private final long[] reclaimedBytes = {0L};
private final @NotNull S3Client s3Client;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;
import org.jetbrains.annotations.NotNull;
import software.amazon.awssdk.annotations.ThreadSafe;
import software.amazon.awssdk.core.SdkClient;
Expand All @@ -12,7 +13,8 @@
@ThreadSafe
public final
class SdkClientProvider implements SdkAutoCloseable {
private static volatile SdkClientProvider INSTANCE = null;
@NotNull
private static final AtomicReference<SdkClientProvider> INSTANCE = new AtomicReference<>(null);
@NotNull
private final ConcurrentHashMap<Class<? extends SdkClient>, SdkClient> clientMap;

Expand All @@ -24,10 +26,10 @@ class SdkClientProvider implements SdkAutoCloseable {

public static synchronized
SdkClientProvider getSdkClientProvider () {
if (isNull(INSTANCE)) {
INSTANCE = new SdkClientProvider();
if (isNull(INSTANCE.get())) {
INSTANCE.set(new SdkClientProvider());
}
return INSTANCE;
return INSTANCE.get();
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.googlecode.lanterna.gui2.WindowBasedTextGUI;
import com.googlecode.lanterna.gui2.dialogs.DialogWindow;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import org.familydirectory.sdk.adminclient.AdminClientTui;
import org.familydirectory.sdk.adminclient.utility.CanceledException;
import org.jetbrains.annotations.NotNull;
Expand All @@ -46,15 +47,16 @@
*/
public final
class RefreshableListSelectDialog<T> extends DialogWindow {
private volatile boolean isCanceled;
@NotNull
private final AtomicBoolean isCanceled;
@Nullable
private T result;

public
RefreshableListSelectDialog (final @NotNull String title, final @Nullable String description, final boolean allowCancel, final @NotNull List<T> content, final @Nullable TerminalSize listBoxSize) {
super(requireNonNull(title));
this.result = null;
this.isCanceled = false;
this.isCanceled = new AtomicBoolean(false);
this.setHints(AdminClientTui.EXTRA_WINDOW_HINTS);
if (content.isEmpty()) {
throw new IllegalArgumentException("content may not be empty");
Expand Down Expand Up @@ -93,7 +95,7 @@ void onSelect (final T item) {

private
void cancel () {
this.isCanceled = true;
this.isCanceled.set(true);
this.close();
}

Expand All @@ -103,7 +105,7 @@ void cancel () {
T showDialog (final WindowBasedTextGUI textGUI) {
this.result = null;
super.showDialog(textGUI);
if (this.isCanceled) {
if (this.isCanceled.get()) {
throw new CanceledException();
}
return this.result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.googlecode.lanterna.gui2.WindowBasedTextGUI;
import com.googlecode.lanterna.gui2.dialogs.DialogWindow;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import org.familydirectory.sdk.adminclient.AdminClientTui;
import org.familydirectory.sdk.adminclient.utility.CanceledException;
import org.jetbrains.annotations.NotNull;
Expand All @@ -46,7 +47,8 @@
*/
public final
class SkippableListSelectDialog<T> extends DialogWindow {
private volatile boolean isCanceled;
@NotNull
private final AtomicBoolean isCanceled;
@Nullable
private T result;

Expand All @@ -55,7 +57,7 @@ class SkippableListSelectDialog<T> extends DialogWindow {
final @Nullable TerminalSize listBoxSize)
{
super(requireNonNull(title));
this.isCanceled = false;
this.isCanceled = new AtomicBoolean(false);
this.result = null;
this.setHints(AdminClientTui.EXTRA_WINDOW_HINTS);
if (content.isEmpty()) {
Expand Down Expand Up @@ -101,7 +103,7 @@ void onSelect (final T item) {

private
void cancel () {
this.isCanceled = true;
this.isCanceled.set(true);
this.close();
}

Expand All @@ -111,7 +113,7 @@ void cancel () {
T showDialog (final WindowBasedTextGUI textGUI) {
this.result = null;
super.showDialog(textGUI);
if (this.isCanceled) {
if (this.isCanceled.get()) {
throw new CanceledException();
}
return this.result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.googlecode.lanterna.gui2.dialogs.MessageDialog;
import com.googlecode.lanterna.gui2.dialogs.MessageDialogButton;
import com.googlecode.lanterna.gui2.dialogs.TextInputDialogResultValidator;
import java.util.concurrent.atomic.AtomicBoolean;
import org.familydirectory.sdk.adminclient.AdminClientTui;
import org.familydirectory.sdk.adminclient.utility.CanceledException;
import org.jetbrains.annotations.NotNull;
Expand All @@ -53,7 +54,8 @@ class SkippableTextInputDialog extends DialogWindow {
private final TextBox textBox;
@Nullable
private final TextInputDialogResultValidator validator;
private volatile boolean isCanceled;
@NotNull
private final AtomicBoolean isCanceled;
@Nullable
private String result;

Expand All @@ -65,7 +67,7 @@ class SkippableTextInputDialog extends DialogWindow {
this.setHints(AdminClientTui.EXTRA_WINDOW_HINTS);
this.textBox = new TextBox();
this.validator = validator;
this.isCanceled = false;
this.isCanceled = new AtomicBoolean(false);
this.result = null;

final Panel buttonPanel = new Panel();
Expand Down Expand Up @@ -110,7 +112,7 @@ void onOK () {

private
void cancel () {
this.isCanceled = true;
this.isCanceled.set(true);
this.close();
}

Expand All @@ -120,7 +122,7 @@ void cancel () {
String showDialog (final WindowBasedTextGUI textGUI) {
this.result = null;
super.showDialog(textGUI);
if (this.isCanceled) {
if (this.isCanceled.get()) {
throw new CanceledException();
}
return this.result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
import org.familydirectory.assets.ddb.models.member.MemberRecord;
import org.familydirectory.sdk.adminclient.AdminClientTui;
import org.jetbrains.annotations.Contract;
Expand All @@ -15,6 +16,7 @@

public abstract
class PickerModel extends Thread implements AutoCloseable {
@NotNull
protected static final Comparator<MemberRecord> FIRST_NAME_COMPARATOR = Comparator.comparing(memberRecord -> memberRecord.member()
.getFirstName());

Expand All @@ -24,17 +26,19 @@ class PickerModel extends Thread implements AutoCloseable {
private final BlockingQueue<Boolean> processingQueue;
@NotNull
private final CountDownLatch closedLatch;
private volatile boolean isClosed;
@NotNull
private final AtomicBoolean isClosed;

protected
PickerModel () {
this.isClosed = false;
super();
this.isClosed = new AtomicBoolean(false);
this.entriesList = new ArrayList<>();
this.processingQueue = new ArrayBlockingQueue<>(1);
this.closedLatch = new CountDownLatch(1);
}

public synchronized final
public final synchronized
boolean isEmpty () {
this.blockUntilReady();
return this.is_empty();
Expand All @@ -57,7 +61,7 @@ void blockUntilReady () {
}
}

protected synchronized final
protected final synchronized
void safeWait () {
try {
this.wait();
Expand Down Expand Up @@ -91,13 +95,13 @@ void close () {

public final
boolean isClosed () {
if (!this.isAlive() && !this.isClosed) {
this.isClosed = true;
if (!this.isAlive() && !this.isClosed.get()) {
this.isClosed.set(true);
}
return this.isClosed;
return this.isClosed.get();
}

public synchronized final
public final synchronized
void removeEntry (final @NotNull MemberRecord memberRecord) {
this.blockUntilReady();
this.remove_entry(memberRecord);
Expand All @@ -108,7 +112,7 @@ void remove_entry (final @NotNull MemberRecord memberRecord) {
this.entriesList.remove(memberRecord);
}

public synchronized final
public final synchronized
void addEntry (final @NotNull MemberRecord memberRecord) {
this.blockUntilReady();
this.add_entry(memberRecord);
Expand All @@ -124,7 +128,7 @@ void add_entry (final @NotNull MemberRecord memberRecord) {
@Contract(pure = true)
@NotNull
@UnmodifiableView
public synchronized final
public final synchronized
List<MemberRecord> getEntries () {
this.blockUntilReady();
return Collections.unmodifiableList(this.entriesList);
Expand Down Expand Up @@ -156,7 +160,7 @@ void run () {
} catch (final Throwable e) {
AdminClientTui.catchAll(e);
} finally {
this.isClosed = true;
this.isClosed.set(true);
this.closedLatch.countDown();
}
}
Expand Down

0 comments on commit ec11292

Please sign in to comment.