Skip to content

Commit

Permalink
BitSetReadOnlyUnbackedObservableList should implement indexOf (#1541)
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinayagarwal committed Mar 7, 2024
1 parent 7a2e185 commit 99ee0c0
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2013, 2022, ControlsFX
* Copyright (c) 2013, 2024, ControlsFX
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -344,6 +344,43 @@ public BitSetReadOnlyUnbackedObservableList(BitSet bitset) {
return -1;
}

@Override public int indexOf(Object obj) {
if (!(obj instanceof Number)) {
return -1;
}
Number n = (Number) obj;
int index = n.intValue();
if (!bitset.get(index)) {
return -1;
}

// is left most bit
if (index == 0) {
return 0;
}

// is right most bit
if (index == bitset.length() - 1) {
return size() - 1;
}

// count right bit
if (index > bitset.length() / 2) {
int count = 1;
for (int i = bitset.nextSetBit(index+1); i >= 0; i = bitset.nextSetBit(i+1)) {
count++;
}
return size() - count;
}

// count left bit
int count = 0;
for (int i = bitset.previousSetBit(index-1); i >= 0; i = bitset.previousSetBit(i-1)) {
count++;
}
return count;
}

@Override public int size() {
return bitset.cardinality();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) 2022, ControlsFX
* Copyright (c) 2022, 2024, ControlsFX
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -31,7 +31,6 @@
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import java.util.HashMap;
Expand All @@ -40,6 +39,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;

public class CheckBitSetModelBaseTest {
Expand Down Expand Up @@ -139,4 +139,14 @@ public void testClearChecksCallsListenerOnce() {
model.clearChecks();
assertEquals(1, count.get());
}

@Test
// needs junit 5
// @RepeatedTest(value = 10, failureThreshold = 2)
public void testSingleCheckAddsItToIndicesList() {
ObservableList<Integer> checkedIndicesList = model.getCheckedIndices();
assertTrue(checkedIndicesList.isEmpty());
model.check(ROW_3_VALUE);
assertNotEquals(-1, checkedIndicesList.indexOf(model.getItemIndex(ROW_3_VALUE)));
}
}

0 comments on commit 99ee0c0

Please sign in to comment.