Skip to content

Commit 07a9aed

Browse files
committed
Add validators
1 parent 2a58a69 commit 07a9aed

File tree

5 files changed

+182
-0
lines changed

5 files changed

+182
-0
lines changed

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## v0.3.0
44

5+
* better type hierarchy with read-only and mutable types
6+
* add validator feature
7+
58
## v0.2.0
69

710
initial version

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,32 @@ public Bytes transform(BytesTransformer transformer) {
690690
return factory.wrap(transformer.transform(internalArray(), isMutable()), byteOrder);
691691
}
692692

693+
/* VALIDATORS ***************************************************************************************************/
694+
695+
/**
696+
* Checks the content of each byte for 0 values
697+
*
698+
* @return true if not empty and only contains zero byte values
699+
*/
700+
public boolean validateNotOnlyZeros() {
701+
return !validate(BytesValidators.onlyOf((byte) 0));
702+
}
703+
704+
/**
705+
* Applies all given validators and returns true if all of them return true
706+
*
707+
* @param bytesValidators array of validators to check against the byte array
708+
* @return true if all validators return true
709+
*/
710+
public boolean validate(BytesValidator... bytesValidators) {
711+
Objects.requireNonNull(bytesValidators);
712+
boolean valid = true;
713+
for (BytesValidator bytesValidator : bytesValidators) {
714+
valid &= bytesValidator.validate(internalArray());
715+
}
716+
return valid;
717+
}
718+
693719
/* ATTRIBUTES ************************************************************************************************/
694720

695721
/**
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package at.favre.lib.bytes;
2+
3+
/**
4+
* Interface for validating byte arrays
5+
*/
6+
public interface BytesValidator {
7+
8+
/**
9+
* Validates given byte array
10+
*
11+
* @param byteArrayToValidate array, must not be altered, only read
12+
* @return true if validation is successful, false otherwise
13+
*/
14+
boolean validate(byte[] byteArrayToValidate);
15+
16+
/**
17+
* Validates for specific array length
18+
*/
19+
final class Length implements BytesValidator {
20+
enum Mode {
21+
SMALLER_THAN, GREATER_THAN, EXACT
22+
}
23+
24+
private final int refLength;
25+
private final Mode mode;
26+
27+
public Length(int refLength, Mode mode) {
28+
this.refLength = refLength;
29+
this.mode = mode;
30+
}
31+
32+
@Override
33+
public boolean validate(byte[] byteArrayToValidate) {
34+
switch (mode) {
35+
case GREATER_THAN:
36+
return byteArrayToValidate.length > refLength;
37+
case SMALLER_THAN:
38+
return byteArrayToValidate.length < refLength;
39+
default:
40+
case EXACT:
41+
return byteArrayToValidate.length == refLength;
42+
}
43+
}
44+
}
45+
46+
47+
/**
48+
* Checks if a byte array contains only the same value
49+
*/
50+
final class IdenticalContent implements BytesValidator {
51+
final byte refByte;
52+
53+
54+
IdenticalContent(byte refByte) {
55+
this.refByte = refByte;
56+
}
57+
58+
@Override
59+
public boolean validate(byte[] byteArrayToValidate) {
60+
for (byte b : byteArrayToValidate) {
61+
if (b != refByte) {
62+
return false;
63+
}
64+
}
65+
return true;
66+
}
67+
}
68+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package at.favre.lib.bytes;
2+
3+
/**
4+
* Util and easy access for {@link BytesValidators}
5+
*/
6+
public class BytesValidators {
7+
/**
8+
* Checks the length of a byte array
9+
*
10+
* @param value to check against
11+
* @return true if longer than given value
12+
*/
13+
public static BytesValidator longerThan(int value) {
14+
return new BytesValidator.Length(value, BytesValidator.Length.Mode.GREATER_THAN);
15+
}
16+
17+
public static BytesValidator shorterThan(int value) {
18+
return new BytesValidator.Length(value, BytesValidator.Length.Mode.SMALLER_THAN);
19+
}
20+
21+
public static BytesValidator exactLength(int value) {
22+
return new BytesValidator.Length(value, BytesValidator.Length.Mode.EXACT);
23+
}
24+
25+
public static BytesValidator onlyOf(byte value) {
26+
return new BytesValidator.IdenticalContent(value);
27+
}
28+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2017 Patrick Favre-Bulle
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
package at.favre.lib.bytes;
23+
24+
import org.junit.Test;
25+
26+
import static org.junit.Assert.assertFalse;
27+
import static org.junit.Assert.assertTrue;
28+
29+
public class BytesValidatorTest extends ABytesTest {
30+
31+
@Test
32+
public void testOnlyOfValidator() throws Exception {
33+
assertFalse(Bytes.allocate(0).validateNotOnlyZeros());
34+
assertFalse(Bytes.allocate(2).validateNotOnlyZeros());
35+
assertTrue(Bytes.wrap(example_bytes_seven).validateNotOnlyZeros());
36+
assertTrue(Bytes.wrap(new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 1}).validateNotOnlyZeros());
37+
assertTrue(Bytes.random(128).validateNotOnlyZeros());
38+
39+
assertTrue(Bytes.allocate(0).validate(BytesValidators.onlyOf((byte) 0)));
40+
assertTrue(Bytes.wrap(new byte[]{1, 1, 1, 1, 1, 1}).validate(BytesValidators.onlyOf((byte) 1)));
41+
}
42+
43+
@Test
44+
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)));
48+
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)));
52+
53+
assertFalse(Bytes.allocate(0).validate(BytesValidators.exactLength(1)));
54+
assertTrue(Bytes.allocate(1).validate(BytesValidators.exactLength(1)));
55+
assertFalse(Bytes.allocate(2).validate(BytesValidators.exactLength(1)));
56+
}
57+
}

0 commit comments

Comments
 (0)