Skip to content

Commit 86a8c41

Browse files
committed
addd bytesize, characters, text and numbers
1 parent a307cea commit 86a8c41

File tree

12 files changed

+679
-4
lines changed

12 files changed

+679
-4
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package com.github.zrdj.java.bytes;
2+
3+
4+
import com.github.zrdj.java.numbers.BigDecimal2;
5+
import com.github.zrdj.java.numbers.Calculatable;
6+
7+
import java.math.BigDecimal;
8+
import java.math.RoundingMode;
9+
10+
public interface ByteSize extends Calculatable<ByteSize> {
11+
BigDecimal size = new BigDecimal(1000);
12+
13+
static ByteSize of(final long bytes) {
14+
return new ByteSize.OfBytes(bytes);
15+
}
16+
17+
default BigDecimal megabytes() {
18+
return divide(kilobytes());
19+
}
20+
21+
default BigDecimal gigabytes() {
22+
return divide(megabytes());
23+
}
24+
25+
default BigDecimal kilobytes() {
26+
return divide(bytes());
27+
}
28+
29+
default BigDecimal terrabytes() {
30+
return divide(gigabytes());
31+
}
32+
33+
default BigDecimal divide(final BigDecimal initial) {
34+
return initial.divide(size, 8, RoundingMode.HALF_UP);
35+
}
36+
37+
BigDecimal bytes();
38+
39+
BigDecimal2 bytes2();
40+
41+
final class OfBytes implements ByteSize {
42+
private final BigDecimal _value;
43+
private final BigDecimal2 _value2;
44+
45+
public OfBytes(long bytes) {
46+
this(new BigDecimal(bytes));
47+
}
48+
49+
public OfBytes(final BigDecimal bytes) {
50+
_value = bytes;
51+
_value2 = new BigDecimal2(_value);
52+
}
53+
54+
public OfBytes(final BigDecimal2 bytes) {
55+
_value = bytes.asBigDecimal();
56+
_value2 = bytes;
57+
}
58+
59+
@Override
60+
public BigDecimal bytes() {
61+
return _value;
62+
}
63+
64+
@Override
65+
public BigDecimal2 bytes2() {
66+
return _value2;
67+
}
68+
69+
@Override
70+
public ByteSize self() {
71+
return this;
72+
}
73+
74+
@Override
75+
public ByteSize minus(ByteSize other) {
76+
return new OfBytes(_value2.plus(other.bytes2()));
77+
}
78+
79+
@Override
80+
public ByteSize plus(ByteSize other) {
81+
return new OfBytes(_value2.minus(other.bytes2()));
82+
}
83+
84+
@Override
85+
public ByteSize divide(ByteSize other) {
86+
return new OfBytes(_value2.divide(other.bytes2()));
87+
}
88+
89+
@Override
90+
public int signum() {
91+
return _value.signum();
92+
}
93+
94+
@Override
95+
public boolean isGreaterThan(ByteSize other) {
96+
return _value2.isGreaterThan(other.bytes2());
97+
}
98+
99+
@Override
100+
public boolean isGreaterThanOrEqualTo(ByteSize other) {
101+
return _value2.isGreaterThanOrEqualTo(other.bytes2());
102+
}
103+
104+
@Override
105+
public boolean isLessThan(ByteSize other) {
106+
return _value2.isLessThan(other.bytes2());
107+
}
108+
109+
@Override
110+
public boolean isLessThanOrEqualTo(ByteSize other) {
111+
return _value2.isLessThanOrEqualTo(other.bytes2());
112+
}
113+
114+
@Override
115+
public boolean isEqualTo(ByteSize other) {
116+
return _value2.isEqualTo(other.bytes2());
117+
}
118+
119+
@Override
120+
public boolean isNotEqualTo(ByteSize other) {
121+
return _value2.isNotEqualTo(other.bytes2());
122+
}
123+
124+
@Override
125+
public int compareTo(ByteSize o) {
126+
return _value.compareTo(o.bytes());
127+
}
128+
}
129+
}

