Compare commits
32 Commits
conflux@1.
...
release@1.
| Author | SHA1 | Date | |
|---|---|---|---|
|
cc6d541b4c
|
|||
|
90efd93b72
|
|||
|
b002d47d82
|
|||
|
2cf6d206e8
|
|||
|
cadf80ea63
|
|||
|
33037293fc
|
|||
|
8d21502f60
|
|||
|
5f46fa13e6
|
|||
|
456d1cf30c
|
|||
|
e5c4c7afd7
|
|||
|
df948368d4
|
|||
|
9acd15ef6d
|
|||
|
2722115959
|
|||
|
aa77d8fe7d
|
|||
|
ec738e30b8
|
|||
|
c9d17696e2
|
|||
|
36233ab444
|
|||
|
bd7bcf54a5
|
|||
|
28e51d89dc
|
|||
|
33e20b7120
|
|||
|
1e8fdb52dd
|
|||
|
3d2cf37a17
|
|||
|
e2d08d7b70
|
|||
|
e8e6a24065
|
|||
|
ad8c2a6752
|
|||
|
c2c1e26a7c
|
|||
|
5d8e533eff
|
|||
|
432b705327
|
|||
|
4968cc516d
|
|||
|
02cd2acd6e
|
|||
|
90d4e063af
|
|||
|
288fbfe0cc
|
13
.classpath
13
.classpath
@@ -6,12 +6,6 @@
|
||||
<attribute name="gradle_used_by_scope" value="main,test"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" output="bin/main" path="src/main/resources">
|
||||
<attributes>
|
||||
<attribute name="gradle_scope" value="main"/>
|
||||
<attribute name="gradle_used_by_scope" value="main,test"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" output="bin/test" path="src/test/java">
|
||||
<attributes>
|
||||
<attribute name="gradle_scope" value="test"/>
|
||||
@@ -19,13 +13,6 @@
|
||||
<attribute name="test" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" output="bin/test" path="src/test/resources">
|
||||
<attributes>
|
||||
<attribute name="gradle_scope" value="test"/>
|
||||
<attribute name="gradle_used_by_scope" value="test"/>
|
||||
<attribute name="test" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-21/"/>
|
||||
<classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"/>
|
||||
<classpathentry kind="output" path="bin/default"/>
|
||||
|
||||
@@ -3,7 +3,7 @@ name: Release
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- 'conflux@*'
|
||||
- 'release@*'
|
||||
|
||||
jobs:
|
||||
release:
|
||||
@@ -12,6 +12,8 @@ jobs:
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Set up Java 21
|
||||
uses: actions/setup-java@v3
|
||||
@@ -29,18 +31,63 @@ jobs:
|
||||
restore-keys: |
|
||||
${{ runner.os }}-gradle-
|
||||
|
||||
- name: Build and publish to Gitea Maven
|
||||
run: ./gradlew clean publish --no-daemon -PgiteaToken=${{ secrets.CI_PUBLISH_TOKEN }}
|
||||
- name: Setup SSH key and rsync
|
||||
run: |
|
||||
mkdir -p ~/.ssh
|
||||
echo "${{ secrets.JAVADOC_SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa
|
||||
echo "${{ secrets.CI_KNOWN_HOSTS }}" > ~/.ssh/known_hosts
|
||||
chmod -R 600 ~/.ssh
|
||||
rm /etc/apt/sources.list.d/microsoft-prod.list
|
||||
apt-get update
|
||||
apt install -y rsync
|
||||
|
||||
- name: Upload built JAR
|
||||
uses: actions/upload-artifact@v3
|
||||
with:
|
||||
name: conflux
|
||||
path: build/libs/*.jar
|
||||
- name: Build and publish to Gitea Maven and JavaDoc to the website
|
||||
run: ./gradlew clean publish uploadJavadoc --no-daemon -PgiteaToken=${{ secrets.CI_PUBLISH_TOKEN }} -PjavadocUser=${{ vars.JAVADOC_USER }} -PjavadocHost=${{ vars.JAVADOC_HOST }} -PjavadocPath=${{ vars.JAVADOC_PATH }} -PjavadocKeyPath=~/.ssh/id_rsa
|
||||
|
||||
- name: Generate release notes
|
||||
id: notes
|
||||
run: |
|
||||
current_tag="${{ github.ref_name }}"
|
||||
|
||||
# strip the prefix for sorting, keep prefix for matching
|
||||
prefix="release@"
|
||||
|
||||
# get all matching tags, strip prefix, sort them
|
||||
all_versions=$(git tag --list "${prefix}*" | sed "s/^${prefix}//" | sort -V)
|
||||
|
||||
# find previous version
|
||||
previous_tag=""
|
||||
for v in $all_versions; do
|
||||
if [[ "$prefix$v" == "$current_tag" ]]; then
|
||||
break
|
||||
fi
|
||||
previous_tag="$prefix$v"
|
||||
done
|
||||
|
||||
if [[ -z "$previous_tag" ]]; then
|
||||
range=""
|
||||
else
|
||||
range="$previous_tag..$current_tag"
|
||||
fi
|
||||
|
||||
echo "Comparing range: $range"
|
||||
|
||||
body="## What's New"
|
||||
|
||||
for category in "feat: Features" "fix: Bug Fixes" "docs: Documentation" "chore: Chores"; do
|
||||
prefix="${category%%:*}"
|
||||
title="${category##*: }"
|
||||
entries=$(git log $range --pretty=format:"- %s" --grep="^$prefix" --no-merges)
|
||||
# echo -e "Found:\n\n$entries\n\n"
|
||||
if [[ -n "$entries" ]]; then
|
||||
body="$body\n\n### $title\n$entries"
|
||||
fi
|
||||
done
|
||||
|
||||
echo -e "$body" > /tmp/release_notes.md
|
||||
|
||||
- name: Create Gitea Release
|
||||
uses: softprops/action-gh-release@v2
|
||||
with:
|
||||
files: build/libs/*.jar
|
||||
tag_name: ${{ github.ref_name }}
|
||||
name: Release ${{ github.ref_name }}
|
||||
body_path: /tmp/release_notes.md
|
||||
|
||||
@@ -40,7 +40,7 @@ jobs:
|
||||
|
||||
- name: Bump version and tag
|
||||
run: |
|
||||
latest=$(git tag --list 'conflux@*' | sed 's/conflux@//' | sort -V | tail -n 1)
|
||||
latest=$(git tag --list 'release@*' | sed 's/release@//' | sort -V | tail -n 1)
|
||||
if [[ -z "$latest" ]]; then
|
||||
latest="0.0.0"
|
||||
fi
|
||||
@@ -59,7 +59,7 @@ jobs:
|
||||
;;
|
||||
esac
|
||||
next="$major.$minor.$patch"
|
||||
new_tag="conflux@$next"
|
||||
new_tag="release@$next"
|
||||
git tag -s $new_tag -m "Release $new_tag"
|
||||
git push origin $new_tag
|
||||
echo "Tagged $new_tag"
|
||||
|
||||
6
.project
6
.project
@@ -15,15 +15,9 @@
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>net.sourceforge.pmd.eclipse.plugin.pmdBuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
|
||||
<nature>net.sourceforge.pmd.eclipse.plugin.pmdNature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
|
||||
298
.ruleset
Normal file
298
.ruleset
Normal file
@@ -0,0 +1,298 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ruleset xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
name="EgothorRuleset"
|
||||
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd">
|
||||
<description>Egothor preferences rule set</description>
|
||||
<exclude-pattern>.*</exclude-pattern>
|
||||
<include-pattern>.*\.java</include-pattern>
|
||||
<rule ref="category/java/bestpractices.xml/AbstractClassWithoutAbstractMethod"/>
|
||||
<rule ref="category/java/bestpractices.xml/AccessorClassGeneration"/>
|
||||
<rule ref="category/java/bestpractices.xml/AccessorMethodGeneration"/>
|
||||
<rule ref="category/java/bestpractices.xml/ArrayIsStoredDirectly"/>
|
||||
<rule ref="category/java/bestpractices.xml/AvoidMessageDigestField"/>
|
||||
<rule ref="category/java/bestpractices.xml/AvoidPrintStackTrace"/>
|
||||
<rule ref="category/java/bestpractices.xml/AvoidReassigningCatchVariables"/>
|
||||
<rule ref="category/java/bestpractices.xml/AvoidReassigningLoopVariables"/>
|
||||
<rule ref="category/java/bestpractices.xml/AvoidReassigningParameters"/>
|
||||
<rule ref="category/java/bestpractices.xml/AvoidStringBufferField"/>
|
||||
<rule ref="category/java/bestpractices.xml/AvoidUsingHardCodedIP"/>
|
||||
<rule ref="category/java/bestpractices.xml/CheckResultSet"/>
|
||||
<rule ref="category/java/bestpractices.xml/ConstantsInInterface"/>
|
||||
<rule ref="category/java/bestpractices.xml/DefaultLabelNotLastInSwitch"/>
|
||||
<rule ref="category/java/bestpractices.xml/DoubleBraceInitialization"/>
|
||||
<rule ref="category/java/bestpractices.xml/ExhaustiveSwitchHasDefault"/>
|
||||
<rule ref="category/java/bestpractices.xml/ForLoopCanBeForeach"/>
|
||||
<rule ref="category/java/bestpractices.xml/ForLoopVariableCount"/>
|
||||
<rule ref="category/java/bestpractices.xml/GuardLogStatement"/>
|
||||
<rule ref="category/java/bestpractices.xml/ImplicitFunctionalInterface"/>
|
||||
<rule ref="category/java/bestpractices.xml/JUnit4SuitesShouldUseSuiteAnnotation"/>
|
||||
<rule ref="category/java/bestpractices.xml/JUnit5TestShouldBePackagePrivate"/>
|
||||
<rule ref="category/java/bestpractices.xml/JUnitUseExpected"/>
|
||||
<rule ref="category/java/bestpractices.xml/LiteralsFirstInComparisons"/>
|
||||
<rule ref="category/java/bestpractices.xml/LooseCoupling"/>
|
||||
<rule ref="category/java/bestpractices.xml/MethodReturnsInternalArray"/>
|
||||
<rule ref="category/java/bestpractices.xml/MissingOverride"/>
|
||||
<rule ref="category/java/bestpractices.xml/NonExhaustiveSwitch"/>
|
||||
<rule ref="category/java/bestpractices.xml/OneDeclarationPerLine"/>
|
||||
<rule ref="category/java/bestpractices.xml/PreserveStackTrace"/>
|
||||
<rule ref="category/java/bestpractices.xml/PrimitiveWrapperInstantiation"/>
|
||||
<rule ref="category/java/bestpractices.xml/ReplaceEnumerationWithIterator"/>
|
||||
<rule ref="category/java/bestpractices.xml/ReplaceHashtableWithMap"/>
|
||||
<rule ref="category/java/bestpractices.xml/ReplaceVectorWithList"/>
|
||||
<rule ref="category/java/bestpractices.xml/SimplifiableTestAssertion"/>
|
||||
<rule ref="category/java/bestpractices.xml/SystemPrintln"/>
|
||||
<rule ref="category/java/bestpractices.xml/UnitTestAssertionsShouldIncludeMessage"/>
|
||||
<rule ref="category/java/bestpractices.xml/UnitTestContainsTooManyAsserts"/>
|
||||
<rule ref="category/java/bestpractices.xml/UnitTestShouldIncludeAssert"/>
|
||||
<rule ref="category/java/bestpractices.xml/UnitTestShouldUseAfterAnnotation"/>
|
||||
<rule ref="category/java/bestpractices.xml/UnitTestShouldUseBeforeAnnotation"/>
|
||||
<rule ref="category/java/bestpractices.xml/UnitTestShouldUseTestAnnotation"/>
|
||||
<rule ref="category/java/bestpractices.xml/UnnecessaryVarargsArrayCreation"/>
|
||||
<rule ref="category/java/bestpractices.xml/UnnecessaryWarningSuppression"/>
|
||||
<rule ref="category/java/bestpractices.xml/UnusedAssignment"/>
|
||||
<rule ref="category/java/bestpractices.xml/UnusedFormalParameter"/>
|
||||
<rule ref="category/java/bestpractices.xml/UnusedLocalVariable"/>
|
||||
<rule ref="category/java/bestpractices.xml/UnusedPrivateField"/>
|
||||
<rule ref="category/java/bestpractices.xml/UnusedPrivateMethod"/>
|
||||
<rule ref="category/java/bestpractices.xml/UseCollectionIsEmpty"/>
|
||||
<rule ref="category/java/bestpractices.xml/UseEnumCollections"/>
|
||||
<rule ref="category/java/bestpractices.xml/UseStandardCharsets"/>
|
||||
<rule ref="category/java/bestpractices.xml/UseTryWithResources"/>
|
||||
<rule ref="category/java/bestpractices.xml/UseVarargs"/>
|
||||
<rule ref="category/java/bestpractices.xml/WhileLoopWithLiteralBoolean"/>
|
||||
<rule ref="category/java/codestyle.xml/AtLeastOneConstructor"/>
|
||||
<rule ref="category/java/codestyle.xml/AvoidDollarSigns"/>
|
||||
<rule ref="category/java/codestyle.xml/AvoidProtectedFieldInFinalClass"/>
|
||||
<rule ref="category/java/codestyle.xml/AvoidProtectedMethodInFinalClassNotExtending"/>
|
||||
<rule ref="category/java/codestyle.xml/AvoidUsingNativeCode"/>
|
||||
<rule ref="category/java/codestyle.xml/BooleanGetMethodName"/>
|
||||
<rule ref="category/java/codestyle.xml/CallSuperInConstructor"/>
|
||||
<rule ref="category/java/codestyle.xml/ClassNamingConventions"/>
|
||||
<rule ref="category/java/codestyle.xml/CommentDefaultAccessModifier"/>
|
||||
<rule ref="category/java/codestyle.xml/ConfusingTernary"/>
|
||||
<rule ref="category/java/codestyle.xml/ControlStatementBraces"/>
|
||||
<rule ref="category/java/codestyle.xml/EmptyControlStatement"/>
|
||||
<rule ref="category/java/codestyle.xml/EmptyMethodInAbstractClassShouldBeAbstract"/>
|
||||
<rule ref="category/java/codestyle.xml/ExtendsObject"/>
|
||||
<rule ref="category/java/codestyle.xml/FieldDeclarationsShouldBeAtStartOfClass"/>
|
||||
<rule ref="category/java/codestyle.xml/FieldNamingConventions"/>
|
||||
<rule ref="category/java/codestyle.xml/FinalParameterInAbstractMethod"/>
|
||||
<rule ref="category/java/codestyle.xml/ForLoopShouldBeWhileLoop"/>
|
||||
<rule ref="category/java/codestyle.xml/FormalParameterNamingConventions"/>
|
||||
<rule ref="category/java/codestyle.xml/GenericsNaming"/>
|
||||
<rule ref="category/java/codestyle.xml/IdenticalCatchBranches"/>
|
||||
<rule ref="category/java/codestyle.xml/LambdaCanBeMethodReference"/>
|
||||
<rule ref="category/java/codestyle.xml/LinguisticNaming"/>
|
||||
<rule ref="category/java/codestyle.xml/LocalHomeNamingConvention"/>
|
||||
<rule ref="category/java/codestyle.xml/LocalInterfaceSessionNamingConvention"/>
|
||||
<!-- rule ref="category/java/codestyle.xml/LocalVariableCouldBeFinal"/ -->
|
||||
<rule ref="category/java/codestyle.xml/LocalVariableNamingConventions"/>
|
||||
<rule ref="category/java/codestyle.xml/LongVariable"/>
|
||||
<rule ref="category/java/codestyle.xml/MDBAndSessionBeanNamingConvention"/>
|
||||
<!-- rule ref="category/java/codestyle.xml/MethodArgumentCouldBeFinal"/ -->
|
||||
<rule ref="category/java/codestyle.xml/MethodNamingConventions"/>
|
||||
<rule ref="category/java/codestyle.xml/NoPackage"/>
|
||||
<rule ref="category/java/codestyle.xml/OnlyOneReturn"/>
|
||||
<rule ref="category/java/codestyle.xml/PackageCase"/>
|
||||
<rule ref="category/java/codestyle.xml/PrematureDeclaration"/>
|
||||
<rule ref="category/java/codestyle.xml/RemoteInterfaceNamingConvention"/>
|
||||
<rule ref="category/java/codestyle.xml/RemoteSessionInterfaceNamingConvention"/>
|
||||
<rule ref="category/java/codestyle.xml/ShortClassName"/>
|
||||
<rule ref="category/java/codestyle.xml/ShortMethodName"/>
|
||||
<!-- rule ref="category/java/codestyle.xml/ShortVariable"/ -->
|
||||
<rule ref="category/java/codestyle.xml/TooManyStaticImports"/>
|
||||
<rule ref="category/java/codestyle.xml/UnnecessaryAnnotationValueElement"/>
|
||||
<rule ref="category/java/codestyle.xml/UnnecessaryBoxing"/>
|
||||
<rule ref="category/java/codestyle.xml/UnnecessaryCast"/>
|
||||
<rule ref="category/java/codestyle.xml/UnnecessaryConstructor"/>
|
||||
<rule ref="category/java/codestyle.xml/UnnecessaryFullyQualifiedName"/>
|
||||
<rule ref="category/java/codestyle.xml/UnnecessaryImport"/>
|
||||
<rule ref="category/java/codestyle.xml/UnnecessaryLocalBeforeReturn"/>
|
||||
<rule ref="category/java/codestyle.xml/UnnecessaryModifier"/>
|
||||
<rule ref="category/java/codestyle.xml/UnnecessaryReturn"/>
|
||||
<rule ref="category/java/codestyle.xml/UnnecessarySemicolon"/>
|
||||
<rule ref="category/java/codestyle.xml/UseDiamondOperator"/>
|
||||
<rule ref="category/java/codestyle.xml/UseExplicitTypes"/>
|
||||
<rule ref="category/java/codestyle.xml/UselessParentheses"/>
|
||||
<rule ref="category/java/codestyle.xml/UselessQualifiedThis"/>
|
||||
<rule ref="category/java/codestyle.xml/UseShortArrayInitializer"/>
|
||||
<rule ref="category/java/codestyle.xml/UseUnderscoresInNumericLiterals"/>
|
||||
<rule ref="category/java/design.xml/AbstractClassWithoutAnyMethod"/>
|
||||
<rule ref="category/java/design.xml/AvoidCatchingGenericException"/>
|
||||
<rule ref="category/java/design.xml/AvoidDeeplyNestedIfStmts"/>
|
||||
<rule ref="category/java/design.xml/AvoidRethrowingException"/>
|
||||
<rule ref="category/java/design.xml/AvoidThrowingNewInstanceOfSameException"/>
|
||||
<rule ref="category/java/design.xml/AvoidThrowingNullPointerException"/>
|
||||
<rule ref="category/java/design.xml/AvoidThrowingRawExceptionTypes"/>
|
||||
<rule ref="category/java/design.xml/AvoidUncheckedExceptionsInSignatures"/>
|
||||
<rule ref="category/java/design.xml/ClassWithOnlyPrivateConstructorsShouldBeFinal"/>
|
||||
<rule ref="category/java/design.xml/CognitiveComplexity"/>
|
||||
<rule ref="category/java/design.xml/CollapsibleIfStatements"/>
|
||||
<rule ref="category/java/design.xml/CouplingBetweenObjects"/>
|
||||
<rule ref="category/java/design.xml/CyclomaticComplexity"/>
|
||||
<rule ref="category/java/design.xml/DataClass"/>
|
||||
<rule ref="category/java/design.xml/DoNotExtendJavaLangError"/>
|
||||
<rule ref="category/java/design.xml/ExceptionAsFlowControl"/>
|
||||
<!-- rule ref="category/java/design.xml/ExcessiveImports"/ -->
|
||||
<rule ref="category/java/design.xml/ExcessiveParameterList"/>
|
||||
<rule ref="category/java/design.xml/ExcessivePublicCount"/>
|
||||
<rule ref="category/java/design.xml/FinalFieldCouldBeStatic"/>
|
||||
<rule ref="category/java/design.xml/GodClass"/>
|
||||
<rule ref="category/java/design.xml/ImmutableField"/>
|
||||
<rule ref="category/java/design.xml/InvalidJavaBean"/>
|
||||
<!-- rule ref="category/java/design.xml/LawOfDemeter"/ -->
|
||||
<rule ref="category/java/design.xml/LogicInversion"/>
|
||||
<!-- rule ref="category/java/design.xml/LoosePackageCoupling"/ -->
|
||||
<rule ref="category/java/design.xml/MutableStaticState"/>
|
||||
<rule ref="category/java/design.xml/NcssCount"/>
|
||||
<rule ref="category/java/design.xml/NPathComplexity"/>
|
||||
<rule ref="category/java/design.xml/SignatureDeclareThrowsException"/>
|
||||
<rule ref="category/java/design.xml/SimplifiedTernary"/>
|
||||
<rule ref="category/java/design.xml/SimplifyBooleanExpressions"/>
|
||||
<rule ref="category/java/design.xml/SimplifyBooleanReturns"/>
|
||||
<rule ref="category/java/design.xml/SimplifyConditional"/>
|
||||
<rule ref="category/java/design.xml/SingularField"/>
|
||||
<rule ref="category/java/design.xml/SwitchDensity"/>
|
||||
<rule ref="category/java/design.xml/TooManyFields"/>
|
||||
<rule ref="category/java/design.xml/TooManyMethods"/>
|
||||
<rule ref="category/java/design.xml/UselessOverridingMethod"/>
|
||||
<rule ref="category/java/design.xml/UseObjectForClearerAPI"/>
|
||||
<rule ref="category/java/design.xml/UseUtilityClass"/>
|
||||
<rule ref="category/java/documentation.xml/CommentContent"/>
|
||||
<rule ref="category/java/documentation.xml/CommentRequired">
|
||||
<properties>
|
||||
<property name="fieldCommentRequirement" value="Ignored"/>
|
||||
</properties>
|
||||
</rule>
|
||||
<rule ref="category/java/documentation.xml/CommentSize">
|
||||
<properties>
|
||||
<property name="maxLines" value="40"/>
|
||||
</properties>
|
||||
</rule>
|
||||
<rule ref="category/java/documentation.xml/UncommentedEmptyConstructor"/>
|
||||
<rule ref="category/java/documentation.xml/UncommentedEmptyMethodBody"/>
|
||||
<rule ref="category/java/errorprone.xml/AssignmentInOperand"/>
|
||||
<rule ref="category/java/errorprone.xml/AssignmentToNonFinalStatic"/>
|
||||
<rule ref="category/java/errorprone.xml/AvoidAccessibilityAlteration"/>
|
||||
<rule ref="category/java/errorprone.xml/AvoidAssertAsIdentifier"/>
|
||||
<rule ref="category/java/errorprone.xml/AvoidBranchingStatementAsLastInLoop"/>
|
||||
<rule ref="category/java/errorprone.xml/AvoidCallingFinalize"/>
|
||||
<rule ref="category/java/errorprone.xml/AvoidCatchingNPE"/>
|
||||
<rule ref="category/java/errorprone.xml/AvoidCatchingThrowable"/>
|
||||
<rule ref="category/java/errorprone.xml/AvoidDecimalLiteralsInBigDecimalConstructor"/>
|
||||
<rule ref="category/java/errorprone.xml/AvoidDuplicateLiterals"/>
|
||||
<rule ref="category/java/errorprone.xml/AvoidEnumAsIdentifier"/>
|
||||
<rule ref="category/java/errorprone.xml/AvoidFieldNameMatchingMethodName"/>
|
||||
<rule ref="category/java/errorprone.xml/AvoidFieldNameMatchingTypeName"/>
|
||||
<rule ref="category/java/errorprone.xml/AvoidInstanceofChecksInCatchClause"/>
|
||||
<rule ref="category/java/errorprone.xml/AvoidLiteralsInIfCondition"/>
|
||||
<rule ref="category/java/errorprone.xml/AvoidLosingExceptionInformation"/>
|
||||
<rule ref="category/java/errorprone.xml/AvoidMultipleUnaryOperators"/>
|
||||
<rule ref="category/java/errorprone.xml/AvoidUsingOctalValues"/>
|
||||
<rule ref="category/java/errorprone.xml/BrokenNullCheck"/>
|
||||
<rule ref="category/java/errorprone.xml/CallSuperFirst"/>
|
||||
<rule ref="category/java/errorprone.xml/CallSuperLast"/>
|
||||
<rule ref="category/java/errorprone.xml/CheckSkipResult"/>
|
||||
<rule ref="category/java/errorprone.xml/ClassCastExceptionWithToArray"/>
|
||||
<rule ref="category/java/errorprone.xml/CloneMethodMustBePublic"/>
|
||||
<rule ref="category/java/errorprone.xml/CloneMethodMustImplementCloneable"/>
|
||||
<rule ref="category/java/errorprone.xml/CloneMethodReturnTypeMustMatchClassName"/>
|
||||
<rule ref="category/java/errorprone.xml/CloseResource"/>
|
||||
<rule ref="category/java/errorprone.xml/CompareObjectsWithEquals"/>
|
||||
<rule ref="category/java/errorprone.xml/ComparisonWithNaN"/>
|
||||
<rule ref="category/java/errorprone.xml/ConfusingArgumentToVarargsMethod"/>
|
||||
<rule ref="category/java/errorprone.xml/ConstructorCallsOverridableMethod"/>
|
||||
<rule ref="category/java/errorprone.xml/DetachedTestCase"/>
|
||||
<rule ref="category/java/errorprone.xml/DoNotCallGarbageCollectionExplicitly"/>
|
||||
<rule ref="category/java/errorprone.xml/DoNotExtendJavaLangThrowable"/>
|
||||
<rule ref="category/java/errorprone.xml/DoNotHardCodeSDCard"/>
|
||||
<rule ref="category/java/errorprone.xml/DoNotTerminateVM"/>
|
||||
<rule ref="category/java/errorprone.xml/DoNotThrowExceptionInFinally"/>
|
||||
<rule ref="category/java/errorprone.xml/DontImportSun"/>
|
||||
<rule ref="category/java/errorprone.xml/DontUseFloatTypeForLoopIndices"/>
|
||||
<rule ref="category/java/errorprone.xml/EmptyCatchBlock"/>
|
||||
<rule ref="category/java/errorprone.xml/EmptyFinalizer"/>
|
||||
<rule ref="category/java/errorprone.xml/EqualsNull"/>
|
||||
<rule ref="category/java/errorprone.xml/FinalizeDoesNotCallSuperFinalize"/>
|
||||
<rule ref="category/java/errorprone.xml/FinalizeOnlyCallsSuperFinalize"/>
|
||||
<rule ref="category/java/errorprone.xml/FinalizeOverloaded"/>
|
||||
<rule ref="category/java/errorprone.xml/FinalizeShouldBeProtected"/>
|
||||
<rule ref="category/java/errorprone.xml/IdempotentOperations"/>
|
||||
<rule ref="category/java/errorprone.xml/ImplicitSwitchFallThrough"/>
|
||||
<rule ref="category/java/errorprone.xml/InstantiationToGetClass"/>
|
||||
<rule ref="category/java/errorprone.xml/InvalidLogMessageFormat"/>
|
||||
<rule ref="category/java/errorprone.xml/JumbledIncrementer"/>
|
||||
<rule ref="category/java/errorprone.xml/JUnitSpelling"/>
|
||||
<rule ref="category/java/errorprone.xml/JUnitStaticSuite"/>
|
||||
<rule ref="category/java/errorprone.xml/MethodWithSameNameAsEnclosingClass"/>
|
||||
<rule ref="category/java/errorprone.xml/MisplacedNullCheck"/>
|
||||
<rule ref="category/java/errorprone.xml/MissingSerialVersionUID"/>
|
||||
<rule ref="category/java/errorprone.xml/MissingStaticMethodInNonInstantiatableClass"/>
|
||||
<rule ref="category/java/errorprone.xml/MoreThanOneLogger"/>
|
||||
<rule ref="category/java/errorprone.xml/NonCaseLabelInSwitch"/>
|
||||
<rule ref="category/java/errorprone.xml/NonSerializableClass"/>
|
||||
<rule ref="category/java/errorprone.xml/NonStaticInitializer"/>
|
||||
<rule ref="category/java/errorprone.xml/NullAssignment"/>
|
||||
<rule ref="category/java/errorprone.xml/OverrideBothEqualsAndHashcode"/>
|
||||
<rule ref="category/java/errorprone.xml/ProperCloneImplementation"/>
|
||||
<rule ref="category/java/errorprone.xml/ProperLogger"/>
|
||||
<rule ref="category/java/errorprone.xml/ReturnEmptyCollectionRatherThanNull"/>
|
||||
<rule ref="category/java/errorprone.xml/ReturnFromFinallyBlock"/>
|
||||
<rule ref="category/java/errorprone.xml/SimpleDateFormatNeedsLocale"/>
|
||||
<rule ref="category/java/errorprone.xml/SingleMethodSingleton"/>
|
||||
<rule ref="category/java/errorprone.xml/SingletonClassReturningNewInstance"/>
|
||||
<rule ref="category/java/errorprone.xml/StaticEJBFieldShouldBeFinal"/>
|
||||
<rule ref="category/java/errorprone.xml/StringBufferInstantiationWithChar"/>
|
||||
<rule ref="category/java/errorprone.xml/SuspiciousEqualsMethodName"/>
|
||||
<rule ref="category/java/errorprone.xml/SuspiciousHashcodeMethodName"/>
|
||||
<rule ref="category/java/errorprone.xml/SuspiciousOctalEscape"/>
|
||||
<rule ref="category/java/errorprone.xml/TestClassWithoutTestCases"/>
|
||||
<rule ref="category/java/errorprone.xml/UnconditionalIfStatement"/>
|
||||
<rule ref="category/java/errorprone.xml/UnnecessaryBooleanAssertion"/>
|
||||
<rule ref="category/java/errorprone.xml/UnnecessaryCaseChange"/>
|
||||
<rule ref="category/java/errorprone.xml/UnnecessaryConversionTemporary"/>
|
||||
<rule ref="category/java/errorprone.xml/UnusedNullCheckInEquals"/>
|
||||
<rule ref="category/java/errorprone.xml/UseCorrectExceptionLogging"/>
|
||||
<rule ref="category/java/errorprone.xml/UseEqualsToCompareStrings"/>
|
||||
<rule ref="category/java/errorprone.xml/UselessOperationOnImmutable"/>
|
||||
<rule ref="category/java/errorprone.xml/UseLocaleWithCaseConversions"/>
|
||||
<rule ref="category/java/errorprone.xml/UseProperClassLoader"/>
|
||||
<rule ref="category/java/multithreading.xml/AvoidSynchronizedAtMethodLevel"/>
|
||||
<rule ref="category/java/multithreading.xml/AvoidSynchronizedStatement"/>
|
||||
<rule ref="category/java/multithreading.xml/AvoidThreadGroup"/>
|
||||
<rule ref="category/java/multithreading.xml/AvoidUsingVolatile"/>
|
||||
<rule ref="category/java/multithreading.xml/DoNotUseThreads"/>
|
||||
<rule ref="category/java/multithreading.xml/DontCallThreadRun"/>
|
||||
<rule ref="category/java/multithreading.xml/DoubleCheckedLocking"/>
|
||||
<rule ref="category/java/multithreading.xml/NonThreadSafeSingleton"/>
|
||||
<rule ref="category/java/multithreading.xml/UnsynchronizedStaticFormatter"/>
|
||||
<rule ref="category/java/multithreading.xml/UseConcurrentHashMap"/>
|
||||
<rule ref="category/java/multithreading.xml/UseNotifyAllInsteadOfNotify"/>
|
||||
<rule ref="category/java/performance.xml/AddEmptyString"/>
|
||||
<rule ref="category/java/performance.xml/AppendCharacterWithChar"/>
|
||||
<rule ref="category/java/performance.xml/AvoidArrayLoops"/>
|
||||
<rule ref="category/java/performance.xml/AvoidCalendarDateCreation"/>
|
||||
<rule ref="category/java/performance.xml/AvoidFileStream"/>
|
||||
<rule ref="category/java/performance.xml/AvoidInstantiatingObjectsInLoops"/>
|
||||
<rule ref="category/java/performance.xml/BigIntegerInstantiation"/>
|
||||
<rule ref="category/java/performance.xml/ConsecutiveAppendsShouldReuse"/>
|
||||
<rule ref="category/java/performance.xml/ConsecutiveLiteralAppends"/>
|
||||
<rule ref="category/java/performance.xml/InefficientEmptyStringCheck"/>
|
||||
<rule ref="category/java/performance.xml/InefficientStringBuffering"/>
|
||||
<rule ref="category/java/performance.xml/InsufficientStringBufferDeclaration"/>
|
||||
<rule ref="category/java/performance.xml/OptimizableToArrayCall"/>
|
||||
<rule ref="category/java/performance.xml/RedundantFieldInitializer"/>
|
||||
<rule ref="category/java/performance.xml/StringInstantiation"/>
|
||||
<rule ref="category/java/performance.xml/StringToString"/>
|
||||
<rule ref="category/java/performance.xml/TooFewBranchesForSwitch"/>
|
||||
<rule ref="category/java/performance.xml/UseArrayListInsteadOfVector"/>
|
||||
<rule ref="category/java/performance.xml/UseArraysAsList"/>
|
||||
<rule ref="category/java/performance.xml/UseIndexOfChar"/>
|
||||
<rule ref="category/java/performance.xml/UseIOStreamsWithApacheCommonsFileItem"/>
|
||||
<rule ref="category/java/performance.xml/UselessStringValueOf"/>
|
||||
<rule ref="category/java/performance.xml/UseStringBufferForStringAppends"/>
|
||||
<rule ref="category/java/performance.xml/UseStringBufferLength"/>
|
||||
<rule ref="category/java/security.xml/HardCodedCryptoKey"/>
|
||||
<rule ref="category/java/security.xml/InsecureCryptoIv"/>
|
||||
</ruleset>
|
||||
33
README.md
33
README.md
@@ -8,7 +8,32 @@ values and react to their changes in a clean and decoupled way.
|
||||
|
||||
## Key Features
|
||||
|
||||
- Shared context for storing and retrieving named values
|
||||
- Publish/subscribe event bus for listening to value changes
|
||||
- Simple API for easy integration into existing projects
|
||||
- Enables modular design by decoupling components through events
|
||||
- Shared context for storing and retrieving named values
|
||||
- Publish/subscribe event bus for listening to value changes
|
||||
- Simple API for easy integration into existing projects
|
||||
- Enables modular design by decoupling components through events
|
||||
|
||||
## Getting Started
|
||||
|
||||
Add to your `build.gradle`:
|
||||
```groovy
|
||||
repositories {
|
||||
|
||||
maven {
|
||||
name = "GiteaMaven"
|
||||
url = uri("https://gitea.egothor.org/api/packages/Egothor/maven")
|
||||
}
|
||||
|
||||
// Use Maven Central for resolving dependencies.
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation 'org.egothor:conflux:x.x.x'
|
||||
}
|
||||
```
|
||||
|
||||
## Documentation
|
||||
|
||||
- [Conflux JavaDoc](https://www.egothor.org/javadoc/conflux/)
|
||||
- [Conflux Wiki](https://gitea.egothor.org/Egothor/conflux/wiki)
|
||||
|
||||
99
build.gradle
99
build.gradle
@@ -3,10 +3,15 @@ plugins {
|
||||
id 'java-library'
|
||||
id 'maven-publish'
|
||||
id 'com.palantir.git-version' version '4.0.0'
|
||||
id 'pmd'
|
||||
}
|
||||
|
||||
import java.time.LocalDate
|
||||
|
||||
def currentYear=LocalDate.now().getYear()
|
||||
|
||||
group 'org.egothor'
|
||||
version gitVersion(prefix:'conflux@')
|
||||
version gitVersion(prefix:'release@')
|
||||
|
||||
repositories {
|
||||
// Use Maven Central for resolving dependencies.
|
||||
@@ -20,43 +25,97 @@ dependencies {
|
||||
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
|
||||
}
|
||||
|
||||
pmd {
|
||||
consoleOutput = true
|
||||
toolVersion = '7.16.0'
|
||||
sourceSets = [sourceSets.main]
|
||||
ruleSetFiles = files(rootProject.file(".ruleset"))
|
||||
}
|
||||
|
||||
// Apply a specific Java toolchain to ease working on different environments.
|
||||
java {
|
||||
toolchain {
|
||||
languageVersion = JavaLanguageVersion.of(21)
|
||||
}
|
||||
|
||||
withJavadocJar()
|
||||
withSourcesJar()
|
||||
}
|
||||
|
||||
javadoc {
|
||||
failOnError = false
|
||||
}
|
||||
failOnError = false
|
||||
|
||||
options.addBooleanOption('html5', true)
|
||||
options.tags('apiNote:a:API Note:')
|
||||
options.tags('implSpec:a:Implementation Requirements:')
|
||||
options.tags('implNote:a:Implementation Note:')
|
||||
options.tags('param')
|
||||
options.tags('return')
|
||||
options.tags('throws')
|
||||
options.tags('since')
|
||||
options.tags('version')
|
||||
options.tags('serialData')
|
||||
options.tags('factory')
|
||||
options.tags('see')
|
||||
|
||||
options.use = true
|
||||
options.author = true
|
||||
options.version = true
|
||||
options.windowTitle = 'Conflux'
|
||||
|
||||
options.bottom = '<div style="text-align: right; padding: 5px;">Copyright © ' + currentYear +
|
||||
' Egothor - Version ' + version +
|
||||
' - <a href="https://gitea.egothor.org/Egothor/conflux/raw/branch/main/LICENSE">License</a>' +
|
||||
'</div>'
|
||||
source = sourceSets.main.allJava}
|
||||
|
||||
tasks.named('test') {
|
||||
// Use JUnit Platform for unit tests.
|
||||
useJUnitPlatform()
|
||||
}
|
||||
|
||||
publishing {
|
||||
publications {
|
||||
mavenJava(MavenPublication) {
|
||||
from components.java
|
||||
}
|
||||
}
|
||||
repositories {
|
||||
maven {
|
||||
name = "GiteaMaven"
|
||||
url = uri("https://gitea.egothor.org/api/packages/Egothor/maven")
|
||||
|
||||
credentials(HttpHeaderCredentials) {
|
||||
name = "Authorization"
|
||||
value = "token ${giteaToken}"
|
||||
}
|
||||
task uploadJavadoc(type: Exec) {
|
||||
dependsOn javadoc
|
||||
|
||||
authentication {
|
||||
header(HttpHeaderAuthentication)
|
||||
doFirst {
|
||||
def javadocDir = tasks.javadoc.destinationDir
|
||||
def relativeJavadocDir = project.projectDir.toPath().relativize(javadocDir.toPath()).toString()
|
||||
|
||||
println "Uploading Javadoc with key: ${javadocKeyPath}"
|
||||
println " from relative path: $relativeJavadocDir"
|
||||
|
||||
commandLine "rsync", "-avz", "--delete",
|
||||
"-e", "ssh -i ${javadocKeyPath} -o IdentitiesOnly=yes",
|
||||
relativeJavadocDir + '/', "${javadocUser}@${javadocHost}:${javadocPath}"
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (project.hasProperty('giteaToken') && project.giteaToken) {
|
||||
publishing {
|
||||
publications {
|
||||
mavenJava(MavenPublication) {
|
||||
from components.java
|
||||
}
|
||||
}
|
||||
repositories {
|
||||
maven {
|
||||
name = "GiteaMaven"
|
||||
url = uri("https://gitea.egothor.org/api/packages/Egothor/maven")
|
||||
|
||||
credentials(HttpHeaderCredentials) {
|
||||
name = "Authorization"
|
||||
value = "token ${giteaToken}"
|
||||
}
|
||||
|
||||
authentication {
|
||||
header(HttpHeaderAuthentication)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
println "No giteaToken defined - skipping publishing configuration"
|
||||
}
|
||||
|
||||
gradle.taskGraph.whenReady { taskGraph ->
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
javadocPath=/var/www/html/javadoc/conflux/
|
||||
@@ -1,12 +1,7 @@
|
||||
# This file was generated by the Gradle 'init' task.
|
||||
# https://docs.gradle.org/current/userguide/platforms.html#sub::toml-dependencies-format
|
||||
|
||||
[versions]
|
||||
commons-math3 = "3.6.1"
|
||||
guava = "33.1.0-jre"
|
||||
junit-jupiter = "5.10.2"
|
||||
|
||||
[libraries]
|
||||
commons-math3 = { module = "org.apache.commons:commons-math3", version.ref = "commons-math3" }
|
||||
guava = { module = "com.google.guava:guava", version.ref = "guava" }
|
||||
junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit-jupiter" }
|
||||
|
||||
@@ -34,169 +34,262 @@
|
||||
*/
|
||||
package conflux;
|
||||
|
||||
import java.lang.ref.ReferenceQueue;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* A globally accessible, type-safe context for sharing data between independent
|
||||
* parts of an application. Supports listeners that are notified whenever a
|
||||
* registered key's value changes.
|
||||
*
|
||||
* This implementation guarantees:
|
||||
* <ul>
|
||||
* <li>Strongly typed keys using {@link Key}</li>
|
||||
* <li>One consistent type per key name</li>
|
||||
* <li>Support for weak-referenced listeners to avoid memory leaks</li>
|
||||
* <li>Thread-safe operations</li>
|
||||
* </ul>
|
||||
*
|
||||
* Usage:
|
||||
* A central context manager for storing and retrieving key-value pairs in a
|
||||
* thread-safe, type-safe manner.
|
||||
*
|
||||
* <pre>
|
||||
* Key<Integer> COUNT = Key.of("count", Integer.class);
|
||||
* Ctx.INSTANCE.put(COUNT, 42);
|
||||
* int v = Ctx.INSTANCE.get(COUNT);
|
||||
* </pre>
|
||||
* This enum serves as both:
|
||||
* <ul>
|
||||
* <li>A singleton context via {@code Ctx.INSTANCE} for global/shared usage</li>
|
||||
* <li>A factory and registry for multiple named independent contexts</li>
|
||||
* </ul>
|
||||
*
|
||||
* Each context provides listener support for reactive value changes.
|
||||
*
|
||||
* @see Key
|
||||
* @see Listener
|
||||
*
|
||||
* @author Leo Galambos
|
||||
*/
|
||||
public enum Ctx {
|
||||
public enum Ctx implements CtxInterface {
|
||||
/**
|
||||
* Singleton instance of the context.
|
||||
* The singleton instance representing the default/global context.
|
||||
*/
|
||||
INSTANCE;
|
||||
|
||||
private final Map<Key<?>, Object> values = new ConcurrentHashMap<>();
|
||||
private final Map<String, Class<?>> keyTypes = new ConcurrentHashMap<>();
|
||||
private final Map<Key<?>, List<WeakReference<Listener<?>>>> listeners = new ConcurrentHashMap<>();
|
||||
/**
|
||||
* The default context instance used by this enum singleton. All interface
|
||||
* method calls delegate to this context.
|
||||
*/
|
||||
private final CtxInterface defaultCtx = new CtxInstance();
|
||||
|
||||
/**
|
||||
* Stores or updates the value for a given key. If the key is used for the first
|
||||
* time, its type is recorded. If the key has been used before with a different
|
||||
* type, an exception will be thrown.
|
||||
*
|
||||
* After setting the value, any registered listeners for that key will be
|
||||
* notified (if the value was modified).
|
||||
*
|
||||
* @param key the key
|
||||
* @param value the value to store
|
||||
* @param <T> the type of the value
|
||||
* @throws IllegalStateException if the key is reused with a different type
|
||||
* A registry of named contexts, allowing for multiple isolated logical
|
||||
* contexts.
|
||||
*/
|
||||
public <T> void put(Key<T> key, T value) {
|
||||
keyTypes.compute(key.name(), (k, existingType) -> {
|
||||
if (existingType == null) {
|
||||
return key.type();
|
||||
} else {
|
||||
if (!existingType.equals(key.type())) {
|
||||
throw new IllegalStateException(
|
||||
"Key '" + key.name() + "' already associated with type " + existingType.getName());
|
||||
}
|
||||
return existingType;
|
||||
private final Map<String, NamedWeakRef> contexts = new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
* A reference queue to learn when weakly referenced contexts are collected.
|
||||
*/
|
||||
private final transient ReferenceQueue<CtxInterface> refQueue = new ReferenceQueue<>();
|
||||
|
||||
/**
|
||||
* A weak reference wrapper that associates a {@link CtxInterface} instance (the
|
||||
* referent) with its corresponding context name.
|
||||
* <p>
|
||||
* Instances of this class are enqueued into a {@link ReferenceQueue} once their
|
||||
* referent becomes unreachable. The extra {@code name} field allows the owning
|
||||
* registry to efficiently remove the corresponding entry from the context map
|
||||
* without requiring a reverse lookup.
|
||||
* </p>
|
||||
*
|
||||
* <h2>Usage</h2>
|
||||
* <ul>
|
||||
* <li>Created whenever a new named context is registered.</li>
|
||||
* <li>Placed into a {@link ConcurrentHashMap} keyed by the same
|
||||
* {@code name}.</li>
|
||||
* <li>When the referent is garbage-collected, this reference is automatically
|
||||
* enqueued, and the registry removes the map entry associated with
|
||||
* {@link #name}.</li>
|
||||
* </ul>
|
||||
*
|
||||
* <p>
|
||||
* This design ensures that contexts are automatically deregistered when no
|
||||
* strong references to them remain, preventing memory leaks while still
|
||||
* allowing explicit removal via {@code removeContext}.
|
||||
* </p>
|
||||
*/
|
||||
private static final class NamedWeakRef extends WeakReference<CtxInterface> {
|
||||
/**
|
||||
* The context name associated with the referent.
|
||||
*/
|
||||
private final String name;
|
||||
|
||||
/**
|
||||
* Creates a new weak reference to the given context instance and associates it
|
||||
* with the provided name.
|
||||
*
|
||||
* @param name the context name; used as the key in the registry
|
||||
* @param referent the context instance being weakly referenced
|
||||
* @param q the reference queue with which the weak reference is
|
||||
* registered
|
||||
*/
|
||||
private NamedWeakRef(String name, CtxInterface referent, ReferenceQueue<CtxInterface> q) {
|
||||
super(referent, q);
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove collected references from map.
|
||||
*/
|
||||
private void drainQueue() {
|
||||
for (NamedWeakRef ref; (ref = (NamedWeakRef) refQueue.poll()) != null;) { // NOPMD
|
||||
contexts.remove(ref.name, ref);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a named context. If the context does not exist, it is lazily created.
|
||||
*
|
||||
* This method allows isolation between different logical scopes (e.g.,
|
||||
* sessions, users).
|
||||
*
|
||||
* @param name the name of the context
|
||||
* @return the associated context instance
|
||||
*/
|
||||
public CtxInterface getContext(String name) {
|
||||
if (name == null || name.isBlank()) {
|
||||
throw new IllegalArgumentException("name must not be null or blank");
|
||||
}
|
||||
drainQueue();
|
||||
NamedWeakRef ref = contexts.compute(name, (k, existing) -> {
|
||||
CtxInterface alive = existing == null ? null : existing.get();
|
||||
if (alive != null) {
|
||||
return existing;
|
||||
}
|
||||
CtxInterface created = new CtxInstance();
|
||||
return new NamedWeakRef(k, created, refQueue);
|
||||
});
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
T previous = (T) values.put(key, value);
|
||||
|
||||
if (Objects.deepEquals(value, previous)) {
|
||||
// no change
|
||||
return;
|
||||
}
|
||||
|
||||
// notify listeners
|
||||
var list = listeners.get(key);
|
||||
if (list != null) {
|
||||
for (var ref : list) {
|
||||
var listener = ref.get();
|
||||
if (listener != null) {
|
||||
@SuppressWarnings("unchecked")
|
||||
Listener<T> typedListener = (Listener<T>) listener;
|
||||
typedListener.valueChanged(value);
|
||||
}
|
||||
}
|
||||
// clean up dead weak references
|
||||
list.removeIf(ref -> ref.get() == null);
|
||||
CtxInterface ctx = ref.get();
|
||||
if (ctx == null) {
|
||||
CtxInterface created = new CtxInstance();
|
||||
NamedWeakRef fresh = new NamedWeakRef(name, created, refQueue);
|
||||
contexts.put(name, fresh);
|
||||
ctx = created;
|
||||
}
|
||||
return ctx;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the value for the given key, or {@code null} if not set.
|
||||
* Removes a named context previously created via {@link #getContext(String)}.
|
||||
* <p>
|
||||
* The context is atomically removed from the internal registry and then
|
||||
* {@linkplain CtxInterface#clear() cleared} to release stored values and
|
||||
* listener references. This operation is thread-safe and idempotent: if the
|
||||
* specified context does not exist, the method returns {@code false} and
|
||||
* performs no action.
|
||||
* </p>
|
||||
* <p>
|
||||
* Calling {@code getContext(name)} again after a successful removal will create
|
||||
* a brand-new, empty context. The default/global context represented by
|
||||
* {@link #INSTANCE} is not affected.
|
||||
* </p>
|
||||
*
|
||||
* @param key the key
|
||||
* @param <T> the type
|
||||
* @return the stored value or null
|
||||
* @param name the name of the context to remove; must not be {@code null} or
|
||||
* blank
|
||||
* @return {@code true} if a context with the given name existed and was
|
||||
* removed; {@code false} otherwise
|
||||
* @throws IllegalArgumentException if {@code name} is {@code null} or blank
|
||||
* @implNote Uses {@link ConcurrentHashMap#remove(Object)} for atomic removal,
|
||||
* then invokes {@link CtxInterface#clear()} on the removed instance.
|
||||
* @since 2025.08
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public boolean removeContext(String name) {
|
||||
if (name == null || name.isBlank()) {
|
||||
throw new IllegalArgumentException("name must not be null or blank");
|
||||
}
|
||||
drainQueue();
|
||||
NamedWeakRef ref = contexts.remove(name);
|
||||
if (ref == null) {
|
||||
return false; // NOPMD
|
||||
}
|
||||
CtxInterface ctx = ref.get();
|
||||
if (ctx != null) {
|
||||
try {
|
||||
ctx.clear();
|
||||
} catch (RuntimeException ignore) { // NOPMD
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the set of names for all currently registered contexts.
|
||||
*
|
||||
* @return an immutable set of context names
|
||||
*/
|
||||
public Set<String> contextNames() {
|
||||
drainQueue();
|
||||
return Set.copyOf(contexts.entrySet().stream().filter(e -> e.getValue().get() != null).map(Map.Entry::getKey)
|
||||
.collect(Collectors.toSet()));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Delegates to the default context instance.
|
||||
*/
|
||||
@Override
|
||||
public <T> void put(Key<T> key, T value) {
|
||||
defaultCtx.put(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Delegates to the default context instance.
|
||||
*/
|
||||
@Override
|
||||
public <T> T get(Key<T> key) {
|
||||
return (T) values.get(key);
|
||||
return defaultCtx.get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a value exists for the given key.
|
||||
*
|
||||
* @param key the key
|
||||
* @return true if a value is present
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Delegates to the default context instance.
|
||||
*/
|
||||
@Override
|
||||
public boolean contains(Key<?> key) {
|
||||
return values.containsKey(key);
|
||||
return defaultCtx.contains(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the value and any listeners for the given key.
|
||||
*
|
||||
* @param key the key
|
||||
* @return the removed value, or null if absent
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Delegates to the default context instance.
|
||||
*/
|
||||
@Override
|
||||
public Object remove(Key<?> key) {
|
||||
keyTypes.remove(key.name());
|
||||
var removed = values.remove(key);
|
||||
listeners.remove(key);
|
||||
return removed;
|
||||
return defaultCtx.remove(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears all keys and listeners from the context.
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Delegates to the default context instance.
|
||||
*/
|
||||
@Override
|
||||
public void clear() {
|
||||
values.clear();
|
||||
keyTypes.clear();
|
||||
listeners.clear();
|
||||
defaultCtx.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a listener to be notified whenever the value for the key changes.
|
||||
* The listener is weakly referenced to avoid preventing its garbage collection.
|
||||
*
|
||||
* @param key the key
|
||||
* @param listener the listener
|
||||
* @param <T> the type
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Delegates to the default context instance.
|
||||
*/
|
||||
@Override
|
||||
public <T> void addListener(Key<T> key, Listener<T> listener) {
|
||||
listeners.computeIfAbsent(key, k -> new CopyOnWriteArrayList<>()).add(new WeakReference<>(listener));
|
||||
defaultCtx.addListener(key, listener);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregisters a previously registered listener for the key.
|
||||
*
|
||||
* @param key the key
|
||||
* @param listener the listener to remove
|
||||
* @param <T> the type
|
||||
* {@inheritDoc}
|
||||
*
|
||||
* Delegates to the default context instance.
|
||||
*/
|
||||
@Override
|
||||
public <T> void removeListener(Key<T> key, Listener<T> listener) {
|
||||
var list = listeners.get(key);
|
||||
if (list != null) {
|
||||
list.removeIf(ref -> {
|
||||
var l = ref.get();
|
||||
return l == null || l.equals(listener);
|
||||
});
|
||||
if (list.isEmpty()) {
|
||||
listeners.remove(key);
|
||||
}
|
||||
}
|
||||
defaultCtx.removeListener(key, listener);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
207
src/main/java/conflux/CtxInstance.java
Normal file
207
src/main/java/conflux/CtxInstance.java
Normal file
@@ -0,0 +1,207 @@
|
||||
/**
|
||||
* Copyright (C) 2025, Leo Galambos
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. All advertising materials mentioning features or use of this software must
|
||||
* display the following acknowledgement:
|
||||
* This product includes software developed by the Egothor project.
|
||||
*
|
||||
* 4. Neither the name of the copyright holder nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package conflux;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
/**
|
||||
* A globally accessible, type-safe context for sharing data between independent
|
||||
* parts of an application. Supports listeners that are notified whenever a
|
||||
* registered key's value changes.
|
||||
*
|
||||
* This implementation guarantees:
|
||||
* <ul>
|
||||
* <li>Strongly typed keys using {@link Key}</li>
|
||||
* <li>One consistent type per key name</li>
|
||||
* <li>Support for weak-referenced listeners to avoid memory leaks</li>
|
||||
* <li>Thread-safe operations</li>
|
||||
* </ul>
|
||||
*
|
||||
* @author Leo Galambos
|
||||
*/
|
||||
public final class CtxInstance implements CtxInterface {
|
||||
|
||||
private final Map<Key<?>, Object> values = new ConcurrentHashMap<>();
|
||||
private final Map<String, Class<?>> keyTypes = new ConcurrentHashMap<>();
|
||||
private final Map<Key<?>, List<WeakReference<Listener<?>>>> listeners = new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
* Protected no-argument constructor added to satisfy code analysis tools.
|
||||
* <p>
|
||||
* This constructor should not be used directly. It exists solely to comply with
|
||||
* static analysis requirements (e.g., PMD) expecting at least one constructor.
|
||||
*/
|
||||
protected CtxInstance() {
|
||||
// Intentionally left blank
|
||||
}
|
||||
|
||||
/**
|
||||
* Stores or updates the value for a given key. If the key is used for the first
|
||||
* time, its type is recorded. If the key has been used before with a different
|
||||
* type, an exception will be thrown.
|
||||
*
|
||||
* After setting the value, any registered listeners for that key will be
|
||||
* notified (if the value was modified).
|
||||
*
|
||||
* @param key the key
|
||||
* @param value the value to store
|
||||
* @param <T> the type of the value
|
||||
* @throws IllegalStateException if the key is reused with a different type
|
||||
*/
|
||||
@Override
|
||||
public <T> void put(Key<T> key, T value) {
|
||||
keyTypes.compute(key.name(), (k, existingType) -> {
|
||||
if (existingType == null) {
|
||||
return key.type();
|
||||
} else {
|
||||
if (!existingType.equals(key.type())) {
|
||||
throw new IllegalStateException(
|
||||
"Key '" + key.name() + "' already associated with type " + existingType.getName());
|
||||
}
|
||||
return existingType;
|
||||
}
|
||||
});
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
T previous = (T) values.put(key, value);
|
||||
|
||||
if (Objects.deepEquals(value, previous)) {
|
||||
// no change
|
||||
return;
|
||||
}
|
||||
|
||||
// notify listeners
|
||||
List<WeakReference<Listener<?>>> list = listeners.get(key);
|
||||
if (list != null) {
|
||||
for (WeakReference<Listener<?>> ref : list) {
|
||||
Listener<?> listener = ref.get();
|
||||
if (listener != null) {
|
||||
@SuppressWarnings("unchecked")
|
||||
Listener<T> typedListener = (Listener<T>) listener;
|
||||
typedListener.valueChanged(value);
|
||||
}
|
||||
}
|
||||
// clean up dead weak references
|
||||
list.removeIf(ref -> ref.get() == null);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the value for the given key, or {@code null} if not set.
|
||||
*
|
||||
* @param key the key
|
||||
* @param <T> the type
|
||||
* @return the stored value or null
|
||||
*/
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> T get(Key<T> key) {
|
||||
return (T) values.get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a value exists for the given key.
|
||||
*
|
||||
* @param key the key
|
||||
* @return true if a value is present
|
||||
*/
|
||||
@Override
|
||||
public boolean contains(Key<?> key) {
|
||||
return values.containsKey(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the value and any listeners for the given key.
|
||||
*
|
||||
* @param key the key
|
||||
* @return the removed value, or null if absent
|
||||
*/
|
||||
@Override
|
||||
public Object remove(Key<?> key) {
|
||||
keyTypes.remove(key.name());
|
||||
Object removed = values.remove(key);
|
||||
listeners.remove(key);
|
||||
return removed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears all keys and listeners from the context.
|
||||
*/
|
||||
@Override
|
||||
public void clear() {
|
||||
values.clear();
|
||||
keyTypes.clear();
|
||||
listeners.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a listener to be notified whenever the value for the key changes.
|
||||
* The listener is weakly referenced to avoid preventing its garbage collection.
|
||||
*
|
||||
* @param key the key
|
||||
* @param listener the listener
|
||||
* @param <T> the type
|
||||
*/
|
||||
@Override
|
||||
public <T> void addListener(Key<T> key, Listener<T> listener) {
|
||||
listeners.computeIfAbsent(key, k -> new CopyOnWriteArrayList<>()).add(new WeakReference<>(listener));
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregisters a previously registered listener for the key.
|
||||
*
|
||||
* @param key the key
|
||||
* @param listener the listener to remove
|
||||
* @param <T> the type
|
||||
*/
|
||||
@Override
|
||||
public <T> void removeListener(Key<T> key, Listener<T> listener) {
|
||||
List<WeakReference<Listener<?>>> list = listeners.get(key);
|
||||
if (list != null) {
|
||||
list.removeIf(ref -> {
|
||||
Listener<?> l = ref.get();
|
||||
return l == null || l.equals(listener);
|
||||
});
|
||||
if (list.isEmpty()) {
|
||||
listeners.remove(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
121
src/main/java/conflux/CtxInterface.java
Normal file
121
src/main/java/conflux/CtxInterface.java
Normal file
@@ -0,0 +1,121 @@
|
||||
/**
|
||||
* Copyright (C) 2025, Leo Galambos
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. All advertising materials mentioning features or use of this software must
|
||||
* display the following acknowledgement:
|
||||
* This product includes software developed by the Egothor project.
|
||||
*
|
||||
* 4. Neither the name of the copyright holder nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package conflux;
|
||||
|
||||
/**
|
||||
* A type-safe, thread-safe context interface for storing and retrieving
|
||||
* key-value pairs.
|
||||
*
|
||||
* Implementations of this interface support:
|
||||
* <ul>
|
||||
* <li>Recording type-safe associations between keys and values</li>
|
||||
* <li>Automatic listener notification when a value changes</li>
|
||||
* <li>Context isolation (e.g., per name or use case)</li>
|
||||
* </ul>
|
||||
*
|
||||
* This interface is sealed to only permit known, safe implementations.
|
||||
*
|
||||
* @see Key
|
||||
* @see Listener
|
||||
*
|
||||
* @author Leo Galambos
|
||||
*/
|
||||
public sealed interface CtxInterface permits Ctx, CtxInstance {
|
||||
|
||||
/**
|
||||
* Stores or updates the value for a given key. If the key is used for the first
|
||||
* time, its type is recorded. If the key has been used before with a different
|
||||
* type, an exception will be thrown.
|
||||
*
|
||||
* After setting the value, any registered listeners for that key will be
|
||||
* notified (if the value was modified).
|
||||
*
|
||||
* @param key the key
|
||||
* @param value the value to store
|
||||
* @param <T> the type of the value
|
||||
* @throws IllegalStateException if the key is reused with a different type
|
||||
*/
|
||||
<T> void put(Key<T> key, T value);
|
||||
|
||||
/**
|
||||
* Retrieves the value for the given key, or {@code null} if not set.
|
||||
*
|
||||
* @param key the key
|
||||
* @param <T> the type
|
||||
* @return the stored value or null
|
||||
*/
|
||||
<T> T get(Key<T> key);
|
||||
|
||||
/**
|
||||
* Checks if a value exists for the given key.
|
||||
*
|
||||
* @param key the key
|
||||
* @return true if a value is present
|
||||
*/
|
||||
boolean contains(Key<?> key);
|
||||
|
||||
/**
|
||||
* Removes the value and any listeners for the given key.
|
||||
*
|
||||
* @param key the key
|
||||
* @return the removed value, or null if absent
|
||||
*/
|
||||
Object remove(Key<?> key);
|
||||
|
||||
/**
|
||||
* Clears all keys and listeners from the context.
|
||||
*/
|
||||
void clear();
|
||||
|
||||
/**
|
||||
* Registers a listener to be notified whenever the value for the key changes.
|
||||
* The listener is weakly referenced to avoid preventing its garbage collection.
|
||||
*
|
||||
* @param key the key
|
||||
* @param listener the listener
|
||||
* @param <T> the type
|
||||
*/
|
||||
<T> void addListener(Key<T> key, Listener<T> listener);
|
||||
|
||||
/**
|
||||
* Unregisters a previously registered listener for the key.
|
||||
*
|
||||
* @param key the key
|
||||
* @param listener the listener to remove
|
||||
* @param <T> the type
|
||||
*/
|
||||
<T> void removeListener(Key<T> key, Listener<T> listener);
|
||||
|
||||
}
|
||||
@@ -42,19 +42,19 @@ package conflux;
|
||||
* @author Leo Galambos
|
||||
*/
|
||||
public final class Key<T> { // NOPMD by Leo Galambos on 7/3/25, 10:29 PM
|
||||
private final String name;
|
||||
private final Class<T> type;
|
||||
private final String nameField;
|
||||
private final Class<T> typeField;
|
||||
|
||||
private Key(String name, Class<T> type) {
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
this.nameField = name;
|
||||
this.typeField = type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new typed key.
|
||||
*
|
||||
* @param name a unique key name
|
||||
* @param type the class of the key's value type
|
||||
* @param name a unique key nameField
|
||||
* @param type the class of the key's value typeField
|
||||
* @param <T> the type
|
||||
* @return a new {@code Key}
|
||||
*/
|
||||
@@ -68,7 +68,7 @@ public final class Key<T> { // NOPMD by Leo Galambos on 7/3/25, 10:29 PM
|
||||
* @return the name
|
||||
*/
|
||||
public String name() {
|
||||
return name;
|
||||
return nameField;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -77,21 +77,21 @@ public final class Key<T> { // NOPMD by Leo Galambos on 7/3/25, 10:29 PM
|
||||
* @return the type
|
||||
*/
|
||||
public Class<T> type() {
|
||||
return type;
|
||||
return typeField;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return name.hashCode();
|
||||
return nameField.hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
return obj instanceof Key<?> other && name.equals(other.name);
|
||||
return obj instanceof Key<?> other && nameField.equals(other.nameField);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Key[" + name + ", " + type.getSimpleName() + "]";
|
||||
return "Key[" + nameField + ", " + typeField.getSimpleName() + "]";
|
||||
}
|
||||
}
|
||||
37
src/main/java/conflux/package-info.java
Normal file
37
src/main/java/conflux/package-info.java
Normal file
@@ -0,0 +1,37 @@
|
||||
/**
|
||||
* Provides a lightweight, type-safe, application-wide context mechanism for
|
||||
* sharing strongly typed data among otherwise decoupled classes.
|
||||
*
|
||||
* The {@link conflux.Ctx} enum serves as a central, generic context storage,
|
||||
* supporting key-based put/get operations. Each {@link conflux.Key} instance
|
||||
* defines a unique, strongly typed entry in the context, ensuring type
|
||||
* consistency and preventing misuse.
|
||||
*
|
||||
* The {@link conflux.Listener} interface allows clients to observe value
|
||||
* changes for specific keys. Listeners are weakly referenced to prevent memory
|
||||
* leaks.
|
||||
*
|
||||
* Typical usage:
|
||||
* <ul>
|
||||
* <li>Define {@link conflux.Key} instances with explicit types</li>
|
||||
* <li>Store values via {@code Ctx.INSTANCE.put(key, value)}</li>
|
||||
* <li>Retrieve values via {@code Ctx.INSTANCE.get(key)}</li>
|
||||
* <li>Register listeners with {@code Ctx.INSTANCE.addListener()}</li>
|
||||
* </ul>
|
||||
*
|
||||
* Values and listeners can be removed individually, or the entire context can
|
||||
* be cleared.
|
||||
*
|
||||
* Best practices:
|
||||
* <ul>
|
||||
* <li>Define keys as constants to maintain consistency and avoid
|
||||
* collisions</li>
|
||||
* <li>Remove listeners when no longer needed</li>
|
||||
* <li>Use unique key names to avoid accidental type conflicts</li>
|
||||
* </ul>
|
||||
*
|
||||
* @see conflux.Ctx
|
||||
* @see conflux.Key
|
||||
* @see conflux.Listener
|
||||
*/
|
||||
package conflux;
|
||||
@@ -36,6 +36,7 @@ package conflux;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@@ -159,4 +160,50 @@ public class CtxTest {
|
||||
assertTrue(flag[0] || !flag[0]);
|
||||
System.out.println("...ok");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testMultipleContextsIsolation() {
|
||||
System.out.println("testMultipleContextsIsolation");
|
||||
var ctxA = Ctx.INSTANCE.getContext("A");
|
||||
var ctxB = Ctx.INSTANCE.getContext("B");
|
||||
|
||||
ctxA.put(intKey, 10);
|
||||
ctxB.put(intKey, 20);
|
||||
|
||||
assertEquals(10, ctxA.get(intKey));
|
||||
assertEquals(20, ctxB.get(intKey));
|
||||
assertNull(Ctx.INSTANCE.get(intKey)); // Default context is separate
|
||||
|
||||
ctxA.clear();
|
||||
assertFalse(ctxA.contains(intKey));
|
||||
assertEquals(20, ctxB.get(intKey));
|
||||
|
||||
System.out.println("...ok");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testListenerNotificationInSeparateContexts() {
|
||||
System.out.println("testListenerNotificationInSeparateContexts");
|
||||
var ctxA = Ctx.INSTANCE.getContext("A");
|
||||
var ctxB = Ctx.INSTANCE.getContext("B");
|
||||
|
||||
var resultA = new StringBuilder();
|
||||
var resultB = new StringBuilder();
|
||||
|
||||
Listener<Integer> listenerA = v -> resultA.append(v);
|
||||
Listener<Integer> listenerB = v -> resultB.append(v);
|
||||
|
||||
ctxA.addListener(intKey, listenerA);
|
||||
ctxB.addListener(intKey, listenerB);
|
||||
|
||||
ctxA.put(intKey, 1);
|
||||
ctxB.put(intKey, 2);
|
||||
ctxA.put(intKey, 3);
|
||||
ctxB.put(intKey, 4);
|
||||
|
||||
assertEquals("13", resultA.toString());
|
||||
assertEquals("24", resultB.toString());
|
||||
|
||||
System.out.println("...ok");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user