-
Notifications
You must be signed in to change notification settings - Fork 210
/
Specification.java
131 lines (111 loc) · 3.79 KB
/
Specification.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
129
130
131
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p>
* http:https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.qlangtech.tis.coredefine.module.action;
import org.apache.commons.lang.StringUtils;
import java.util.Objects;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 规格
*
* @author 百岁([email protected])
* @date 2020/04/13
*/
public class Specification {
private static final Pattern p = Pattern.compile("(\\d+(\\.\\d)?)(([mGM]|Mi)?)");
public static Specification parse(String val) {
Matcher m = p.matcher(val);
if (!m.matches()) {
throw new IllegalArgumentException("val:" + val + " is not match the pattern:" + p);
}
Specification s = new Specification();
s.setVal(Float.parseFloat(m.group(1)));
s.setUnit(m.group(3));
return s;
}
private float val;
private String unit;
public float getVal() {
return val;
}
public boolean isUnitEmpty() {
return StringUtils.isEmpty(this.unit);
}
public void setVal(float val) {
this.val = val;
}
public int normalizeMemory() {
return this.normalizeMemory(Optional.empty());
}
/**
* 归一化内存规格,单位:兆
*
* @return
*/
public int normalizeMemory(Optional<Integer> proportion) {
float result = 0;
if ("Mi".equals(this.getUnit()) || "M".equals(this.getUnit())) {
result = this.getVal();
} else if ("G".equals(this.getUnit())) {
result = this.getVal() * 1024;
} else {
throw new IllegalStateException("invalid memory unit:" + this.getUnit());
}
final float r = result;
return proportion.map((p) -> {
if (p < 1 || p > 100) {
throw new IllegalArgumentException("proportion:" + p + " is invalid");
}
return (int) (r * p / 100);
}).orElse((int) result);
}
public String literalVal() {
return this.getVal() + this.getUnit();
}
public int normalizeCPU() {
// d.setCpuRequest(Specification.parse("300m"));
// d.setCpuLimit(Specification.parse("2"));
float result = 0;
if ("m".equals(this.getUnit())) {
result = this.getVal();
} else if (this.isUnitEmpty()) {
result = this.getVal() * 1024;
} else {
throw new IllegalStateException("invalid cpu unit:" + this.getUnit());
}
return (int) result;
}
public boolean memoryBigThan(Specification spec) {
Objects.requireNonNull(spec, "param spec can not be null");
return this.normalizeMemory() > spec.normalizeMemory();
}
public boolean cpuBigThan(Specification spec) {
Objects.requireNonNull(spec, "param spec can not be null");
return this.normalizeCPU() > spec.normalizeCPU();
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
public String toString() {
return this.val + this.unit;
}
}