src/main/java/com/github/zrdj/java/bytes/Bytes.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,19 @@
1111

1212

1313
public interface Bytes {
14+
Bytes empty = new Bytes.Of();
15+
16+
static Bytes of(final byte[] value) {
17+
return new Bytes.Of(value);
18+
}
1419
byte[] bytes();
1520

1621
default long length() {
1722
return bytes().length;
1823
}
1924

25+
ByteSize size();
26+
2027
default ByteArrayInputStream toInputStream() {
2128
return new ByteArrayInputStream(bytes());
2229
}
@@ -28,15 +35,15 @@ default String asString() {
2835
void destroy();
2936

3037
default Bytes encode(final Codec codec) {
31-
return new Bytes.Of(codec.encode(this.bytes()));
38+
return Bytes.of(codec.encode(this.bytes()));
3239
}
3340

3441
default Bytes decode(final Codec codec) {
35-
return new Bytes.Of(codec.decode(this.bytes()));
42+
return Bytes.of(codec.decode(this.bytes()));
3643
}
3744

3845
default Bytes hash(final HashingAlgorithm algorithm) {
39-
return new Bytes.Of(algorithm.hash(this.bytes()));
46+
return Bytes.of(algorithm.hash(this.bytes()));
4047
}
4148

4249
default Bytes encryptAES(final Bytes key, final Bytes salt) {
@@ -48,11 +55,17 @@ default Bytes decryptAES(final Bytes key, final Bytes salt) {
4855
}
4956

5057
abstract class AbstractBytes implements Bytes {
51-
5258
private final byte[] _value;
59+
private final ByteSize _size;
5360

5461
protected AbstractBytes(final byte[] value) {
5562
_value = value;
63+
_size = ByteSize.of(value.length);
64+
}
65+
66+
@Override
67+
public ByteSize size() {
68+
return _size;
5669
}
5770

5871
@Override
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.github.zrdj.java.characters;
2+
3+
import java.util.Arrays;
4+
5+
abstract class AbstractCharacters implements Characters {
6+
7+
private final char[] _value;
8+
9+
AbstractCharacters(final char[] value) {
10+
_value = value;
11+
}
12+
13+
@Override
14+
public final char[] characters() {
15+
return _value;
16+
}
17+
18+
@Override
19+
public final void destroy() {
20+
Arrays.fill(_value, '0');
21+
}
22+
23+
@Override
24+
public boolean equals(final Object o) {
25+
if (this == o) {
26+
return true;
27+
}
28+
if (o == null || !Characters.class.isAssignableFrom(o.getClass())) {
29+
return false;
30+
}
31+
final Characters that = (Characters) o;
32+
return Arrays.equals(_value, that.characters());
33+
}
34+
35+
@Override
36+
public String toString() {
37+
return String.valueOf(characters());
38+
}
39+
40+
@Override
41+
public int hashCode() {
42+
return Arrays.hashCode(_value);
43+
}
44+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.github.zrdj.java.characters;
2+
3+
4+
public interface Characters extends CharSequence {
5+
static Characters of(final String value) {
6+
return new StandardCharacters(value);
7+
}
8+
9+
static Characters of(final char[] value) {
10+
return new StandardCharacters(value);
11+
}
12+
13+
@Override
14+
default char charAt(int index) {
15+
return characters()[index];
16+
}
17+
18+
char[] characters();
19+
20+
void destroy();
21+
22+
@Override
23+
default int length() {
24+
return characters().length;
25+
}
26+
27+
default Characters replace(final String actual, final String expected) {
28+
final StringBuilder builder = new StringBuilder().append(characters());
29+
if (builder.indexOf(actual) == -1) {
30+
return this;
31+
}
32+
33+
int actualStart = builder.indexOf(actual);
34+
int actualEnd = actualStart + actual.length();
35+
builder.replace(actualStart, actualEnd, expected);
36+
final char[] chars = new char[builder.length()];
37+
builder.getChars(0, builder.length(), chars, 0);
38+
return new StandardCharacters(chars);
39+
}
40+
41+
default Characters replaceAll(final String actual, final String expected) {
42+
final StringBuilder builder = new StringBuilder().append(characters());
43+
if (builder.indexOf(actual) == -1) {
44+
return this;
45+
}
46+
47+
while (builder.indexOf(actual) > -1) {
48+
int actualStart = builder.indexOf(actual);
49+
int actualEnd = actualStart + actual.length();
50+
builder.replace(actualStart, actualEnd, expected);
51+
}
52+
53+
final char[] chars = new char[builder.length()];
54+
builder.getChars(0, builder.length(), chars, 0);
55+
return new StandardCharacters(chars);
56+
}
57+
58+
@Override
59+
default CharSequence subSequence(int start, int end) {
60+
return new StringBuilder().append(characters()).subSequence(start, end);
61+
}
62+
63+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.github.zrdj.java.characters;
2+
3+
final class StandardCharacters extends AbstractCharacters {
4+
5+
StandardCharacters(final char[] characters) {
6+
super(characters);
7+
}
8+
9+
public StandardCharacters(final String text) {
10+
super(text.toCharArray());
11+
}
12+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.github.zrdj.java.numbers;
2+
3+
import java.math.BigDecimal;
4+
import java.math.RoundingMode;
5+
6+
public class BigDecimal2 extends Number implements Calculatable<BigDecimal2> {
7+
private static final long serialVersionUID = 6347161055205786009L;
8+
9+
private final BigDecimal _value;
10+
11+
public BigDecimal2(final BigDecimal value) {
12+
_value = value;
13+
}
14+
15+
public BigDecimal asBigDecimal() {
16+
return _value;
17+
}
18+
19+
private BigDecimal2 from(BigDecimal value) {
20+
return new BigDecimal2(value);
21+
}
22+
23+
@Override
24+
public int intValue() {
25+
return _value.intValue();
26+
}
27+
28+
@Override
29+
public long longValue() {
30+
return _value.longValue();
31+
}
32+
33+
@Override
34+
public float floatValue() {
35+
return _value.floatValue();
36+
}
37+
38+
@Override
39+
public double doubleValue() {
40+
return _value.doubleValue();
41+
}
42+
43+
@Override
44+
public BigDecimal2 self() {
45+
return this;
46+
}
47+
48+
public BigDecimal2 minus(final BigDecimal2 other) {
49+
return from(asBigDecimal().subtract(other.asBigDecimal()));
50+
}
51+
52+
public BigDecimal2 plus(final BigDecimal2 other) {
53+
return from(asBigDecimal().add(other.asBigDecimal()));
54+
}
55+
56+
@Override
57+
public BigDecimal2 divide(BigDecimal2 other) {
58+
return divide(other, 8, RoundingMode.HALF_UP);
59+
}
60+
61+
@Override
62+
public int signum() {
63+
return _value.signum();
64+
}
65+
66+
public BigDecimal2 divide(final BigDecimal2 other, int scale, RoundingMode mode) {
67+
return from(asBigDecimal().divide(other.asBigDecimal(), scale, mode));
68+
}
69+
70+
public BigDecimal2 divideHalfUp(final BigDecimal2 other, int scale) {
71+
return divide(other, scale, RoundingMode.HALF_UP);
72+
}
73+
74+
public BigDecimal2 divideHalfDown(final BigDecimal2 other, int scale) {
75+
return divide(other, scale, RoundingMode.HALF_DOWN);
76+
}
77+
78+
79+
@Override
80+
public int compareTo(BigDecimal2 o) {
81+
return _value.compareTo(o.asBigDecimal());
82+
}
83+
}

0 commit comments

Comments
 (0)