NodeUtil { public static int binarySearchForEdge(AtomicReferenceArray<Node> childNodes, Character edgeFirstCharacter) { int low = 0; int high = childNodes.length() - 1; while (low <= high) { int mid = (low + high) >>> 1; Node midVal = childNodes.get(mid); int cmp = midVal.getIncomingEdgeFirstCharacter().compareTo(edgeFirstCharacter); if (cmp < 0) low = mid + 1; else if (cmp > 0) high = mid - 1; else return mid; } return -(low + 1); } NodeUtil(); static int binarySearchForEdge(AtomicReferenceArray<Node> childNodes, Character edgeFirstCharacter); static void ensureNoDuplicateEdges(List<Node> nodes);  }
@Test @SuppressWarnings({"NullableProblems"}) public void testBinarySearchForEdge() throws Exception { NodeFactory nodeFactory = new DefaultCharArrayNodeFactory(); Node[] nodes = new Node[] { nodeFactory.createNode("A", null, Collections.<Node>emptyList(), false), nodeFactory.createNode("B", null, Collections.<Node>emptyList(), false), nodeFactory.createNode("C", null, Collections.<Node>emptyList(), false) }; AtomicReferenceArray<Node> atomicReferenceArray = new AtomicReferenceArray<Node>(nodes); Assert.assertEquals(0, NodeUtil.binarySearchForEdge(atomicReferenceArray, 'A')); Assert.assertEquals(1, NodeUtil.binarySearchForEdge(atomicReferenceArray, 'B')); Assert.assertEquals(2, NodeUtil.binarySearchForEdge(atomicReferenceArray, 'C')); Assert.assertTrue(NodeUtil.binarySearchForEdge(atomicReferenceArray, 'D') < 0); }