Skip to content

Commit c8fcc12

Browse files
committed
README updated with Kotlin usage
1 parent 2d25615 commit c8fcc12

1 file changed

Lines changed: 71 additions & 0 deletions

File tree

README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ dependencies {
5353

5454
### Using `Tuple`
5555

56+
#### Java
57+
5658
Create a tuple with `Tuple.of()`:
5759

5860
```java
@@ -68,8 +70,25 @@ Object o = tuple.getObject(2);
6870
System.out.println("Size: " + tuple.size()); // Size: 3
6971
```
7072

73+
#### Kotlin
74+
75+
```kotlin
76+
import com.appxiom.ax.tuple.Tuple
77+
78+
val tuple = Tuple.of("Hello", 42, 3.14)
79+
80+
// Access by index
81+
val s = tuple.get(0) // Inferred type
82+
val i = tuple.get(1, Integer::class.java) // Explicit type
83+
val o = tuple.getObject(2)
84+
85+
println("Size: ${tuple.size()}") // Size: 3
86+
```
87+
7188
### Using `NamedTuple`
7289

90+
#### Java
91+
7392
Create a named tuple from a `Map`:
7493

7594
```java
@@ -89,10 +108,30 @@ Boolean isActive = user.get("active", Boolean.class);
89108
System.out.println(user.toString());
90109
```
91110

111+
#### Kotlin
112+
113+
```kotlin
114+
import com.appxiom.ax.tuple.NamedTuple
115+
116+
val user = NamedTuple.of(mapOf(
117+
"id" to 1,
118+
"username" to "robin",
119+
"active" to true
120+
))
121+
122+
// Access by key
123+
val username = user.get("username")
124+
val isActive = user.get("active", Boolean::class.java)
125+
126+
println(user.toString())
127+
```
128+
92129
## Using as HashMap Keys
93130

94131
### Using Tuple as a key
95132

133+
#### Java
134+
96135
Both `Tuple` and `NamedTuple` override `equals()` and `hashCode()`, making them perfectly suitable for use as keys in a `HashMap` or as elements in a `HashSet`.
97136

98137
Two tuples are considered equal if they contain the same elements in the same order. Two named tuples are equal if they contain the same key-value pairs.
@@ -110,8 +149,24 @@ cache.put(key, "Cached Result");
110149
System.out.println(cache.get(Tuple.of("request", 12345))); // Output: Cached Result
111150
```
112151

152+
#### Kotlin
153+
154+
```kotlin
155+
import com.appxiom.ax.tuple.Tuple
156+
157+
val cache = mutableMapOf<Tuple, String>()
158+
val key = Tuple.of("request", 12345)
159+
160+
cache[key] = "Cached Result"
161+
162+
// Retrievable with a different Tuple instance containing identical data
163+
println(cache[Tuple.of("request", 12345)]) // Output: Cached Result
164+
```
165+
113166
### Using NamedTuple as a key
114167

168+
#### Java
169+
115170
```java
116171
import java.util.HashMap;
117172
import com.appxiom.ax.tuple.NamedTuple;
@@ -128,6 +183,22 @@ NamedTuple key2 = NamedTuple.of(Map.of("id", 101, "role", "admin"));
128183
System.out.println(userRegistry.get(key2)); // Output: Admin Account
129184
```
130185

186+
#### Kotlin
187+
188+
```kotlin
189+
import com.appxiom.ax.tuple.NamedTuple
190+
191+
// Using NamedTuple as a key
192+
val userRegistry = mutableMapOf<NamedTuple, String>()
193+
val key1 = NamedTuple.of(mapOf("id" to 101, "role" to "admin"))
194+
195+
userRegistry[key1] = "Admin Account"
196+
197+
// Retrieve using a new instance with identical keys and values
198+
val key2 = NamedTuple.of(mapOf("id" to 101, "role" to "admin"))
199+
println(userRegistry[key2]) // Output: Admin Account
200+
```
201+
131202
## License
132203

133204
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

0 commit comments

Comments
 (0)