-
Notifications
You must be signed in to change notification settings - Fork 1
/
ArrayBackedProperties.java
128 lines (103 loc) · 2.69 KB
/
ArrayBackedProperties.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Array-backed properties
package behavioral.iterator;
import java.util.Iterator;
import java.util.Spliterator;
import java.util.function.Consumer;
import java.util.stream.IntStream;
class SimpleCreature
{
private int strength, agility, intelligence;
public int max()
{
return Math.max(strength,
Math.max(agility, intelligence));
}
public int sum()
{
return strength+agility+intelligence;
}
public double average()
{
return sum() / 3.0;
}
// Getters and setters
public int getStrength() {
return strength;
}
public void setStrength(int strength) {
this.strength = strength;
}
public int getAgility() {
return agility;
}
public void setAgility(int agility) {
this.agility = agility;
}
public int getIntelligence() {
return intelligence;
}
public void setIntelligence(int intelligence) {
this.intelligence = intelligence;
}
}
// class masquerading as an array
// you can take various elements, group them in one object, and perform stream operations on them
class Creature implements Iterable<Integer>
{
private int [] stats = new int[3]; // array of statistics
private final int STR = 0;
public int getStrength() { return stats[STR]; }
public void setStrength(int value)
{
stats[STR] = value;
}
public int getAgility() { return stats[1]; }
public void setAgility(int value)
{
stats[1] = value;
}
public int getIntelligence() { return stats[2]; }
public void setIntelligence(int value)
{
stats[2] = value;
}
public int sum()
{
return IntStream.of(stats).sum();
}
public int max()
{
return IntStream.of(stats).max().getAsInt();
}
public double average()
{
return IntStream.of(stats).average().getAsDouble();
}
@Override
public Iterator<Integer> iterator() {
return IntStream.of(stats).iterator();
}
@Override
public void forEach(Consumer<? super Integer> action) {
for (int x : stats)
action.accept(x);
}
@Override
public Spliterator<Integer> spliterator() {
return IntStream.of(stats).spliterator();
}
}
class ArrayBackedPropertiesDemo
{
public static void main(String[] args) {
Creature creature = new Creature();
creature.setAgility(12);
creature.setIntelligence(13);
creature.setStrength(17);
System.out.println(
"Creature has a max stat of " + creature.max()
+ ", total stats = " + creature.sum()
+ " average stat = " + creature.average()
);
}
}