Skip to content

Commit

Permalink
✨ CodeException tweak stack by code #29
Browse files Browse the repository at this point in the history
  • Loading branch information
trydofor committed Mar 19, 2024
1 parent 6eda088 commit 847cfdc
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 7 deletions.
61 changes: 54 additions & 7 deletions src/main/java/pro/fessional/mirana/pain/CodeException.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,26 @@

import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import pro.fessional.mirana.data.CodeEnum;
import pro.fessional.mirana.data.Null;
import pro.fessional.mirana.evil.TweakingContext;
import pro.fessional.mirana.i18n.I18nAware;
import pro.fessional.mirana.i18n.I18nString;

import java.util.concurrent.ConcurrentHashMap;

/**
* Readability and performance first, with support for stackless exceptions.
* Readability and performance first exception.
* always enable suppression and support tweak stacktrace,
* ClassStack(1) has higher priority than TweakStack(2)
*
* @author trydofor
* @since 2019-05-29
*/
public class CodeException extends RuntimeException implements I18nAware {
private static final long serialVersionUID = 19791023L;
public static final TweakingContext<Boolean> TweakStack = new TweakingContext<>(false);
public static final TweakingCodeException TweakStack = new TweakingCodeException(false);

private final String code;

Expand All @@ -27,28 +32,28 @@ public class CodeException extends RuntimeException implements I18nAware {
* Constructs a stacked or unstacked exception depending on the Global or Thread setting.
*/
public CodeException(String code) {
this(TweakStack.current(true), code, null);
this(TweakStack.current(code), code, null);
}

/**
* Constructs a stacked or unstacked exception depending on the Global or Thread setting.
*/
public CodeException(String code, String message) {
this(TweakStack.current(true), code, message);
this(TweakStack.current(code), code, message);
}

/**
* Constructs a stacked or unstacked exception depending on the Global or Thread setting.
*/
public CodeException(CodeEnum code) {
this(TweakStack.current(true), code, Null.Objects);
this(TweakStack.current(code), code, Null.Objects);
}

/**
* Constructs a stacked or unstacked exception depending on the Global or Thread setting.
*/
public CodeException(CodeEnum code, Object... args) {
this(TweakStack.current(true), code, args);
this(TweakStack.current(code), code, args);
}

/**
Expand All @@ -62,7 +67,7 @@ public CodeException(boolean stack, String code) {
* Constructs a stacked or unstacked exception directly.
*/
public CodeException(boolean stack, String code, String message) {
super(message == null ? Null.notNull(code) : message, null, stack, stack);
super(message == null ? Null.notNull(code) : message, null, true, stack);
if (code == null) {
this.code = "";
this.i18nCode = null;
Expand Down Expand Up @@ -157,4 +162,46 @@ public String getI18nCode() {
public Object[] getI18nArgs() {
return i18nArgs;
}

/**
* tweaking stacktrace by code and thread
*/
public static class TweakingCodeException extends TweakingContext<Boolean> {

public final ConcurrentHashMap<String, Boolean> tweakCode = new ConcurrentHashMap<>();

public TweakingCodeException(boolean initDefault) {
super(initDefault);
}

public boolean current(@Nullable String code) {
if (code == null || code.isEmpty()) {
return TweakStack.current(true);
}
else {
Boolean b = tweakCode.get(code);
return b != null ? b : TweakStack.current(true);
}
}

public boolean current(@Nullable CodeEnum code) {
return current(code == null ? null : code.getCode());
}

public void tweakCode(@NotNull String code, boolean stack) {
tweakCode.put(code, stack);
}

public void tweakCode(@NotNull CodeEnum code, boolean stack) {
tweakCode.put(code.getCode(), stack);
}

public void resetCode(@NotNull String code) {
tweakCode.remove(code);
}

public void resetCode(@NotNull CodeEnum code) {
tweakCode.remove(code.getCode());
}
}
}
17 changes: 17 additions & 0 deletions src/test/java/pro/fessional/mirana/pain/CodeExceptionTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package pro.fessional.mirana.pain;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import pro.fessional.mirana.SystemOut;
import pro.fessional.mirana.data.Null;
Expand Down Expand Up @@ -40,4 +41,20 @@ void testNull() {
assertEquals(0, noc.getStackTrace().length);
assertEquals(0, noc.getSuppressed().length);
}

@Test
void testCode() {
String stack = "stack";
CodeException.TweakStack.tweakCode(stack, true);
final CodeException st1 = new CodeException(stack);
Assertions.assertTrue(st1.getStackTrace().length > 0);

CodeException.TweakStack.tweakCode(stack, false);
final CodeException st2 = new CodeException(stack);
Assertions.assertEquals(0, st2.getStackTrace().length);

CodeException.TweakStack.resetCode(stack);
final CodeException st3 = new CodeException(stack);
Assertions.assertEquals(0, st3.getStackTrace().length);
}
}

0 comments on commit 847cfdc

Please sign in to comment.