Skip to content

Commit 5459c01

Browse files
committed
Improvements
1 parent e428a3f commit 5459c01

File tree

4 files changed

+45
-43
lines changed

4 files changed

+45
-43
lines changed

pages/diagnostics/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ nav_order: 3
44
has_children: true
55
has_toc: false
66
permalink: /diagnostics/
7-
description: Understand LiquidJava errors and warnings.
7+
description: Learn about the different types of errors and warnings, how to use custom error messages, and how to interpret refinement errors.
88
---
99

1010
# Diagnostics
1111

12-
Learn how to understand LiquidJava errors and warnings.
12+
In this section, you can learn about the different types of errors and warnings that LiquidJava can report, how to use custom error messages to provide more informative feedback, and how to interpret refinement errors.

pages/examples/pagination.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import liquidjava.specification.*;
1515

1616
@RefinementAlias("Page(int p) { p >= 1 }")
1717
@RefinementAlias("PageSize(int s) { 1 <= s && s <= 100 }")
18+
@RefinementAlias("PageCount(int p) { p >= 0 }")
1819
@RefinementAlias("ItemCount(int n) { n >= 0 }")
1920
public class Pagination {
2021
@Refinement("ItemCount(_) && _ == (page - 1) * size")
@@ -30,7 +31,7 @@ public class Pagination {
3031
return page + 1;
3132
}
3233

33-
@Refinement("Page(_) && _ == (items + size - 1) / size")
34+
@Refinement("PageCount(_) && _ == (items + size - 1) / size")
3435
public static int totalPages(
3536
@Refinement("ItemCount(_)") int items,
3637
@Refinement("PageSize(_)") int size
@@ -60,4 +61,5 @@ The aliases capture the key domain concepts for pagination:
6061

6162
- `Page` says page numbers start at `1`
6263
- `PageSize` constrains the accepted page size range
64+
- `PageCount` prevents negative page counts
6365
- `ItemCount` prevents negative item counts

pages/examples/reentrant-lock.md

Lines changed: 0 additions & 40 deletions
This file was deleted.

pages/examples/semaphore.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
title: Semaphore
3+
parent: Examples
4+
nav_order: 9
5+
permalink: /examples/semaphore/
6+
description: An external typestate refinement for a simple semaphore protocol.
7+
---
8+
9+
# Semaphore
10+
11+
This example refines `java.util.concurrent.Semaphore` with a simple two-state protocol that models a binary semaphore being acquired and released.
12+
13+
```java
14+
import liquidjava.specification.*;
15+
16+
@ExternalRefinementsFor("java.util.concurrent.Semaphore")
17+
@StateSet({"available", "acquired"})
18+
public interface SemaphoreRefinements {
19+
@StateRefinement(to="available()")
20+
public void Semaphore(@Refinement("_ == 1") int permits);
21+
22+
@StateRefinement(from="available()", to="acquired()")
23+
public void acquire();
24+
25+
@StateRefinement(from="acquired()", to="available()")
26+
public void release();
27+
}
28+
```
29+
30+
```java
31+
Semaphore semaphore = new Semaphore(1);
32+
semaphore.acquire();
33+
semaphore.release();
34+
semaphore.release(); // State Refinement Error
35+
```
36+
37+
The external refinement enforces the expected protocol: `acquire()` is only allowed when the semaphore is `available()`, and `release()` is only allowed when it is `acquired()`.
38+
39+
{: .note }
40+
This specification models a binary semaphore with one permit. It does not account for semaphores with multiple permits or concurrent access across multiple threads.

0 commit comments

Comments
 (0)