Skip to content

Commit cf146d6

Browse files
committed
Add more create methods
1 parent bb54fc9 commit cf146d6

File tree

6 files changed

+56
-26
lines changed

6 files changed

+56
-26
lines changed

CHANGELOG

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Releases
22

3+
## v0.3.1
4+
5+
* better validation and more creation possibilities
6+
37
## v0.3.0
48

59
* better type hierarchy with read-only and mutable types

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>at.favre.lib</groupId>
88
<artifactId>bytes</artifactId>
9-
<version>0.3.0</version>
9+
<version>0.3.1</version>
1010
<packaging>jar</packaging>
1111

1212
<name>Bytes Java Primitive Tools</name>

src/main/java/at/favre/lib/bytes/Bytes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@ public Bytes transform(BytesTransformer transformer) {
749749
* @return true if not empty and only contains zero byte values
750750
*/
751751
public boolean validateNotOnlyZeros() {
752-
return !validate(BytesValidators.onlyOf((byte) 0));
752+
return validate(BytesValidators.notOnlyOf((byte) 0));
753753
}
754754

755755
/**

src/main/java/at/favre/lib/bytes/BytesValidator.java

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public interface BytesValidator {
3939
*/
4040
final class Length implements BytesValidator {
4141
enum Mode {
42-
SMALLER_THAN, GREATER_THAN, EXACT
42+
SMALLER_OR_EQ_THAN, GREATER_OR_EQ_THAN, EXACT
4343
}
4444

4545
private final int refLength;
@@ -53,10 +53,10 @@ public Length(int refLength, Mode mode) {
5353
@Override
5454
public boolean validate(byte[] byteArrayToValidate) {
5555
switch (mode) {
56-
case GREATER_THAN:
57-
return byteArrayToValidate.length > refLength;
58-
case SMALLER_THAN:
59-
return byteArrayToValidate.length < refLength;
56+
case GREATER_OR_EQ_THAN:
57+
return byteArrayToValidate.length >= refLength;
58+
case SMALLER_OR_EQ_THAN:
59+
return byteArrayToValidate.length <= refLength;
6060
default:
6161
case EXACT:
6262
return byteArrayToValidate.length == refLength;
@@ -69,21 +69,33 @@ public boolean validate(byte[] byteArrayToValidate) {
6969
* Checks if a byte array contains only the same value
7070
*/
7171
final class IdenticalContent implements BytesValidator {
72-
final byte refByte;
72+
private final byte refByte;
7373

74+
enum Mode {
75+
ONLY_OF, NONE_OF, NOT_ONLY_OF
76+
}
77+
78+
private final Mode mode;
7479

75-
IdenticalContent(byte refByte) {
80+
IdenticalContent(byte refByte, Mode mode) {
7681
this.refByte = refByte;
82+
this.mode = mode;
7783
}
7884

7985
@Override
8086
public boolean validate(byte[] byteArrayToValidate) {
8187
for (byte b : byteArrayToValidate) {
82-
if (b != refByte) {
88+
if (mode == Mode.NONE_OF && b == refByte) {
89+
return false;
90+
}
91+
if (mode == Mode.ONLY_OF && b != refByte) {
8392
return false;
8493
}
94+
if (mode == Mode.NOT_ONLY_OF && b != refByte) {
95+
return true;
96+
}
8597
}
86-
return true;
98+
return mode == Mode.NONE_OF || mode == Mode.ONLY_OF;
8799
}
88100
}
89101
}

src/main/java/at/favre/lib/bytes/BytesValidators.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,30 @@ private BytesValidators() {
3232
/**
3333
* Checks the length of a byte array
3434
*
35-
* @param value to check against
35+
* @param byteLength to check against
3636
* @return true if longer than given value
3737
*/
38-
public static BytesValidator longerThan(int value) {
39-
return new BytesValidator.Length(value, BytesValidator.Length.Mode.GREATER_THAN);
38+
public static BytesValidator atLeast(int byteLength) {
39+
return new BytesValidator.Length(byteLength, BytesValidator.Length.Mode.GREATER_OR_EQ_THAN);
4040
}
4141

42-
public static BytesValidator shorterThan(int value) {
43-
return new BytesValidator.Length(value, BytesValidator.Length.Mode.SMALLER_THAN);
42+
public static BytesValidator atMost(int byteLength) {
43+
return new BytesValidator.Length(byteLength, BytesValidator.Length.Mode.SMALLER_OR_EQ_THAN);
4444
}
4545

46-
public static BytesValidator exactLength(int value) {
47-
return new BytesValidator.Length(value, BytesValidator.Length.Mode.EXACT);
46+
public static BytesValidator exactLength(int byteLength) {
47+
return new BytesValidator.Length(byteLength, BytesValidator.Length.Mode.EXACT);
4848
}
4949

5050
public static BytesValidator onlyOf(byte value) {
51-
return new BytesValidator.IdenticalContent(value);
51+
return new BytesValidator.IdenticalContent(value, BytesValidator.IdenticalContent.Mode.ONLY_OF);
52+
}
53+
54+
public static BytesValidator notOnlyOf(byte value) {
55+
return new BytesValidator.IdenticalContent(value, BytesValidator.IdenticalContent.Mode.NOT_ONLY_OF);
56+
}
57+
58+
public static BytesValidator noneOf(byte value) {
59+
return new BytesValidator.IdenticalContent(value, BytesValidator.IdenticalContent.Mode.NONE_OF);
5260
}
5361
}

src/test/java/at/favre/lib/bytes/BytesValidatorTest.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,25 @@ public void testOnlyOfValidator() throws Exception {
3636
assertTrue(Bytes.wrap(new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 1}).validateNotOnlyZeros());
3737
assertTrue(Bytes.random(128).validateNotOnlyZeros());
3838

39-
assertTrue(Bytes.allocate(0).validate(BytesValidators.onlyOf((byte) 0)));
39+
assertTrue(Bytes.allocate(1).validate(BytesValidators.onlyOf((byte) 0)));
40+
assertFalse(Bytes.allocate(1).validate(BytesValidators.noneOf((byte) 0)));
41+
assertFalse(Bytes.allocate(1).validate(BytesValidators.onlyOf((byte) 1)));
42+
assertTrue(Bytes.allocate(1).validate(BytesValidators.noneOf((byte) 1)));
43+
assertTrue(Bytes.allocate(1).validate(BytesValidators.noneOf((byte) 1)));
44+
assertTrue(Bytes.wrap(new byte[]{1, 1, 1, 1, 0, 1}).validate(BytesValidators.notOnlyOf((byte) 1)));
45+
assertFalse(Bytes.wrap(new byte[]{1, 1, 1, 1, 1}).validate(BytesValidators.notOnlyOf((byte) 1)));
4046
assertTrue(Bytes.wrap(new byte[]{1, 1, 1, 1, 1, 1}).validate(BytesValidators.onlyOf((byte) 1)));
4147
}
4248

4349
@Test
4450
public void testLengthValidators() throws Exception {
45-
assertFalse(Bytes.allocate(0).validate(BytesValidators.longerThan(1)));
46-
assertFalse(Bytes.allocate(1).validate(BytesValidators.longerThan(1)));
47-
assertTrue(Bytes.allocate(2).validate(BytesValidators.longerThan(1)));
51+
assertFalse(Bytes.allocate(0).validate(BytesValidators.atLeast(1)));
52+
assertTrue(Bytes.allocate(1).validate(BytesValidators.atLeast(1)));
53+
assertTrue(Bytes.allocate(2).validate(BytesValidators.atLeast(1)));
4854

49-
assertFalse(Bytes.allocate(2).validate(BytesValidators.shorterThan(1)));
50-
assertFalse(Bytes.allocate(1).validate(BytesValidators.shorterThan(1)));
51-
assertTrue(Bytes.allocate(0).validate(BytesValidators.shorterThan(1)));
55+
assertFalse(Bytes.allocate(2).validate(BytesValidators.atMost(1)));
56+
assertTrue(Bytes.allocate(1).validate(BytesValidators.atMost(1)));
57+
assertTrue(Bytes.allocate(0).validate(BytesValidators.atMost(1)));
5258

5359
assertFalse(Bytes.allocate(0).validate(BytesValidators.exactLength(1)));
5460
assertTrue(Bytes.allocate(1).validate(BytesValidators.exactLength(1)));

0 commit comments

Comments
 (0)