Skip to content

Commit ebd0cdd

Browse files
committed
Immutable Tuple implementation for Java
0 parents  commit ebd0cdd

6 files changed

Lines changed: 555 additions & 0 deletions

File tree

.gitignore

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
/index.html
4+
5+
### STS ###
6+
.apt_generated
7+
.classpath
8+
.factorypath
9+
.project
10+
.settings
11+
.springBeans
12+
.sts4-cache
13+
14+
### IntelliJ IDEA ###
15+
.idea
16+
*.iws
17+
*.iml
18+
*.ipr
19+
20+
### NetBeans ###
21+
/nbproject/private/
22+
/nbbuild/
23+
/dist/
24+
/nbdist/
25+
/.nb-gradle/
26+
build/
27+
!**/src/main/resources/archetype-resources/pom.xml
28+
29+
### VS Code ###
30+
.vscode/
31+
32+
### Mac OS ###
33+
.DS_Store

pom.xml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.appxiom.ax.tuple</groupId>
8+
<artifactId>ax-tuple</artifactId>
9+
<version>0.1.0</version>
10+
11+
<properties>
12+
<maven.compiler.source>21</maven.compiler.source>
13+
<maven.compiler.target>21</maven.compiler.target>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<junit.jupiter.version>5.10.1</junit.jupiter.version>
16+
</properties>
17+
18+
<dependencies>
19+
<dependency>
20+
<groupId>org.junit.jupiter</groupId>
21+
<artifactId>junit-jupiter-api</artifactId>
22+
<version>${junit.jupiter.version}</version>
23+
<scope>test</scope>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.junit.jupiter</groupId>
27+
<artifactId>junit-jupiter-engine</artifactId>
28+
<version>${junit.jupiter.version}</version>
29+
<scope>test</scope>
30+
</dependency>
31+
</dependencies>
32+
33+
<build>
34+
<plugins>
35+
<plugin>
36+
<groupId>org.apache.maven.plugins</groupId>
37+
<artifactId>maven-compiler-plugin</artifactId>
38+
<version>3.11.0</version>
39+
<configuration>
40+
<source>${maven.compiler.source}</source>
41+
<target>${maven.compiler.target}</target>
42+
</configuration>
43+
</plugin>
44+
<plugin>
45+
<groupId>org.apache.maven.plugins</groupId>
46+
<artifactId>maven-surefire-plugin</artifactId>
47+
<version>3.2.2</version>
48+
</plugin>
49+
</plugins>
50+
</build>
51+
</project>
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/**
2+
* A named tuple implementation for Java that stores elements as key-value pairs using a Map.
3+
*
4+
* @author Robin Panicker
5+
* @version 0.1.0
6+
* @since 0.1.0
7+
*
8+
*/
9+
package com.appxiom.ax.tuple;
10+
11+
import java.util.Map;
12+
import java.util.Objects;
13+
14+
/**
15+
* Represents a tuple where each element is associated with a specific name
16+
*/
17+
public class NamedTuple {
18+
19+
/**
20+
* The internal map storing the named elements of the tuple.
21+
*/
22+
private final Map<String, Object> map;
23+
24+
/**
25+
* Creates a new NamedTuple with the specified map of name-value pairs.
26+
*
27+
* @param map the map containing the named elements
28+
* @return a new NamedTuple containing the specified map
29+
*/
30+
public static NamedTuple of(Map<String, Object> map) {
31+
return new NamedTuple(map);
32+
}
33+
34+
/**
35+
* Private constructor to prevent direct instantiation.
36+
*
37+
* @param map the map containing the named elements
38+
*/
39+
private NamedTuple(Map<String, Object> map) {
40+
this.map = map;
41+
}
42+
43+
/**
44+
* Retrieves the value associated with the specified name.
45+
*
46+
* @param key the name of the element to retrieve
47+
* @return the value associated with the key, or {@code null} if the key is not
48+
* found
49+
*/
50+
public Object getObject(String key) {
51+
return map.get(key);
52+
}
53+
54+
/**
55+
* Retrieves the element at the specified index and casts it to the inferred
56+
* type.
57+
* Use with caution as it may throw a ClassCastException at runtime if the type
58+
* is incorrect.
59+
*
60+
* @param <T> the type to cast the element to
61+
* @param key the name of the element to retrieve
62+
* @return the element at the specified index, cast to type T
63+
* @throws ClassCastException if the element cannot be cast to the
64+
* specified type
65+
*/
66+
@SuppressWarnings("unchecked")
67+
public <T> T get(String key) {
68+
return (T) map.get(key);
69+
}
70+
71+
/**
72+
* Retrieves the element at the specified index and casts it to the specified
73+
* class type.
74+
*
75+
* @param <T> the type to cast the element to
76+
* @param key the name of the element to retrieve
77+
* @param type the class of the type to cast the element to
78+
* @return the element at the specified index, cast to the specified type
79+
* @throws ClassCastException if the element cannot be cast to the
80+
* specified type
81+
*/
82+
public <T> T get(String key, Class<T> type) {
83+
return type.cast(map.get(key));
84+
}
85+
86+
/**
87+
* Returns the number of named elements in this tuple.
88+
*
89+
* @return the size of the named tuple
90+
*/
91+
public int size() {
92+
return map.size();
93+
}
94+
95+
/**
96+
* Returns a string representation of the named tuple, typically following
97+
* the format of the underlying Map.
98+
*
99+
* @return a string representation of this named tuple
100+
*/
101+
@Override
102+
public String toString() {
103+
return map.toString();
104+
}
105+
106+
/**
107+
* Compares this named tuple to the specified object for equality.
108+
* Two named tuples are considered equal if they have the same name-value pairs.
109+
*
110+
* @param o the object to compare with
111+
* @return {@code true} if the specified object is equal to this named tuple,
112+
* {@code false} otherwise
113+
*/
114+
@Override
115+
public boolean equals(Object o) {
116+
if (this == o)
117+
return true;
118+
if (o == null || getClass() != o.getClass())
119+
return false;
120+
NamedTuple that = (NamedTuple) o;
121+
return Objects.equals(map, that.map);
122+
}
123+
124+
/**
125+
* Returns a hash code value for this named tuple.
126+
*
127+
* @return a hash code value for this named tuple
128+
*/
129+
@Override
130+
public int hashCode() {
131+
return Objects.hash(map);
132+
}
133+
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/**
2+
* A simple immutable Tuple implementation for Java that can hold an arbitrary number of elements.
3+
*
4+
* @author Robin Panicker
5+
* @version 0.1.0
6+
* @since 0.1.0
7+
*/
8+
package com.appxiom.ax.tuple;
9+
10+
import java.util.Arrays;
11+
12+
/**
13+
* Represents an immutable sequence of elements.
14+
*/
15+
public final class Tuple {
16+
17+
/**
18+
* The internal array of elements stored in the tuple.
19+
*/
20+
private final Object[] elements;
21+
22+
/**
23+
* Creates a new Tuple with the specified elements.
24+
*
25+
* @param elements the elements to be included in the tuple
26+
* @return a new Tuple containing the specified elements
27+
*/
28+
public static Tuple of(Object... elements) {
29+
return new Tuple(elements);
30+
}
31+
32+
/**
33+
* Private constructor to prevent direct instantiation.
34+
*
35+
* @param elements the elements to be included in the tuple
36+
*/
37+
private Tuple(Object... elements) {
38+
this.elements = elements;
39+
}
40+
41+
/**
42+
* Retrieves the element at the specified index as an Object.
43+
*
44+
* @param index the index of the element to retrieve
45+
* @return the element at the specified index
46+
* @throws ArrayIndexOutOfBoundsException if the index is out of range
47+
*/
48+
public Object getObject(int index) {
49+
return elements[index];
50+
}
51+
52+
/**
53+
* Retrieves the element at the specified index and casts it to the inferred
54+
* type.
55+
* Use with caution as it may throw a ClassCastException at runtime if the type
56+
* is incorrect.
57+
*
58+
* @param <T> the type to cast the element to
59+
* @param index the index of the element to retrieve
60+
* @return the element at the specified index, cast to type T
61+
* @throws ArrayIndexOutOfBoundsException if the index is out of range
62+
*/
63+
@SuppressWarnings("unchecked")
64+
public <T> T get(int index) {
65+
return (T) elements[index];
66+
}
67+
68+
/**
69+
* Retrieves the element at the specified index and casts it to the specified
70+
* class type.
71+
*
72+
* @param <T> the type to cast the element to
73+
* @param index the index of the element to retrieve
74+
* @param type the class of the type to cast the element to
75+
* @return the element at the specified index, cast to the specified type
76+
* @throws ArrayIndexOutOfBoundsException if the index is out of range
77+
* @throws ClassCastException if the element cannot be cast to the
78+
* specified type
79+
*/
80+
public <T> T get(int index, Class<T> type) {
81+
Object value = elements[index];
82+
return type.cast(value);
83+
}
84+
85+
/**
86+
* Returns the number of elements in this tuple.
87+
*
88+
* @return the size of the tuple
89+
*/
90+
public int size() {
91+
return elements.length;
92+
}
93+
94+
/**
95+
* Returns a string representation of the tuple, consisting of the elements
96+
* wrapped in square brackets and separated by commas.
97+
*
98+
* @return a string representation of this tuple
99+
*/
100+
@Override
101+
public String toString() {
102+
return Arrays.toString(elements);
103+
}
104+
105+
/**
106+
* Compares this tuple to the specified object for equality.
107+
* Two tuples are considered equal if they have the same elements in the same
108+
* order.
109+
*
110+
* @param o the object to compare with
111+
* @return {@code true} if the specified object is equal to this tuple,
112+
* {@code false} otherwise
113+
*/
114+
@Override
115+
public boolean equals(Object o) {
116+
if (this == o)
117+
return true;
118+
if (o == null || !(o instanceof Tuple))
119+
return false;
120+
Tuple tuple = (Tuple) o;
121+
return Arrays.equals(elements, tuple.elements);
122+
}
123+
124+
/**
125+
* Returns a hash code value for this tuple.
126+
*
127+
* @return a hash code value for this tuple
128+
*/
129+
@Override
130+
public int hashCode() {
131+
return Arrays.hashCode(elements);
132+
}
133+
}

0 commit comments

Comments
 (0)