├── .clj-kondo └── config.edn ├── .github └── workflows │ └── lint-and-test.yml ├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── bin ├── cljdoc-local └── cljdoc-local-publish ├── build.edn ├── deps.edn ├── img ├── desiderata-logo-1024.png ├── desiderata-logo-150.png ├── desiderata-logo-300.png └── desiderata-logo.svg ├── resources └── clj-kondo.exports │ └── systems.thoughtfull │ └── desiderata │ ├── config.edn │ └── systems │ └── thoughtfull │ └── desiderata.clj ├── src └── systems │ └── thoughtfull │ └── desiderata.clj └── test └── systems └── thoughtfull └── desiderata_test.clj /.clj-kondo/config.edn: -------------------------------------------------------------------------------- 1 | {:config-paths ["../resources/clj-kondo.exports/systems.thoughtfull/desiderata"] 2 | :linters 3 | {:unsorted-required-namespaces {:level :warning}}} 4 | -------------------------------------------------------------------------------- /.github/workflows/lint-and-test.yml: -------------------------------------------------------------------------------- 1 | name: Lint and test 2 | on: [push] 3 | jobs: 4 | lint: 5 | name: Lint 6 | runs-on: ubuntu-latest 7 | steps: 8 | - name: Checkout 9 | uses: actions/checkout@v4 10 | - name: Setup Java 11 | uses: actions/setup-java@v4 12 | with: 13 | distribution: adopt 14 | java-version: 17 15 | - name: Setup 16 | uses: DeLaGuardo/setup-clojure@13.2 17 | with: 18 | cli: 1.12.0.1479 19 | clj-kondo: 2025.02.20 20 | - name: Cache dependencies 21 | uses: actions/cache@v3 22 | with: 23 | path: | 24 | ~/.m2/repository 25 | ~/.gitlibs 26 | ~/.deps.clj 27 | key: cljdeps-${{ hashFiles('deps.edn') }} 28 | restore-keys: cljdeps- 29 | - name: Lint 30 | run: | 31 | (echo '```'; 32 | clj-kondo --lint src --lint test --config '{:ignore [:deprecated-var :deprecated-namespace]}'; 33 | echo '```') >>$GITHUB_STEP_SUMMARY 34 | test: 35 | name: Test 36 | runs-on: ubuntu-latest 37 | steps: 38 | - name: Checkout 39 | uses: actions/checkout@v4 40 | - name: Setup Java 41 | uses: actions/setup-java@v4 42 | with: 43 | distribution: adopt 44 | java-version: 17 45 | - name: Setup 46 | uses: DeLaGuardo/setup-clojure@13.2 47 | with: 48 | cli: 1.12.0.1479 49 | clj-kondo: 2025.02.20 50 | - name: Cache dependencies 51 | uses: actions/cache@v3 52 | with: 53 | path: | 54 | ~/.m2/repository 55 | ~/.gitlibs 56 | ~/.deps.clj 57 | key: cljdeps-${{ hashFiles('deps.edn') }} 58 | restore-keys: cljdeps- 59 | - name: Test 60 | run: | 61 | (echo '```'; 62 | clojure -X:test; 63 | echo '```') >>$GITHUB_STEP_SUMMARY 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.clj-kondo/.cache/ 2 | /.cljdoc-preview/ 3 | /.cpcache/ 4 | /.nrepl-port 5 | /target/ 6 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to this project will be documented in this file. 4 | 5 | The format is based on [Keep a Changelog 1.1.0](https://keepachangelog.com/en/1.1.0/), and this 6 | project adheres to [Semantic Versioning 2.0.0](https://semver.org/spec/v2.0.0.html). 7 | 8 | ## [Unreleased] 9 | 10 | ### Added 11 | 12 | ### Changed 13 | 14 | ### Deprecated 15 | 16 | ### Removed 17 | 18 | ### Fixed 19 | 20 | ### Security 21 | 22 | ## [2.1.2] - 2025-05-07 23 | 24 | ### Changed 25 | 26 | - More documentation updates 27 | 28 | ## [2.1.1] - 2025-05-07 29 | 30 | ### Changed 31 | 32 | - Documentation updates 33 | 34 | ## [2.1.0] - 2025-05-07 35 | 36 | ### Added 37 | 38 | - Convey metadata from `defrecord` name symbol to factory functions. 39 | 40 | ## [2.0.0] - 2025-04-25 41 | 42 | ### Changed 43 | 44 | - ***BREAKING*** Convey bindings from creation of thread factory, not creation of thread. 45 | - ***BREAKING*** Thread factory `:convey-bindings?` default changed to `false`. 46 | - ***BREAKING*** Always convey thread bindings to `uncaught-exception-handler-fn`. 47 | - ***BREAKING*** Removed `IBindingConveyingThreadFactory` protocol. 48 | 49 | ### Fixed 50 | 51 | - Reset thread bindings in thread factory, instead of pushing bindings. 52 | 53 | ## [1.1.2] - 2025-04-16 54 | 55 | ### Fixed 56 | 57 | - Update `defrecord` linter for compile error fix. 58 | 59 | ## [1.1.1] - 2025-04-15 60 | 61 | ### Fixed 62 | 63 | - Fixed compile error for `defrecord` with only name and fields. 64 | 65 | ## [1.1.0] - 2025-03-31 66 | 67 | ### Added 68 | 69 | - Functions for adapting a two argument function into a `Thread.UncaughtExceptionHandler` and 70 | setting the default handler. 71 | - A `java.util.concurrent.ThreadFactory` constructor including binding conveyance. 72 | 73 | ## [1.0.1] - 2025-03-23 74 | 75 | ### Added 76 | 77 | - Correct linting of field names in initializer 78 | 79 | ## [1.0.0] - 2025-03-23 80 | 81 | ### Added 82 | 83 | - Initial release with defrecord macro. 84 | 85 | [unreleased]: https://github.com/thoughtfull-clojure/desiderata/compare/v2.1.2...main 86 | [2.1.2]: https://github.com/thoughtfull-clojure/desiderata/compare/v2.1.1...v2.1.2 87 | [2.1.1]: https://github.com/thoughtfull-clojure/desiderata/compare/v2.1.0...v2.1.1 88 | [2.1.0]: https://github.com/thoughtfull-clojure/desiderata/compare/v2.0.0...v2.1.0 89 | [2.0.0]: https://github.com/thoughtfull-clojure/desiderata/compare/v1.1.2...v2.0.0 90 | [1.1.2]: https://github.com/thoughtfull-clojure/desiderata/compare/v1.1.1...v1.1.2 91 | [1.1.1]: https://github.com/thoughtfull-clojure/desiderata/compare/v1.1.0...v1.1.1 92 | [1.1.0]: https://github.com/thoughtfull-clojure/desiderata/compare/v1.0.1...v1.1.0 93 | [1.0.1]: https://github.com/thoughtfull-clojure/desiderata/compare/v1.0.0...v1.0.1 94 | [1.0.0]: https://github.com/thoughtfull-clojure/desiderata/releases/tag/v1.0.0 95 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Mozilla Public License Version 2.0 2 | ================================== 3 | 4 | 1. Definitions 5 | -------------- 6 | 7 | 1.1. "Contributor" 8 | means each individual or legal entity that creates, contributes to 9 | the creation of, or owns Covered Software. 10 | 11 | 1.2. "Contributor Version" 12 | means the combination of the Contributions of others (if any) used 13 | by a Contributor and that particular Contributor's Contribution. 14 | 15 | 1.3. "Contribution" 16 | means Covered Software of a particular Contributor. 17 | 18 | 1.4. "Covered Software" 19 | means Source Code Form to which the initial Contributor has attached 20 | the notice in Exhibit A, the Executable Form of such Source Code 21 | Form, and Modifications of such Source Code Form, in each case 22 | including portions thereof. 23 | 24 | 1.5. "Incompatible With Secondary Licenses" 25 | means 26 | 27 | (a) that the initial Contributor has attached the notice described 28 | in Exhibit B to the Covered Software; or 29 | 30 | (b) that the Covered Software was made available under the terms of 31 | version 1.1 or earlier of the License, but not also under the 32 | terms of a Secondary License. 33 | 34 | 1.6. "Executable Form" 35 | means any form of the work other than Source Code Form. 36 | 37 | 1.7. "Larger Work" 38 | means a work that combines Covered Software with other material, in 39 | a separate file or files, that is not Covered Software. 40 | 41 | 1.8. "License" 42 | means this document. 43 | 44 | 1.9. "Licensable" 45 | means having the right to grant, to the maximum extent possible, 46 | whether at the time of the initial grant or subsequently, any and 47 | all of the rights conveyed by this License. 48 | 49 | 1.10. "Modifications" 50 | means any of the following: 51 | 52 | (a) any file in Source Code Form that results from an addition to, 53 | deletion from, or modification of the contents of Covered 54 | Software; or 55 | 56 | (b) any new file in Source Code Form that contains any Covered 57 | Software. 58 | 59 | 1.11. "Patent Claims" of a Contributor 60 | means any patent claim(s), including without limitation, method, 61 | process, and apparatus claims, in any patent Licensable by such 62 | Contributor that would be infringed, but for the grant of the 63 | License, by the making, using, selling, offering for sale, having 64 | made, import, or transfer of either its Contributions or its 65 | Contributor Version. 66 | 67 | 1.12. "Secondary License" 68 | means either the GNU General Public License, Version 2.0, the GNU 69 | Lesser General Public License, Version 2.1, the GNU Affero General 70 | Public License, Version 3.0, or any later versions of those 71 | licenses. 72 | 73 | 1.13. "Source Code Form" 74 | means the form of the work preferred for making modifications. 75 | 76 | 1.14. "You" (or "Your") 77 | means an individual or a legal entity exercising rights under this 78 | License. For legal entities, "You" includes any entity that 79 | controls, is controlled by, or is under common control with You. For 80 | purposes of this definition, "control" means (a) the power, direct 81 | or indirect, to cause the direction or management of such entity, 82 | whether by contract or otherwise, or (b) ownership of more than 83 | fifty percent (50%) of the outstanding shares or beneficial 84 | ownership of such entity. 85 | 86 | 2. License Grants and Conditions 87 | -------------------------------- 88 | 89 | 2.1. Grants 90 | 91 | Each Contributor hereby grants You a world-wide, royalty-free, 92 | non-exclusive license: 93 | 94 | (a) under intellectual property rights (other than patent or trademark) 95 | Licensable by such Contributor to use, reproduce, make available, 96 | modify, display, perform, distribute, and otherwise exploit its 97 | Contributions, either on an unmodified basis, with Modifications, or 98 | as part of a Larger Work; and 99 | 100 | (b) under Patent Claims of such Contributor to make, use, sell, offer 101 | for sale, have made, import, and otherwise transfer either its 102 | Contributions or its Contributor Version. 103 | 104 | 2.2. Effective Date 105 | 106 | The licenses granted in Section 2.1 with respect to any Contribution 107 | become effective for each Contribution on the date the Contributor first 108 | distributes such Contribution. 109 | 110 | 2.3. Limitations on Grant Scope 111 | 112 | The licenses granted in this Section 2 are the only rights granted under 113 | this License. No additional rights or licenses will be implied from the 114 | distribution or licensing of Covered Software under this License. 115 | Notwithstanding Section 2.1(b) above, no patent license is granted by a 116 | Contributor: 117 | 118 | (a) for any code that a Contributor has removed from Covered Software; 119 | or 120 | 121 | (b) for infringements caused by: (i) Your and any other third party's 122 | modifications of Covered Software, or (ii) the combination of its 123 | Contributions with other software (except as part of its Contributor 124 | Version); or 125 | 126 | (c) under Patent Claims infringed by Covered Software in the absence of 127 | its Contributions. 128 | 129 | This License does not grant any rights in the trademarks, service marks, 130 | or logos of any Contributor (except as may be necessary to comply with 131 | the notice requirements in Section 3.4). 132 | 133 | 2.4. Subsequent Licenses 134 | 135 | No Contributor makes additional grants as a result of Your choice to 136 | distribute the Covered Software under a subsequent version of this 137 | License (see Section 10.2) or under the terms of a Secondary License (if 138 | permitted under the terms of Section 3.3). 139 | 140 | 2.5. Representation 141 | 142 | Each Contributor represents that the Contributor believes its 143 | Contributions are its original creation(s) or it has sufficient rights 144 | to grant the rights to its Contributions conveyed by this License. 145 | 146 | 2.6. Fair Use 147 | 148 | This License is not intended to limit any rights You have under 149 | applicable copyright doctrines of fair use, fair dealing, or other 150 | equivalents. 151 | 152 | 2.7. Conditions 153 | 154 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted 155 | in Section 2.1. 156 | 157 | 3. Responsibilities 158 | ------------------- 159 | 160 | 3.1. Distribution of Source Form 161 | 162 | All distribution of Covered Software in Source Code Form, including any 163 | Modifications that You create or to which You contribute, must be under 164 | the terms of this License. You must inform recipients that the Source 165 | Code Form of the Covered Software is governed by the terms of this 166 | License, and how they can obtain a copy of this License. You may not 167 | attempt to alter or restrict the recipients' rights in the Source Code 168 | Form. 169 | 170 | 3.2. Distribution of Executable Form 171 | 172 | If You distribute Covered Software in Executable Form then: 173 | 174 | (a) such Covered Software must also be made available in Source Code 175 | Form, as described in Section 3.1, and You must inform recipients of 176 | the Executable Form how they can obtain a copy of such Source Code 177 | Form by reasonable means in a timely manner, at a charge no more 178 | than the cost of distribution to the recipient; and 179 | 180 | (b) You may distribute such Executable Form under the terms of this 181 | License, or sublicense it under different terms, provided that the 182 | license for the Executable Form does not attempt to limit or alter 183 | the recipients' rights in the Source Code Form under this License. 184 | 185 | 3.3. Distribution of a Larger Work 186 | 187 | You may create and distribute a Larger Work under terms of Your choice, 188 | provided that You also comply with the requirements of this License for 189 | the Covered Software. If the Larger Work is a combination of Covered 190 | Software with a work governed by one or more Secondary Licenses, and the 191 | Covered Software is not Incompatible With Secondary Licenses, this 192 | License permits You to additionally distribute such Covered Software 193 | under the terms of such Secondary License(s), so that the recipient of 194 | the Larger Work may, at their option, further distribute the Covered 195 | Software under the terms of either this License or such Secondary 196 | License(s). 197 | 198 | 3.4. Notices 199 | 200 | You may not remove or alter the substance of any license notices 201 | (including copyright notices, patent notices, disclaimers of warranty, 202 | or limitations of liability) contained within the Source Code Form of 203 | the Covered Software, except that You may alter any license notices to 204 | the extent required to remedy known factual inaccuracies. 205 | 206 | 3.5. Application of Additional Terms 207 | 208 | You may choose to offer, and to charge a fee for, warranty, support, 209 | indemnity or liability obligations to one or more recipients of Covered 210 | Software. However, You may do so only on Your own behalf, and not on 211 | behalf of any Contributor. You must make it absolutely clear that any 212 | such warranty, support, indemnity, or liability obligation is offered by 213 | You alone, and You hereby agree to indemnify every Contributor for any 214 | liability incurred by such Contributor as a result of warranty, support, 215 | indemnity or liability terms You offer. You may include additional 216 | disclaimers of warranty and limitations of liability specific to any 217 | jurisdiction. 218 | 219 | 4. Inability to Comply Due to Statute or Regulation 220 | --------------------------------------------------- 221 | 222 | If it is impossible for You to comply with any of the terms of this 223 | License with respect to some or all of the Covered Software due to 224 | statute, judicial order, or regulation then You must: (a) comply with 225 | the terms of this License to the maximum extent possible; and (b) 226 | describe the limitations and the code they affect. Such description must 227 | be placed in a text file included with all distributions of the Covered 228 | Software under this License. Except to the extent prohibited by statute 229 | or regulation, such description must be sufficiently detailed for a 230 | recipient of ordinary skill to be able to understand it. 231 | 232 | 5. Termination 233 | -------------- 234 | 235 | 5.1. The rights granted under this License will terminate automatically 236 | if You fail to comply with any of its terms. However, if You become 237 | compliant, then the rights granted under this License from a particular 238 | Contributor are reinstated (a) provisionally, unless and until such 239 | Contributor explicitly and finally terminates Your grants, and (b) on an 240 | ongoing basis, if such Contributor fails to notify You of the 241 | non-compliance by some reasonable means prior to 60 days after You have 242 | come back into compliance. Moreover, Your grants from a particular 243 | Contributor are reinstated on an ongoing basis if such Contributor 244 | notifies You of the non-compliance by some reasonable means, this is the 245 | first time You have received notice of non-compliance with this License 246 | from such Contributor, and You become compliant prior to 30 days after 247 | Your receipt of the notice. 248 | 249 | 5.2. If You initiate litigation against any entity by asserting a patent 250 | infringement claim (excluding declaratory judgment actions, 251 | counter-claims, and cross-claims) alleging that a Contributor Version 252 | directly or indirectly infringes any patent, then the rights granted to 253 | You by any and all Contributors for the Covered Software under Section 254 | 2.1 of this License shall terminate. 255 | 256 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all 257 | end user license agreements (excluding distributors and resellers) which 258 | have been validly granted by You or Your distributors under this License 259 | prior to termination shall survive termination. 260 | 261 | ************************************************************************ 262 | * * 263 | * 6. Disclaimer of Warranty * 264 | * ------------------------- * 265 | * * 266 | * Covered Software is provided under this License on an "as is" * 267 | * basis, without warranty of any kind, either expressed, implied, or * 268 | * statutory, including, without limitation, warranties that the * 269 | * Covered Software is free of defects, merchantable, fit for a * 270 | * particular purpose or non-infringing. The entire risk as to the * 271 | * quality and performance of the Covered Software is with You. * 272 | * Should any Covered Software prove defective in any respect, You * 273 | * (not any Contributor) assume the cost of any necessary servicing, * 274 | * repair, or correction. This disclaimer of warranty constitutes an * 275 | * essential part of this License. No use of any Covered Software is * 276 | * authorized under this License except under this disclaimer. * 277 | * * 278 | ************************************************************************ 279 | 280 | ************************************************************************ 281 | * * 282 | * 7. Limitation of Liability * 283 | * -------------------------- * 284 | * * 285 | * Under no circumstances and under no legal theory, whether tort * 286 | * (including negligence), contract, or otherwise, shall any * 287 | * Contributor, or anyone who distributes Covered Software as * 288 | * permitted above, be liable to You for any direct, indirect, * 289 | * special, incidental, or consequential damages of any character * 290 | * including, without limitation, damages for lost profits, loss of * 291 | * goodwill, work stoppage, computer failure or malfunction, or any * 292 | * and all other commercial damages or losses, even if such party * 293 | * shall have been informed of the possibility of such damages. This * 294 | * limitation of liability shall not apply to liability for death or * 295 | * personal injury resulting from such party's negligence to the * 296 | * extent applicable law prohibits such limitation. Some * 297 | * jurisdictions do not allow the exclusion or limitation of * 298 | * incidental or consequential damages, so this exclusion and * 299 | * limitation may not apply to You. * 300 | * * 301 | ************************************************************************ 302 | 303 | 8. Litigation 304 | ------------- 305 | 306 | Any litigation relating to this License may be brought only in the 307 | courts of a jurisdiction where the defendant maintains its principal 308 | place of business and such litigation shall be governed by laws of that 309 | jurisdiction, without reference to its conflict-of-law provisions. 310 | Nothing in this Section shall prevent a party's ability to bring 311 | cross-claims or counter-claims. 312 | 313 | 9. Miscellaneous 314 | ---------------- 315 | 316 | This License represents the complete agreement concerning the subject 317 | matter hereof. If any provision of this License is held to be 318 | unenforceable, such provision shall be reformed only to the extent 319 | necessary to make it enforceable. Any law or regulation which provides 320 | that the language of a contract shall be construed against the drafter 321 | shall not be used to construe this License against a Contributor. 322 | 323 | 10. Versions of the License 324 | --------------------------- 325 | 326 | 10.1. New Versions 327 | 328 | Mozilla Foundation is the license steward. Except as provided in Section 329 | 10.3, no one other than the license steward has the right to modify or 330 | publish new versions of this License. Each version will be given a 331 | distinguishing version number. 332 | 333 | 10.2. Effect of New Versions 334 | 335 | You may distribute the Covered Software under the terms of the version 336 | of the License under which You originally received the Covered Software, 337 | or under the terms of any subsequent version published by the license 338 | steward. 339 | 340 | 10.3. Modified Versions 341 | 342 | If you create software not governed by this License, and you want to 343 | create a new license for such software, you may create and use a 344 | modified version of this License if you rename the license and remove 345 | any references to the name of the license steward (except to note that 346 | such modified license differs from this License). 347 | 348 | 10.4. Distributing Source Code Form that is Incompatible With Secondary 349 | Licenses 350 | 351 | If You choose to distribute Source Code Form that is Incompatible With 352 | Secondary Licenses under the terms of this version of the License, the 353 | notice described in Exhibit B of this License must be attached. 354 | 355 | Exhibit A - Source Code Form License Notice 356 | ------------------------------------------- 357 | 358 | This Source Code Form is subject to the terms of the Mozilla Public 359 | License, v. 2.0. If a copy of the MPL was not distributed with this 360 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 361 | 362 | If it is not possible or desirable to put the notice in a particular 363 | file, then You may include the notice in a location (such as a LICENSE 364 | file in a relevant directory) where a recipient would be likely to look 365 | for such a notice. 366 | 367 | You may add additional accurate notices of copyright ownership. 368 | 369 | Exhibit B - "Incompatible With Secondary Licenses" Notice 370 | --------------------------------------------------------- 371 | 372 | This Source Code Form is "Incompatible With Secondary Licenses", as 373 | defined by the Mozilla Public License, v. 2.0. 374 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # desiderata 2 | 3 | 4 | 5 | [![Lint and test](https://github.com/thoughtfull-clojure/desiderata/actions/workflows/lint-and-test.yml/badge.svg)](https://github.com/thoughtfull-clojure/desiderata/actions/workflows/lint-and-test.yml) [![clojars badge](https://badgen.net/badge/clojars/2.1.2/blue)](https://clojars.org/systems.thoughtfull/desiderata/versions/2.1.2) [![cljdoc badge](https://badgen.net/badge/cljdoc/2.1.2/blue)](https://cljdoc.org/d/systems.thoughtfull/desiderata/2.1.2/doc/readme) 6 | 7 | Things wanted or needed but missing from clojure.core. 8 | 9 | This is not just a collection of utility functions, but more significant features (and maybe some 10 | utility functions). Maybe it's best to just poke around in the documentation? Otherwise, I've 11 | attempted to provide details below. 12 | 13 | ## Baseline versions 14 | 15 | - JVM: 21 16 | - Clojure: 1.12.0 17 | 18 | ## defrecord 19 | 20 | You can use `systems.thoughtfull.desiderata/defrecord` as a drop-in replacement for 21 | `clojure.core/defrecord` and what you get are five new features. The first (minor) feature is the 22 | factory function (i.e. `map->...`) takes keyword arguments. 23 | 24 | ### Metadata propagation 25 | 26 | Metadata attached to the name symbol of `systems.thoughtfull.desiderata/defrecord` will get 27 | propagated to the factory functions. So, for example, if you deprecate the `defrecord`, then its 28 | factory functions will also be deprecated. 29 | 30 | ### Docstring 31 | 32 | `systems.thoughtfull.desiderata/defrecord` takes a docstring that it appends to the end of both the 33 | factory and positional factory functions. 34 | 35 | ```clojure 36 | user> (require '[systems.thoughtfull.desiderata :as desiderata]) 37 | nil 38 | 39 | user> (desiderata/defrecord Widget 40 | "A widget for frobbing gizmos. 41 | 42 | width and height are in metric." 43 | [width height]) 44 | user.Widget 45 | 46 | user> (doc map->Widget) 47 | ------------------------- 48 | user/map->Widget 49 | ([& {:keys [width height]}]) 50 | Factory function for class user.Widget, taking a map of keywords to field values. 51 | 52 | A widget for frobbing gizmos. 53 | 54 | width and height are in metric. 55 | nil 56 | 57 | user> (doc ->Widget) 58 | ------------------------- 59 | user/->Widget 60 | ([width height]) 61 | Positional factory function for class user.Widget. 62 | 63 | A widget for frobbing gizmos. 64 | 65 | width and height are in metric. 66 | nil 67 | ``` 68 | 69 | ### Defaults 70 | 71 | You can specify a map of default values for fields (and non-fields) with the 72 | `systems.thoughtfull.desiderata/defaults` option. Values given to the factory or positional factory 73 | functions will override defaults. 74 | 75 | ```clojure 76 | user> (desiderata/defrecord Gizmo 77 | [name] 78 | ::desiderata/defaults 79 | {:name "Gizmo" 80 | :color :blue}) 81 | user.Gizmo 82 | 83 | user> (->Gizmo "the Great") 84 | {:name "the Great", :color :blue} 85 | 86 | user> (map->Gizmo :texture :bumpy) 87 | {:name "Gizmo", :color :blue, :texture :bumpy} 88 | ``` 89 | 90 | ### Initializer 91 | 92 | A method with the same name as the defrecord is an initializer method called from both the factory 93 | and positional factory functions. The initializer runs after the defaults and arguments have merged 94 | and the factory function has executed. 95 | 96 | As a sanity check, the initializer must return an instance of the defrecord, otherwise an 97 | *IllegalStateException* is thrown. 98 | 99 | ```clojure 100 | user> (desiderata/defrecord Company 101 | [debt equity] 102 | (Company 103 | [this] 104 | (assoc this :gearing-ratio (/ debt equity)))) 105 | user.Company 106 | 107 | user> (->Company 100 1000) 108 | {:debt 100, :equity 1000, :gearing-ratio 1/10} 109 | ``` 110 | 111 | ## Thread.UncaughtExceptionHandler 112 | 113 | There are two convenience functions for working with *Thread.UncaughtExceptionHandler*. 114 | `uncaught-exception-handler` will adapt a two argument function to a 115 | *Thread.UncaughtExceptionHandler*. 116 | 117 | `set-default-uncaught-exception-handler-fn!` will set the default *Thread.UncaughtExceptionHandler* 118 | after adapting the given two argument function to a *Thread.UncaughtExceptionHandler*. The default 119 | handler is used if there is no handler defined for a *Thread* and its *ThreadGroup* (see 120 | *[Thread/setDefaultUncaughtExceptionHandler](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Thread.html#setDefaultUncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler))*). 121 | 122 | ## thread-factory 123 | 124 | `thread-factory` creates a *java.util.concurrent.ThreadFactory* for use particularly with executors. 125 | It passes options to *Thread*s as it creates them, however, a notable feature is it takes an 126 | `uncaught-exception-handler-fn` that it will set as a *Thread.UncaughtExceptionHandler* on each 127 | thread it creates. It adapts the `uncaught-exception-handler-fn` into a 128 | *Thread.UncaughtExceptionHandler* using `uncaught-exception-handler`, and conveys to the 129 | `uncaught-exception-handler-fn` the bindings established at the time the thread factory is created. 130 | 131 | Another notable and Clojure-specific feature is the *ThreadFactory* can convey to created threads 132 | the bindings established when the thread factory was created. You can enable this by passing 133 | `:convey-bindings?` true, but by default it is disabled. 134 | 135 | It will also take a `name` which is used as a prefix to the names of threads it creates. 136 | 137 | ## clj-kondo 138 | 139 | To import custom clj-kondo hooks for `systems.thoughtfull.desiderata/defrecord` use 140 | 141 | ``` 142 | clj-kondo --lint "$(clojure -Spath)" --copy-configs --skip-lint 143 | ``` 144 | 145 | ## License 146 | 147 | > Copyright © technosophist 148 | > 149 | > This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of 150 | > the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 151 | > 152 | > This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public 153 | > License, v. 2.0. 154 | -------------------------------------------------------------------------------- /bin/cljdoc-local: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | docker pull cljdoc/cljdoc 4 | mkdir -p .cljdoc-preview 5 | docker run --rm \ 6 | --publish 8000:8000 \ 7 | --volume "$HOME/.m2:/root/.m2" \ 8 | --volume ./.cljdoc-preview:/app/data \ 9 | --platform linux/amd64 \ 10 | cljdoc/cljdoc 11 | -------------------------------------------------------------------------------- /bin/cljdoc-local-publish: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | clj -T:build install 4 | docker run --rm \ 5 | --volume $(pwd):/repo-to-import \ 6 | --volume "$HOME/.m2:/root/.m2" \ 7 | --volume ./.cljdoc-preview:/app/data \ 8 | --platform linux/amd64 \ 9 | --entrypoint clojure \ 10 | cljdoc/cljdoc -Sforce -M:cli ingest \ 11 | --project systems.thoughtfull/desiderata \ 12 | --version 2.1.2 \ 13 | --git /repo-to-import \ 14 | --rev $(git rev-parse HEAD) 15 | -------------------------------------------------------------------------------- /build.edn: -------------------------------------------------------------------------------- 1 | {:description "Things wanted or needed but missing from clojure.core" 2 | :lib systems.thoughtfull/desiderata 3 | :version "2.1.2"} 4 | -------------------------------------------------------------------------------- /deps.edn: -------------------------------------------------------------------------------- 1 | {:aliases {:build {:deps {systems.thoughtfull/build {:mvn/version "0.5.0"}} 2 | :ns-default systems.thoughtfull.build} 3 | ;; clj -X:doc -> target/doc 4 | :doc {:extra-deps {codox/codox {:mvn/version "0.10.8"}} 5 | :exec-fn codox.main/generate-docs 6 | :exec-args {:doc-files ["README.md"] 7 | :metadata {:doc/format :markdown} 8 | :source-paths ["src"]}} 9 | :test {:exec-fn cognitect.test-runner.api/test 10 | :extra-deps {io.github.cognitect-labs/test-runner 11 | {:git/tag "v0.5.1" :git/sha "dfb30dd"}} 12 | :extra-paths ["test"]}} 13 | :deps {org.clojure/clojure {:mvn/version "1.12.0"}} 14 | :paths ["src" "resources"]} 15 | -------------------------------------------------------------------------------- /img/desiderata-logo-1024.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thoughtfull-clojure/desiderata/39a809e51d6737d8fcc7d7f921e1d4def715500f/img/desiderata-logo-1024.png -------------------------------------------------------------------------------- /img/desiderata-logo-150.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thoughtfull-clojure/desiderata/39a809e51d6737d8fcc7d7f921e1d4def715500f/img/desiderata-logo-150.png -------------------------------------------------------------------------------- /img/desiderata-logo-300.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thoughtfull-clojure/desiderata/39a809e51d6737d8fcc7d7f921e1d4def715500f/img/desiderata-logo-300.png -------------------------------------------------------------------------------- /img/desiderata-logo.svg: -------------------------------------------------------------------------------- 1 | 2 | 19 | 39 | 41 | 42 | 44 | image/svg+xml 45 | 47 | 48 | 49 | 50 | 52 | 55 | 58 | 62 | 66 | 70 | 74 | 78 | 82 | 83 | 84 | 102 | 109 | 127 | 133 | 134 | -------------------------------------------------------------------------------- /resources/clj-kondo.exports/systems.thoughtfull/desiderata/config.edn: -------------------------------------------------------------------------------- 1 | {:hooks 2 | {:macroexpand 3 | {systems.thoughtfull.desiderata/defrecord systems.thoughtfull.desiderata/defrecord}}} 4 | -------------------------------------------------------------------------------- /resources/clj-kondo.exports/systems.thoughtfull/desiderata/systems/thoughtfull/desiderata.clj: -------------------------------------------------------------------------------- 1 | (ns systems.thoughtfull.desiderata 2 | (:refer-clojure :exclude [defrecord])) 3 | 4 | (defn- parse-opts 5 | [opts+specs] 6 | (loop [[k v & s :as specs] opts+specs 7 | defaults nil 8 | opts []] 9 | (if (and (seq specs) (keyword? k)) 10 | (if (= (name k) "defaults") 11 | (recur s v opts) 12 | (recur s defaults (conj opts k v))) 13 | [defaults opts specs]))) 14 | 15 | (defn- parse-initializer 16 | [name specs] 17 | (loop [[s & ss :as specs] specs 18 | not-initializer []] 19 | (if (seq specs) 20 | (if (and (list? s) (= (first s) name)) 21 | [s (into not-initializer ss)] 22 | (recur ss (conj not-initializer s))) 23 | [nil not-initializer]))) 24 | 25 | (defmacro defrecord 26 | [name & args] 27 | (let [[docstring & args] (cond->> args (not (string? (first args))) (cons nil)) 28 | [fields & args] args 29 | [defaults opts specs] (parse-opts args) 30 | [initializer specs] (parse-initializer name specs) 31 | [init-params & init-body] (or (next initializer) `[[this#] this#]) 32 | pfactory (symbol (format "->%s" name)) 33 | factory (symbol (format "map->%s" name))] 34 | `(let [defaults# (fn [] ~defaults) 35 | record# (clojure.core/defrecord ~name ~fields ~@opts ~@specs) 36 | pfactory# ~pfactory 37 | factory# ~factory 38 | initializer# (fn [this#] 39 | (let [{:keys [~@fields]} this# 40 | ~init-params [this#] 41 | this# (do ~@init-body)] 42 | (when-not (instance? record# this#) 43 | (throw (java.lang.IllegalStateException. 44 | (str "Initializer should return instance of " '~name)))) 45 | this#))] 46 | ^{:clj-kondo/ignore [:redefined-var]} 47 | (defn ~pfactory 48 | {:doc (str (:doc (meta (var ~pfactory))) 49 | (when-let [docstring# ~docstring] 50 | (format "\n\n %s" docstring#)))} 51 | [~@fields] 52 | (let [defaults# (defaults#) 53 | extra-keys# (into #{} (remove ~(into #{} (map keyword) fields)) (keys defaults#))] 54 | (initializer# (merge (pfactory# ~@fields) (select-keys defaults# extra-keys#))))) 55 | ^{:clj-kondo/ignore [:redefined-var]} 56 | (defn ~factory 57 | {:doc (str (:doc (meta (var ~factory))) 58 | (when-let [docstring# ~docstring] 59 | (format "\n\n %s" docstring#))) 60 | :arglists '([& {:keys [~@fields]}])} 61 | [& {:as args#}] 62 | (initializer# (factory# (merge (defaults#) args#)))) 63 | record#))) 64 | -------------------------------------------------------------------------------- /src/systems/thoughtfull/desiderata.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright © technosophist 2 | ;; 3 | ;; This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of 4 | ;; the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | ;; 6 | ;; This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public 7 | ;; License, v. 2.0. 8 | (ns systems.thoughtfull.desiderata 9 | (:refer-clojure :exclude [defrecord]) 10 | (:import 11 | (java.util.concurrent ThreadFactory))) 12 | 13 | (set! *warn-on-reflection* true) 14 | 15 | (defn- parse-opts 16 | [opts+specs] 17 | (loop [[k v & s :as specs] opts+specs 18 | defaults nil 19 | opts []] 20 | (if (and (seq specs) (keyword? k)) 21 | (if (= k ::defaults) 22 | (recur s v opts) 23 | (recur s defaults (conj opts k v))) 24 | [defaults opts specs]))) 25 | 26 | (defn- parse-initializer 27 | [name specs] 28 | (loop [[s & ss :as specs] specs 29 | not-initializer []] 30 | (if (seq specs) 31 | (if (and (list? s) (= (first s) name)) 32 | [s (into not-initializer ss)] 33 | (recur ss (conj not-initializer s))) 34 | [nil not-initializer]))) 35 | 36 | (defmacro defrecord 37 | "Drop-in replacement for `clojure.core/defrecord` with extra functionality. See 38 | `clojure.core/defrecord` for details about core functionality. 39 | 40 | As a relatively minor addition the factory function takes keyword arguments. Another somewhat 41 | minor addition, metadata on the name symbol is propagated to the factory functions. There are 42 | three other additions: a docstring, default values, and an initializer. 43 | 44 | A docstring, if given, is appended to the docstring for both the factory and positional factory 45 | functions. 46 | 47 | Example: 48 | 49 | ```clojure 50 | user> (desiderata/defrecord Widget 51 | \"A widget for frobbing gizmos. 52 | 53 | width and height are in metric.\" 54 | [width height]) 55 | user.Widget 56 | 57 | user> (doc map->Widget) 58 | ------------------------- 59 | user/map->Widget 60 | ([& {:keys [width height]}]) 61 | Factory function for class user.Widget, taking a map of keywords to field values. 62 | 63 | A widget for frobbing gizmos. 64 | 65 | width and height are in metric. 66 | nil 67 | ``` 68 | 69 | The `:systems.thoughtfull.desiderata/defaults` option can be given with a hash map to supply 70 | defaults. The hash map supplies default values for the declared fields (and extra non-field 71 | keys and values). Any values given as arguments to the factory or positional factory functions 72 | override these defaults. 73 | 74 | Example: 75 | 76 | ```clojure 77 | user> (desiderata/defrecord Gizmo 78 | [name] 79 | ::desiderata/defaults 80 | {:name \"Gizmo\" 81 | :color :blue}) 82 | user.Gizmo 83 | 84 | user> (->Gizmo \"the Great\") 85 | {:name \"the Great\", :color :blue} 86 | 87 | user> (map->Gizmo :texture :bumpy) 88 | {:name \"Gizmo\", :color :blue, :texture :bumpy} 89 | ``` 90 | 91 | If a method with the same name as the defrecord is defined, it is used to as an initializer. 92 | After the record is constructed by the factory or positional factory function, it is given to 93 | the initializer and the result is returned from the factory or positional factory function. If 94 | the initializer does not return an instance of the type, an *IllegalStateException* is thrown. 95 | 96 | Example: 97 | 98 | ```clojure 99 | user> (desiderata/defrecord Company 100 | [debt equity] 101 | (Company 102 | [this] 103 | (assoc this :gearing-ratio (/ debt equity)))) 104 | user.Company 105 | 106 | user> (->Company 100 1000) 107 | {:debt 100, :equity 1000, :gearing-ratio 1/10} 108 | ``` 109 | 110 | - **`name`** — name of the type 111 | - **`docstring`** (optional) — appended to the docstrings of the factory and positional factory 112 | functions 113 | - **`fields`** — names of the fields of the type 114 | - **`opts+specs`** (optional) — options, interfaces, and methods 115 | 116 | See `clojure.core/defrecord`" 117 | {:arglists '([name docstring? [& fields] & opts+specs])} 118 | [name & args] 119 | (let [[docstring & args] (cond->> args (not (string? (first args))) (cons nil)) 120 | [fields & args] args 121 | [defaults opts specs] (parse-opts args) 122 | [initializer specs] (parse-initializer name specs) 123 | [init-params & init-body] (or (next initializer) `[[this#] this#]) 124 | pfactory (symbol (format "->%s" name)) 125 | factory (symbol (format "map->%s" name))] 126 | `(let [defaults# (fn [] ~defaults) 127 | record# (clojure.core/defrecord ~name ~fields ~@opts ~@specs) 128 | pfactory# ~pfactory 129 | factory# ~factory 130 | initializer# (fn [this#] 131 | (let [{:keys [~@fields]} this# 132 | ~init-params [this#] 133 | this# (do ~@init-body)] 134 | (when-not (instance? record# this#) 135 | (throw (java.lang.IllegalStateException. 136 | (str "Initializer should return instance of " '~name)))) 137 | this#))] 138 | (defn ~(with-meta pfactory (meta name)) 139 | {:doc (str (:doc (meta (var ~pfactory))) 140 | (when-let [docstring# ~docstring] 141 | (format "\n\n %s" docstring#))) 142 | :meta (meta (var ~pfactory))} 143 | [~@fields] 144 | (let [defaults# (defaults#) 145 | extra-keys# (into #{} (remove ~(into #{} (map keyword) fields)) (keys defaults#))] 146 | (initializer# (merge (pfactory# ~@fields) (select-keys defaults# extra-keys#))))) 147 | (defn ~(with-meta factory (meta name)) 148 | {:doc (str (:doc (meta (var ~factory))) 149 | (when-let [docstring# ~docstring] 150 | (format "\n\n %s" docstring#))) 151 | :arglists '([& {:keys [~@fields]}]) 152 | :meta (meta (var ~factory))} 153 | [& {:as args#}] 154 | (initializer# (factory# (merge (defaults#) args#)))) 155 | record#))) 156 | 157 | (defn uncaught-exception-handler 158 | "Adapt `uncaught-exception-handler-fn` to a *Thread.UncaughtExceptionHandler*. 159 | `uncaught-exception-handler-fn` should be a function of two arguments (thread and 160 | throwable). Returns nil if `uncaught-exception-handler-fn` is nil. 161 | 162 | - **`uncaught-exception-handler-fn`** — function of two arguments (thread and throwable) to 163 | adapt as a *Thread.UncaughtExceptionHandler*. Returns nil if `uncaught-exception-handler-fn` is 164 | nil. 165 | 166 | See *Thread.UncaughtExceptionHandler*" 167 | ^Thread$UncaughtExceptionHandler [uncaught-exception-handler-fn] 168 | (when uncaught-exception-handler-fn 169 | (reify Thread$UncaughtExceptionHandler 170 | (uncaughtException 171 | [_ thread throwable] 172 | (uncaught-exception-handler-fn thread throwable))))) 173 | 174 | (defn set-default-uncaught-exception-handler-fn! 175 | "Set default *Thread.UncaughtExceptionHandler* to `uncaught-exception-handler-fn` after adapting 176 | it using [[uncaught-exception-handler]]. `uncaught-exception-handler-fn` should be a function 177 | of two arguments (thread and throwable). If `uncaught-exception-handler-fn` is nil, then the 178 | default uncaught exception handler is cleared. 179 | 180 | - **`uncaught-exception-handler-fn`** — function of two arguments (thread and throwable) to set 181 | as the default *Thread.UncaughtExceptionHandler* after adapting it using 182 | [[uncaught-exception-handler]]. Returns nil if `uncaught-exception-handler-fn` is nil. 183 | 184 | See *Thread/setDefaultUncaughtExceptionHandler*, *Thread.UncaughtExceptionHandler*" 185 | [uncaught-exception-handler-fn] 186 | (Thread/setDefaultUncaughtExceptionHandler 187 | (uncaught-exception-handler uncaught-exception-handler-fn))) 188 | 189 | (defonce ^:private pool-count (atom 0)) 190 | 191 | (defn thread-factory 192 | "Create *java.util.concurrent.ThreadFactory*. 193 | 194 | - **`name`** (optional) — prefix for thread names, e.g., a `name` of `\"widget-pool\"` will create 195 | threads named `\"widget-pool-thread-N\"` where N increments each time a thread is created. If 196 | `name` is not given, it defaults to `\"pool-M\"` where M is incremented for each new thread 197 | factory. 198 | - **`convey-bindings?`** (optional) — if true convey to created threads the bindings established 199 | when the thread factory was created, defaults to false. 200 | - **`priority`** (optional) — initial thread priority for created threads, defaults to 201 | *Thread/NORMAL_PRIORITY*. 202 | - **`daemon?`** (optional) — if true threads should be marked as daemon threads, defaults to 203 | false. 204 | - **`uncaught-exception-handler-fn`** (optional) — function to set as a 205 | *Thread.UncaughtExceptionHandler* after adapting with [[uncaught-exception-handler]]. Bindings 206 | established when the thread factory is created are conveyed to `uncaught-exception-handler-fn`. 207 | - **`inherit-inheritable-thread-locals?`** (optional) — if true, then new threads inherit initial 208 | values for *InheritableThreadLocal* from constructing thread, defaults to true. See 209 | *java.util.Thread/new*. 210 | 211 | See *java.util.concurrent.ThreadFactory*, *Thread.UncaughtExceptionHandler*" 212 | {:arglists '([& {:keys [name convey-bindings? priority daemon? uncaught-exception-handler-fn 213 | inherit-inheritable-thread-locals?]}])} 214 | ^ThreadFactory 215 | [& {:as options 216 | :keys [name convey-bindings? priority daemon? uncaught-exception-handler-fn 217 | inherit-inheritable-thread-locals?]}] 218 | (let [daemon? (boolean daemon?) 219 | inherit-inheritable-thread-locals? (if (contains? options 220 | :inherit-inheritable-thread-locals?) 221 | (boolean inherit-inheritable-thread-locals?) 222 | true) 223 | prefix (format "%s-thread-" (or name (format "pool-%d" (swap! pool-count inc)))) 224 | thread-count (atom 0) 225 | frame (clojure.lang.Var/cloneThreadBindingFrame) 226 | ueh (when uncaught-exception-handler-fn 227 | (uncaught-exception-handler 228 | (fn [thread throwable] 229 | (clojure.lang.Var/resetThreadBindingFrame frame) 230 | (uncaught-exception-handler-fn thread throwable))))] 231 | (reify ThreadFactory 232 | (^Thread newThread [_ ^Runnable r] 233 | (let [r (if convey-bindings? 234 | (fn [] 235 | (clojure.lang.Var/resetThreadBindingFrame frame) 236 | (.run r)) 237 | r) 238 | t (Thread. nil r (str prefix (swap! thread-count inc)) 0 239 | inherit-inheritable-thread-locals?)] 240 | (.setPriority t (or priority Thread/NORM_PRIORITY)) 241 | (when ueh (.setUncaughtExceptionHandler t ueh)) 242 | (.setDaemon t daemon?) 243 | t))))) 244 | -------------------------------------------------------------------------------- /test/systems/thoughtfull/desiderata_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright © technosophist 2 | ;; 3 | ;; This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of 4 | ;; the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 | ;; 6 | ;; This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public 7 | ;; License, v. 2.0. 8 | (ns systems.thoughtfull.desiderata-test 9 | (:require 10 | [clojure.test :refer [deftest is]] 11 | [systems.thoughtfull.desiderata :as desiderata])) 12 | 13 | (desiderata/defrecord OnlyFields []) 14 | 15 | (deftest defrecord-only-fields 16 | (is (= {} (into {} (->OnlyFields))))) 17 | 18 | (desiderata/defrecord Docstringed 19 | "Docstringed is a docstring'd." 20 | []) 21 | 22 | (deftest defrecord-uses-docstring 23 | (is (re-find #"\n\n Docstringed is a docstring'd." (:doc (meta #'->Docstringed)))) 24 | (is (re-find #"\n\n Docstringed is a docstring'd." (:doc (meta #'map->Docstringed))))) 25 | 26 | (desiderata/defrecord Initialized 27 | [initialized?] 28 | (Initialized 29 | [this] 30 | (assoc this :initialized? (not initialized?)))) 31 | 32 | (deftest defrecord-initializes 33 | (is (:initialized? (->Initialized false))) 34 | (is (:initialized? (map->Initialized :initialized? false)))) 35 | 36 | (desiderata/defrecord InvalidInitialized 37 | [] 38 | (InvalidInitialized 39 | [_this] 40 | nil)) 41 | 42 | (deftest defrecord-validates-initializer-return-value 43 | (is (thrown? IllegalStateException (->InvalidInitialized))) 44 | (is (thrown? IllegalStateException (map->InvalidInitialized)))) 45 | 46 | (desiderata/defrecord Defaulted 47 | [field defaulted-field] 48 | ::desiderata/defaults {:defaulted-field :default :extra :extra}) 49 | 50 | (deftest defrecord-uses-defaults 51 | (is (= {:field :arg 52 | :defaulted-field :arg 53 | :extra :extra} 54 | (into {} (->Defaulted :arg :arg)))) 55 | (is (= {:field :arg 56 | :defaulted-field :default 57 | :extra :extra 58 | :double-extra :double-extra} 59 | (into {} (map->Defaulted :field :arg :double-extra :double-extra))))) 60 | 61 | (desiderata/defrecord Freshly 62 | [] 63 | ::desiderata/defaults {:fresh (Object.)}) 64 | 65 | (deftest defrecord-defaults-are-freshly-evaluated 66 | (is (not= (:fresh (->Freshly)) (:fresh (->Freshly))))) 67 | 68 | (desiderata/defrecord ^:conveyed? ConveyMeta []) 69 | 70 | (deftest defrecord-conveys-meta 71 | (is (:conveyed? (meta #'->ConveyMeta))) 72 | (is (:conveyed? (meta #'map->ConveyMeta)))) 73 | 74 | (deftest default-uncaught-exception-handler-handles 75 | (let [handled? (promise)] 76 | (desiderata/set-default-uncaught-exception-handler-fn! (fn [_ _] (deliver handled? true))) 77 | (doto (Thread. #(throw (Exception.))) 78 | .start 79 | .join) 80 | (is (deref handled? 1000 false)))) 81 | 82 | (deftest default-uncaught-exception-handler-is-cleared-with-null-fn 83 | (desiderata/set-default-uncaught-exception-handler-fn! nil) 84 | (is (nil? (Thread/getDefaultUncaughtExceptionHandler)))) 85 | 86 | (deftest thread-factory-sets-thread-name 87 | (let [tf (desiderata/thread-factory)] 88 | (is (re-matches #"pool-\d+-thread-1" (.getName (.newThread tf #(do))))))) 89 | 90 | (deftest thread-factory-sets-thread-name-with-prefix 91 | (let [tf (desiderata/thread-factory :name "widget-pool")] 92 | (is (= "widget-pool-thread-1" (.getName (.newThread tf #(do))))))) 93 | 94 | (deftest thread-factory-sets-normal-priority-when-priority-not-given 95 | (let [tf (desiderata/thread-factory)] 96 | (is (= Thread/NORM_PRIORITY (.getPriority (.newThread tf #(do))))))) 97 | 98 | (deftest thread-factory-sets-priority-when-priority-given 99 | (let [tf (desiderata/thread-factory :priority 3)] 100 | (is (= 3 (.getPriority (.newThread tf #(do))))))) 101 | 102 | (def ^:dynamic *bindable* :root) 103 | 104 | (deftest thread-factory-conveys-bindings-to-exception-handler 105 | (let [handler-value (promise) 106 | tf (binding [*bindable* :binding] 107 | (desiderata/thread-factory 108 | :convey-bindings? true 109 | :uncaught-exception-handler-fn (fn [_ _] (deliver handler-value *bindable*)))) 110 | t (.newThread tf #(throw (Exception.)))] 111 | (doto t .start .join) 112 | (is (= :binding (deref handler-value 1000 :timeout))))) 113 | 114 | (deftest thread-factory-does-not-convey-bindings-to-exception-handler-with-no-bindings 115 | (let [handler-value (promise) 116 | tf (desiderata/thread-factory 117 | :convey-bindings? true 118 | :uncaught-exception-handler-fn (fn [_ _] (deliver handler-value *bindable*))) 119 | t (.newThread tf #(throw (Exception.)))] 120 | (doto t .start .join) 121 | (is (= :root (deref handler-value 1000 :timeout))))) 122 | 123 | (deftest thread-factory-conveys-bindings-to-threads 124 | (let [thread-value (promise) 125 | tf (binding [*bindable* :binding] 126 | (desiderata/thread-factory :convey-bindings? true)) 127 | t (.newThread tf #(deliver thread-value *bindable*))] 128 | (doto t .start .join) 129 | (is (= :binding (deref thread-value 1000 :timeout))))) 130 | 131 | (deftest thread-factory-does-not-convey-bindings-to-threads-with-no-bindings 132 | (let [thread-value (promise) 133 | tf (desiderata/thread-factory :convey-bindings? true) 134 | t (.newThread tf #(deliver thread-value *bindable*))] 135 | (doto t .start .join) 136 | (is (= :root (deref thread-value 1000 :timeout))))) 137 | 138 | (deftest thread-factory-does-not-convey-bindings-to-threads-with-bindings 139 | (let [thread-value (promise) 140 | tf (binding [*bindable* :binding] 141 | (desiderata/thread-factory :convey-bindings? false)) 142 | t (.newThread tf #(deliver thread-value *bindable*))] 143 | (doto t .start .join) 144 | (is (= :root (deref thread-value 1000 :timeout))))) 145 | 146 | (deftest thread-factory-does-not-convey-bindings-from-thread-creation 147 | (let [thread-value (promise) 148 | tf (desiderata/thread-factory :convey-bindings? false) 149 | t (binding [*bindable* :binding] 150 | (.newThread tf #(deliver thread-value *bindable*)))] 151 | (doto t .start .join) 152 | (is (= :root (deref thread-value 1000 :timeout))))) 153 | 154 | (deftest thread-factory-does-not-make-daemon-threads 155 | (let [tf (desiderata/thread-factory)] 156 | (is (not (.isDaemon (.newThread tf #(do))))))) 157 | 158 | (deftest thread-factory-makes-daemon-threads 159 | (let [tf (desiderata/thread-factory :daemon? true)] 160 | (is (.isDaemon (.newThread tf #(do)))))) 161 | 162 | (deftest thread-factory-inherits-inheritable-thread-locals 163 | (let [tf (desiderata/thread-factory) 164 | tl (doto (InheritableThreadLocal.) 165 | (.set :thread-local)) 166 | thread-value (promise) 167 | t (.newThread tf #(deliver thread-value (.get tl)))] 168 | (.remove tl) 169 | (doto t .start .join) 170 | (is (= :thread-local (deref thread-value 1000 :timeout))))) 171 | 172 | (deftest thread-factory-does-not-inherit-inheritable-thread-locals-when-not-set 173 | (let [tf (desiderata/thread-factory) 174 | tl (InheritableThreadLocal.) 175 | thread-value (promise) 176 | t (.newThread tf #(deliver thread-value (.get tl)))] 177 | (doto t .start .join) 178 | (is (nil? (deref thread-value 1000 :timeout))))) 179 | 180 | (deftest thread-factory-does-not-inherit-inheritable-thread-locals 181 | (let [tf (desiderata/thread-factory :inherit-inheritable-thread-locals? false) 182 | tl (doto (InheritableThreadLocal.) 183 | (.set :thread-local)) 184 | thread-value (promise) 185 | t (.newThread tf #(deliver thread-value (.get tl)))] 186 | (doto t .start .join) 187 | (is (nil? (deref thread-value 1000 :timeout))))) 188 | --------------------------------------------------------------------------------