public int getValue(int input) {
int[] data = {1,2,3,4,5};
Preconditions.checkElementIndex(input,data.length, "Illegal Argument passed: Invalid index.");
return 0;
}
public void testArrayElement(final String[] strArray, final int indexNumber)
{
final int index = checkElementIndex(indexNumber, strArray.length, "String array index number");
}
public void givenArrayAndMsg_whenCheckElementEvalsFalse_throwsException() {
int[] numbers = { 1, 2, 3, 4, 5 };
String message = "Please check the bound of an array and retry";
assertThatThrownBy(() ->
Preconditions.checkElementIndex(6, numbers.length - 1, message))
.isInstanceOf(IndexOutOfBoundsException.class)
.hasMessageStartingWith(message).hasNoCause();
}
public void testCheckElementIndex_ok() {
assertEquals(0, Preconditions.checkElementIndex(0, 1));
assertEquals(0, Preconditions.checkElementIndex(0, 2));
assertEquals(1, Preconditions.checkElementIndex(1, 2));
}
public void testCheckElementIndex_withDesc_tooHigh() {
try {
Preconditions.checkElementIndex(1, 1, "foo");
fail();
} catch (IndexOutOfBoundsException expected) {
assertThat(expected).hasMessage("foo (1) must be less than size (1)");
}
}
public L getAt(int index) {
if (size != Integer.MAX_VALUE) {
Preconditions.checkElementIndex(index, size());
} // else no check necessary, all index values are valid
ArrayReference<? extends L> existingRef = locks.get(index);
L existing = existingRef == null ? null : existingRef.get();
if (existing != null) {
return existing;
}
L created = supplier.get();
ArrayReference<L> newRef = new ArrayReference<L>(created, index, queue);
while (!locks.compareAndSet(index, existingRef, newRef)) {
// we raced, we need to re-read and try again
existingRef = locks.get(index);
existing = existingRef == null ? null : existingRef.get();
if (existing != null) {
return existing;
}
}
drainQueue();
return created;
}
public FinalizationCodePart getFinalization(Integer i, List<List<Point>> upper_bold_p, Collection<BallotEntry> upper_b) {
BigInteger p_prime = publicParameters.getPrimeField().getP_prime();
Preconditions.checkArgument(upper_bold_p.stream().flatMap(Collection::stream)
.allMatch(point -> BigInteger.ZERO.compareTo(point.x) <= 0 &&
point.x.compareTo(p_prime) < 0 &&
BigInteger.ZERO.compareTo(point.y) <= 0 &&
point.y.compareTo(p_prime) < 0),
"All points' coordinates must be in Z_p_prime");
Preconditions.checkElementIndex(i, upper_bold_p.size());
Object[] bold_p_i = upper_bold_p.get(i).toArray();
byte[] upper_f_i = ByteArrayUtils.truncate(hash.recHash_L(bold_p_i), publicParameters.getUpper_l_f());
BallotEntry ballotEntry = upper_b.stream().filter(b -> Objects.equals(b.getI(), i)).findFirst().orElseThrow(
() -> new BallotNotFoundRuntimeException(String.format("Couldn't find any ballot for voter %d", i))
);
return new FinalizationCodePart(upper_f_i, ballotEntry.getBold_r());
}
public L getAt(int index) {
if (size != Integer.MAX_VALUE) {
Preconditions.checkElementIndex(index, size());
} // else no check necessary, all index values are valid
L existing = locks.get(index);
if (existing != null) {
return existing;
}
L created = supplier.get();
existing = locks.putIfAbsent(index, created);
return MoreObjects.firstNonNull(existing, created);
}
public void publishKeyPart(int j, EncryptionPublicKey publicKey) {
Preconditions.checkState(publicParameters != null,
"The public parameters need to have been defined first");
Preconditions.checkElementIndex(j, publicParameters.getS(),
"The index j should be lower than the number of authorities");
publicKeyParts.put(j, publicKey);
}
public void publishShuffleAndProof(int j, List<Encryption> shuffle, ShuffleProof proof) {
Preconditions.checkElementIndex(j, publicParameters.getS(),
"j needs to be within bounds");
Preconditions.checkArgument(shuffles.size() == j,
"Shuffle j can only be inserted after the previous shuffles");
Preconditions.checkArgument(shuffleProofs.size() == j,
"Shuffle proof j can only be inserted after the previous shuffle proof");
shuffles.put(j, shuffle);
shuffleProofs.put(j, proof);
}