Skip to content

Commit 0e6e75d

Browse files
committed
added Demo classes
1 parent 50ba70e commit 0e6e75d

4 files changed

Lines changed: 225 additions & 0 deletions

File tree

pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@
5454
<artifactId>maven-surefire-plugin</artifactId>
5555
<version>3.2.2</version>
5656
</plugin>
57+
<plugin>
58+
<groupId>org.codehaus.mojo</groupId>
59+
<artifactId>exec-maven-plugin</artifactId>
60+
<version>3.1.0</version>
61+
<configuration>
62+
<mainClass>com.appxiom.ax.tuple.demo.DemoRunner</mainClass>
63+
</configuration>
64+
</plugin>
5765
</plugins>
5866
</build>
5967
</project>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @author Robin Panicker
3+
* @version 1.0.0
4+
* @since 0.1.0
5+
*/
6+
package com.appxiom.ax.tuple.demo;
7+
8+
/**
9+
* A runner class to execute all Tuple and NamedTuple demos.
10+
*/
11+
public class DemoRunner {
12+
13+
public static void main(String[] args) {
14+
System.out.println("========================================");
15+
System.out.println(" Ax-Tuple Demonstration Runner");
16+
System.out.println("========================================\n");
17+
18+
NestedMapComparison.main(args);
19+
20+
System.out.println("========================================");
21+
System.out.println(" Ax-NamedTuple Demonstration Runner");
22+
System.out.println("========================================\n");
23+
24+
NamedTupleComparison.main(args);
25+
26+
System.out.println("\n========================================");
27+
System.out.println(" Demonstration Completed");
28+
System.out.println("========================================");
29+
}
30+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/**
2+
* @author Robin Panicker
3+
* @version 1.0.0
4+
* @since 0.1.0
5+
*/
6+
package com.appxiom.ax.tuple.demo;
7+
8+
import com.appxiom.ax.tuple.NamedTuple;
9+
import java.util.HashMap;
10+
import java.util.Map;
11+
12+
/**
13+
* This class demonstrates the usage of NamedTuple as a HashMap key.
14+
* It shows how named keys provide better self-documentation compared to
15+
* standard Tuples.
16+
*/
17+
public class NamedTupleComparison {
18+
19+
public static void main(String[] args) {
20+
System.out.println("--- Scenario 1: Nested HashMaps ---");
21+
demonstrateNestedMap();
22+
23+
System.out.println("\n--- Scenario 2: Flat HashMap with NamedTuple Key ---");
24+
demonstrateNamedTupleMap();
25+
}
26+
27+
/**
28+
* Demonstrates the use of nested HashMaps to manage sales data.
29+
*/
30+
private static void demonstrateNestedMap() {
31+
// Data Structure: Region -> Year -> Product -> Sales
32+
Map<String, Map<Integer, Map<String, Double>>> salesData = new HashMap<>();
33+
34+
// 1. Populating the data
35+
addDataNested(salesData, "North", 2023, "Widget A", 1500.0);
36+
addDataNested(salesData, "North", 2023, "Widget B", 2000.0);
37+
addDataNested(salesData, "South", 2023, "Widget A", 1200.0);
38+
addDataNested(salesData, "North", 2024, "Widget A", 1800.0);
39+
40+
// 2. Iterating to get final data (Requires 3 nested for-loops)
41+
for (Map.Entry<String, Map<Integer, Map<String, Double>>> regionEntry : salesData.entrySet()) {
42+
String region = regionEntry.getKey();
43+
for (Map.Entry<Integer, Map<String, Double>> yearEntry : regionEntry.getValue().entrySet()) {
44+
Integer year = yearEntry.getKey();
45+
for (Map.Entry<String, Double> productEntry : yearEntry.getValue().entrySet()) {
46+
String product = productEntry.getKey();
47+
Double sales = productEntry.getValue();
48+
System.out.printf("Region: %s, Year: %d, Product: %s -> Sales: $%.2f%n",
49+
region, year, product, sales);
50+
}
51+
}
52+
}
53+
}
54+
55+
private static void addDataNested(Map<String, Map<Integer, Map<String, Double>>> salesData,
56+
String region, Integer year, String product, Double sales) {
57+
salesData.computeIfAbsent(region, k -> new HashMap<>())
58+
.computeIfAbsent(year, k -> new HashMap<>())
59+
.put(product, sales);
60+
}
61+
62+
/**
63+
* Demonstrates the use of a flat HashMap with NamedTuple keys to manage sales
64+
* data.
65+
*/
66+
private static void demonstrateNamedTupleMap() {
67+
// Data Structure: NamedTuple(Region, Year, Product) -> Sales
68+
Map<NamedTuple, Double> salesData = new HashMap<>();
69+
70+
// 1. Populating the data
71+
salesData.put(createNamedKey("North", 2023, "Widget A"), 1500.0);
72+
salesData.put(createNamedKey("North", 2023, "Widget B"), 2000.0);
73+
salesData.put(createNamedKey("South", 2023, "Widget A"), 1200.0);
74+
salesData.put(createNamedKey("North", 2024, "Widget A"), 1800.0);
75+
76+
// 2. Iterating to get final data
77+
for (Map.Entry<NamedTuple, Double> entry : salesData.entrySet()) {
78+
NamedTuple key = entry.getKey();
79+
String region = key.get("Region");
80+
Integer year = key.get("Year");
81+
String product = key.get("Product");
82+
Double sales = entry.getValue();
83+
84+
System.out.printf("Region: %s, Year: %d, Product: %s -> Sales: $%.2f%n",
85+
region, year, product, sales);
86+
}
87+
}
88+
89+
/**
90+
* Helper to create a NamedTuple key for the demo.
91+
*/
92+
private static NamedTuple createNamedKey(String region, Integer year, String product) {
93+
Map<String, Object> fields = new HashMap<>();
94+
fields.put("Region", region);
95+
fields.put("Year", year);
96+
fields.put("Product", product);
97+
return NamedTuple.of(fields);
98+
}
99+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/**
2+
* @author Robin Panicker
3+
* @version 1.0.0
4+
* @since 0.1.0
5+
*/
6+
package com.appxiom.ax.tuple.demo;
7+
8+
import com.appxiom.ax.tuple.Tuple;
9+
import java.util.HashMap;
10+
import java.util.Map;
11+
12+
/**
13+
* This class demonstrates the comparison between nested HashMaps and a flat
14+
* HashMap
15+
* with Tuple keys for managing sales data.
16+
*/
17+
public class NestedMapComparison {
18+
19+
public static void main(String[] args) {
20+
System.out.println("--- Scenario 1: Nested HashMaps ---");
21+
demonstrateNestedMap();
22+
23+
System.out.println("\n--- Scenario 2: Flat HashMap with Tuple Key ---");
24+
demonstrateTupleMap();
25+
}
26+
27+
/**
28+
* Demonstrates the use of nested HashMaps to manage sales data.
29+
*/
30+
private static void demonstrateNestedMap() {
31+
// Data Structure: Region -> Year -> Product -> Sales
32+
Map<String, Map<Integer, Map<String, Double>>> salesData = new HashMap<>();
33+
34+
// 1. Populating the data (Nested maps require checking for existence at each
35+
// level)
36+
addDataNested(salesData, "North", 2023, "Widget A", 1500.0);
37+
addDataNested(salesData, "North", 2023, "Widget B", 2000.0);
38+
addDataNested(salesData, "South", 2023, "Widget A", 1200.0);
39+
addDataNested(salesData, "North", 2024, "Widget A", 1800.0);
40+
41+
// 2. Iterating to get final data (Requires 3 nested for-loops)
42+
for (Map.Entry<String, Map<Integer, Map<String, Double>>> regionEntry : salesData.entrySet()) {
43+
String region = regionEntry.getKey();
44+
for (Map.Entry<Integer, Map<String, Double>> yearEntry : regionEntry.getValue().entrySet()) {
45+
Integer year = yearEntry.getKey();
46+
for (Map.Entry<String, Double> productEntry : yearEntry.getValue().entrySet()) {
47+
String product = productEntry.getKey();
48+
Double sales = productEntry.getValue();
49+
System.out.printf("Region: %s, Year: %d, Product: %s -> Sales: $%.2f%n",
50+
region, year, product, sales);
51+
}
52+
}
53+
}
54+
}
55+
56+
private static void addDataNested(Map<String, Map<Integer, Map<String, Double>>> salesData,
57+
String region, Integer year, String product, Double sales) {
58+
salesData.computeIfAbsent(region, k -> new HashMap<>())
59+
.computeIfAbsent(year, k -> new HashMap<>())
60+
.put(product, sales);
61+
}
62+
63+
/**
64+
* Demonstrates the use of a flat HashMap with Tuple keys to manage sales data.
65+
*/
66+
private static void demonstrateTupleMap() {
67+
// Data Structure: Tuple(Region, Year, Product) -> Sales
68+
Map<Tuple, Double> salesData = new HashMap<>();
69+
70+
// 1. Populating the data (Flat map is much simpler)
71+
salesData.put(Tuple.of("North", 2023, "Widget A"), 1500.0);
72+
salesData.put(Tuple.of("North", 2023, "Widget B"), 2000.0);
73+
salesData.put(Tuple.of("South", 2023, "Widget A"), 1200.0);
74+
salesData.put(Tuple.of("North", 2024, "Widget A"), 1800.0);
75+
76+
// 2. Iterating to get final data (Single loop!)
77+
for (Map.Entry<Tuple, Double> entry : salesData.entrySet()) {
78+
Tuple key = entry.getKey();
79+
String region = key.get(0);
80+
Integer year = key.get(1);
81+
String product = key.get(2);
82+
Double sales = entry.getValue();
83+
84+
System.out.printf("Region: %s, Year: %d, Product: %s -> Sales: $%.2f%n",
85+
region, year, product, sales);
86+
}
87+
}
88+
}

0 commit comments

Comments
 (0)