-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayComponents.java
110 lines (95 loc) · 2.75 KB
/
ArrayComponents.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
package lastYear;
import java.util.List;
public class ArrayComponents {
private String proclamation;
private String name;
private List<String> elements;
private int numberElements;
private int capacity;
private boolean formArray;
private String type;
public ArrayComponents(String proclamation, String name, String type, List<String> elements, boolean formArray) {
this.proclamation = proclamation;
this.name = new String(name);
this.type = new String(type);
this.elements = elements;
this.formArray = formArray;
numberElements(formArray);
capacity(formArray);
}
public void capacityFirstMethod() {
final int mandatoryBytes = 8 + 16 + 4;
int sum = 0;
int i = 1;
for (String item : elements) {
if (i == 1) {
sum += mandatoryBytes + Integer.parseInt(item) * bytesType();
sum = rounding(sum);
++i;
} else {
int temp = Integer.parseInt(item) * sum;
sum = temp + mandatoryBytes;
sum = rounding(sum);
}
}
capacity = sum;
}
public void capacitySecondMethod() {
final int mandatoryBytes = 8 + 16 + 4;
int sum = mandatoryBytes + (elements.size() * bytesType());
capacity = rounding(sum);
}
public void capacity(boolean formArray) {
if (formArray) {
capacityFirstMethod();
} else
capacitySecondMethod();
}
public void numberElements(boolean formArray) {
if (formArray) {
numberFirstMethod();
} else
numberSecondMethod();
}
public void numberFirstMethod() {
numberElements = 1;
for (String item : elements) {
numberElements *= Integer.parseInt(item);
}
}
public void numberSecondMethod() {
numberElements = elements.size();
}
public int bytesType() {
if (type.matches("int|float")) {
return 4;
}
if (type.matches("short|char")) {
return 2;
}
if (type.matches("double|long|")) {
return 8;
}
return 1;
}
public int rounding(int number) {
if (number % 8 != 0) {
int temp = number / 8;
temp = (temp * 8) - number;
return number + (8 + temp);
}
return number;
}
public String getName() {
return name;
}
public int getNumberEl() {
return numberElements;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(proclamation).append(" - ").append(capacity);
return sb.toString();
}
}