1 package com.guinetik.corefun.examples;
2
3 import com.guinetik.corefun.Computable;
4
5
6
7
8
9
10
11
12 public class ComputableExample {
13
14 public static void main(String[] args) {
15 System.out.println("=== Computable<T> Examples ===\n");
16
17 basicTransformations();
18 validationAndFiltering();
19 combiningValues();
20 dataProcessingPipeline();
21 }
22
23
24
25
26 static void basicTransformations() {
27 System.out.println("--- Basic Transformations ---");
28
29
30 Computable<String> name = Computable.of("hello");
31 System.out.println("Original: " + name.getValue());
32
33
34 Computable<Integer> length = name.map(String::length);
35 System.out.println("Length: " + length.getValue());
36
37
38 Computable<String> result = Computable.of(" hello world ")
39 .map(String::trim)
40 .map(String::toUpperCase)
41 .map(s -> s.replace(" ", "_"));
42 System.out.println("Transformed: " + result.getValue());
43
44
45 Computable<String> greeting = Computable.of("World")
46 .flatMap(n -> Computable.of("Hello, " + n + "!"));
47 System.out.println("Greeting: " + greeting.getValue());
48
49 System.out.println();
50 }
51
52
53
54
55 static void validationAndFiltering() {
56 System.out.println("--- Validation and Filtering ---");
57
58 Computable<Integer> age = Computable.of(25);
59
60
61 boolean isAdult = age.isValid(a -> a >= 18);
62 System.out.println("Age " + age.getValue() + " is adult: " + isAdult);
63
64 boolean isTeenager = Computable.of(15).isValid(a -> a >= 13 && a < 20);
65 System.out.println("Age 15 is teenager: " + isTeenager);
66
67
68 Computable<Integer> minAge = Computable.of(15)
69 .filter(a -> a >= 18, () -> 18);
70 System.out.println("Minimum age (from 15): " + minAge.getValue());
71
72 Computable<Integer> keepAge = Computable.of(25)
73 .filter(a -> a >= 18, () -> 18);
74 System.out.println("Minimum age (from 25): " + keepAge.getValue());
75
76
77 Computable<String> nullableValue = Computable.of((String) null);
78 String safeValue = nullableValue.orElse("default");
79 System.out.println("Nullable with orElse: " + safeValue);
80
81 System.out.println();
82 }
83
84
85
86
87 static void combiningValues() {
88 System.out.println("--- Combining Values ---");
89
90
91 Computable<Integer> a = Computable.of(10);
92 Computable<Integer> b = Computable.of(5);
93
94 Computable<Integer> sum = a.combine(b, Integer::sum);
95 System.out.println("10 + 5 = " + sum.getValue());
96
97 Computable<Integer> product = a.combine(b, (x, y) -> x * y);
98 System.out.println("10 * 5 = " + product.getValue());
99
100
101 Computable<String> firstName = Computable.of("John");
102 Computable<String> lastName = Computable.of("Doe");
103 Computable<String> fullName = firstName.combine(lastName, (f, l) -> f + " " + l);
104 System.out.println("Full name: " + fullName.getValue());
105
106
107 Computable<String> word = Computable.of("world");
108 String reduced = word.reduce("Hello, ", (acc, w) -> acc + w + "!");
109 System.out.println("Reduced: " + reduced);
110
111 System.out.println();
112 }
113
114
115
116
117 static void dataProcessingPipeline() {
118 System.out.println("--- Data Processing Pipeline ---");
119
120
121 String rawInput = " john.doe@example.com ";
122
123 Computable<String> processedEmail = Computable.of(rawInput)
124 .peek(v -> System.out.println("Raw input: '" + v + "'"))
125 .map(String::trim)
126 .peek(v -> System.out.println("After trim: '" + v + "'"))
127 .map(String::toLowerCase)
128 .peek(v -> System.out.println("After lowercase: '" + v + "'"))
129 .filter(e -> e.contains("@"), () -> "invalid@email.com")
130 .peek(v -> System.out.println("After validation: '" + v + "'"));
131
132 System.out.println("Final result: " + processedEmail.getValue());
133
134
135 System.out.println("\nProcessing user record:");
136 User rawUser = new User(" Bob ", 25, " admin ");
137
138 Computable<User> cleanUser = Computable.of(rawUser)
139 .map(u -> new User(u.name.trim(), u.age, u.role.trim().toLowerCase()));
140
141 System.out.println("Cleaned user: " + cleanUser.getValue());
142
143
144 boolean isValidAdmin = cleanUser
145 .isValid(u -> u.name.length() > 0)
146 && cleanUser.isValid(u -> u.age >= 18)
147 && cleanUser.isValid(u -> "admin".equals(u.role));
148
149 System.out.println("Is valid admin: " + isValidAdmin);
150
151 System.out.println();
152 }
153
154
155 static class User {
156 final String name;
157 final int age;
158 final String role;
159
160 User(String name, int age, String role) {
161 this.name = name;
162 this.age = age;
163 this.role = role;
164 }
165
166 @Override
167 public String toString() {
168 return "User{name='" + name + "', age=" + age + ", role='" + role + "'}";
169 }
170 }
171 }