Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BitSetReadOnlyUnbackedObservableList should implement indexOf #1541

Merged
merged 4 commits into from Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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
@@ -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)));
}
}