private int readVInt(IntHolder pos) { byte b = this.concatenated[pos.value]; pos.value++; if (b >= 0) { return b; } else { int res = (b & 0x7F); int shift = 7; while (true) { b = this.concatenated[pos.value]; pos.value++; if (b > 0) { res = res | (b << shift); break; } else { res = res | ((b & 0x7F) << shift); shift += 7; } } return res; } }
public synchronized CandidateCounters getTask(IntHolder boundHolder) throws StopPreparingJobsException, StopResumingJobsException { while (!stackedEs.isEmpty() && stackedEs.peek().getCandidate() == this.nextConsumable) { this.nextConsumable++; CandidateCounters next = stackedEs.poll(); if (next.getNewCandidateBound() > 0) { this.minBoundToNextConsumable = Math .min(this.minBoundToNextConsumable, next.getNewCandidateBound()); } if (next.counters != null) { boundHolder.value = this.minBoundToNextConsumable; return next; } } if (this.nextConsumable >= ExplorationStep.INSERT_UNCLOSED_UP_TO_ITEM) { if (this.stackedEs.isEmpty()) { throw new StopResumingJobsException(); } else { throw new StopPreparingJobsException(); } } boundHolder.value = this.minBoundToNextConsumable; return null; }
/** * Read the name part (id or kind). * * @param p the current position. After reading, advances * to the beginning of the next name fragment. * * @param t the string buffer. * * @return the name part with resolved escape sequences. */ private String readPart(IntHolder p, String[] t) { CPStringBuilder part = new CPStringBuilder(); while (t [ p.value ] != null && !t [ p.value ].equals(".") && !t [ p.value ].equals("/") ) { if (t [ p.value ].equals(ESCAPE)) { p.value++; part.append(t [ p.value ]); } else part.append(t [ p.value ]); p.value++; } return part.toString(); }
@Test public void shouldSupportATrailingOption() throws InterruptedException, ExecutionException { DebounceOptions<Void> o0 = new DebounceOptions<Void>().executor(executor).trailing(true); DebounceOptions<Void> o1 = new DebounceOptions<Void>().executor(executor).trailing(false); IntHolder withCount = new IntHolder(0); IntHolder withoutCount = new IntHolder(0); Runnable withTrailing = Lodash.debounce(() -> { withCount.value++; } , 32, o0); Runnable withoutTrailing = Lodash.debounce(() -> { withoutCount.value++; } , 32, o1); withTrailing.run(); Assert.assertEquals(0, withCount.value); withoutTrailing.run(); Assert.assertEquals(0, withoutCount.value); o1.setTimeout().apply(() -> { Assert.assertEquals(1, withCount.value); Assert.assertEquals(0, withoutCount.value); } , 64L).get(); }
@Test public void shouldSupportAMaxWaitOption() throws InterruptedException, ExecutionException { IntHolder callCount = new IntHolder(0); DebounceOptions<Void> o = new DebounceOptions<Void>().executor(executor).maxWait(64); Runnable debounced = Lodash.debounce(() -> { callCount.value++; } , 32, o); debounced.run(); debounced.run(); Assert.assertEquals(0, callCount.value); Future<?> f1 = o.setTimeout().apply(() -> { Assert.assertEquals(1, callCount.value); debounced.run(); debounced.run(); Assert.assertEquals(1, callCount.value); } , 128L); Future<?> f2 = o.setTimeout().apply(() -> { Assert.assertEquals(2, callCount.value); } , 256L); f1.get(); f2.get(); }
@Test public void shouldQueueATrailingCallForSubsequentDebouncedCallsAfterMaxWait() throws InterruptedException, ExecutionException { IntHolder callCount = new IntHolder(0); DebounceOptions<Void> o = new DebounceOptions<Void>().executor(executor).maxWait(200); Runnable debounced = Lodash.debounce(() -> { callCount.value++; } , 200, o); debounced.run(); o.setTimeout().apply(debounced, 190L); o.setTimeout().apply(debounced, 200L); o.setTimeout().apply(debounced, 210L); o.setTimeout().apply(() -> { Assert.assertEquals(2, callCount.value); } , 500L).get(); }
@Test public void shouldInvokeTheTrailingCallWithTheCorrectArguments() throws InterruptedException, ExecutionException { IntHolder callCount = new IntHolder(0); DebounceOptions<Boolean> o = new DebounceOptions<Boolean>().executor(executor).leading(true).maxWait(64); List<Integer> actualA = new ArrayList<>(); List<String> actualB = new ArrayList<>(); BiFunction<Integer, String, Boolean> debounced = Lodash.debounce((a, b) -> { actualA.add(a); actualB.add(b); return ++callCount.value != 2; } , 32, o); while (true) { if (!debounced.apply(1, "a")) { break; } } o.setTimeout().apply(() -> { Assert.assertEquals(2, callCount.value); Assert.assertEquals(Arrays.asList(1, 1), actualA); Assert.assertEquals(Arrays.asList("a", "a"), actualB); } , 64L).get(); }
void initStack( Node start ) { Node child = start; Node parent = child.getParent( ); final Stack<IntHolder> revStack = new Stack<IntHolder>( ); while( parent != null ) { if( parent instanceof Group ) { revStack.push( new IntHolder( ( ( Group ) parent ).indexOfChild( child ) ) ); } child = parent; parent = child.getParent( ); } while( !revStack.isEmpty( ) ) { indexStack.push( revStack.pop( ) ); } }
public Counters nextPreprocessed(PerItemTopKCollector collector, IntHolder candidateHolder, IntHolder boundHolder) { if (this.candidates == null) { candidateHolder.value = -1; return null; } int candidate = this.candidates.next(); if (candidate < 0) { candidateHolder.value = -1; return null; } else { candidateHolder.value = candidate; return this.prepareExploration(candidate, collector, boundHolder); } }
public ExplorationStep resumeExploration(Counters candidateCounts, int candidate, PerItemTopKCollector collector, int countersMinSupportVerification) { if (ultraVerbose) { System.err.format("{\"time\":\"%1$tY/%1$tm/%1$td %1$tk:%1$tM:%1$tS\",\"thread\":%2$d,\"resume_candidate\":%3$d}\n", Calendar.getInstance(), Thread.currentThread().getId(), candidate); } // check that the counters we made are also ok for all items < candidate if (candidateCounts.getMinSupport() > countersMinSupportVerification && candidateCounts.getMinSupport() > this.counters.getMinSupport()) { CountersHandler.increment(TopPICounters.RedoCounters); candidateCounts = prepareExploration(candidate, collector, new IntHolder(countersMinSupportVerification), true); } if (candidate < INSERT_UNCLOSED_UP_TO_ITEM) { candidateCounts.raiseMinimumSupport(collector); if (LOG_EPSILONS) { synchronized (System.out) { if (this.counters != null && this.counters.getPattern() != null && this.counters.getPattern().length == 0) { System.out.println(candidate + " " + candidateCounts.getMinSupport()); } } } } Dataset dataset = this.datasetProvider.getDatasetForSupportThreshold(candidateCounts.getMinSupport()); ExplorationStep next = new ExplorationStep(this, dataset, candidate, candidateCounts, dataset.getSupport(candidate)); return next; }
/** {@inheritDoc} */ public int get_long() throws TypeMismatch { try { return ((IntHolder) holder).value; } catch (ClassCastException cex) { TypeMismatch m = new TypeMismatch(); m.initCause(cex); throw m; } }
/** {@inheritDoc} */ public void insert_long(int a_x) throws InvalidValue, TypeMismatch { try { ((IntHolder) holder).value = a_x; valueChanged(); } catch (ClassCastException cex) { TypeMismatch t = new TypeMismatch(); t.initCause(cex); throw t; } }
/** {@inheritDoc} */ public void insert_ulong(int a_x) throws InvalidValue, TypeMismatch { try { ((IntHolder) holder).value = a_x; valueChanged(); } catch (ClassCastException cex) { TypeMismatch t = new TypeMismatch(); t.initCause(cex); throw t; } }
/** * Convert the string name representation into the array name * representation. See {@link #toString(NameComponent)} for the * description of this format. * * @param a_name the string form of the name. * * @return the array form of the name. * * @throws InvalidName if the name cannot be parsed. */ public NameComponent[] toName(String a_name) throws InvalidName { ArrayList components = new ArrayList(); StringTokenizer st = new StringTokenizer(a_name, "./\\", true); // Create the buffer array, reserving the last element for null. String[] n = new String[ st.countTokens() + 1 ]; int pp = 0; while (st.hasMoreTokens()) n [ pp++ ] = st.nextToken(); IntHolder p = new IntHolder(); NameComponent node = readNode(p, n); while (node != null) { components.add(node); node = readNode(p, n); } NameComponent[] name = new NameComponent[ components.size() ]; for (int i = 0; i < name.length; i++) { name [ i ] = (NameComponent) components.get(i); } NameValidator.check(name); return name; }
/** * Assert the end of the current name component. */ private void assertEndOfNode(IntHolder p, String[] t) throws InvalidName { if (t [ p.value ] != null) if (!t [ p.value ].equals("/")) throw new InvalidName("End of node expected at token " + p.value); }
/** {@inheritDoc} */ public int extract_long() throws BAD_OPERATION { // CORBA long = java int. check(TCKind._tk_long); return ((IntHolder) has).value; }
/** {@inheritDoc} */ public int extract_ulong() throws BAD_OPERATION { // IntHolder also holds ulongs. check(TCKind._tk_ulong); return ((IntHolder) has).value; }
@Test public void testWait() { Thread thread = new Thread(new Runnable() { public void run() { Process.wait(NOTEPAD_PROC_NAME); } }); thread.start(); sleep(2000); Assert.assertTrue(thread.isAlive()); // run notepad int pid = runNotepad(); Assert.assertFalse(thread.isAlive()); // close notepad Process.close(pid); sleep(500); Assert.assertFalse(Process.exists(pid)); final IntHolder timeHolder = new IntHolder(); thread = new Thread(new Runnable() { public void run() { long start = System.currentTimeMillis(); Process.wait(NOTEPAD_PROC_NAME, 2); long end = System.currentTimeMillis(); timeHolder.value = (int) (end - start); } }); thread.start(); sleep(3000); Assert.assertFalse(thread.isAlive()); Assert.assertTrue("Assert timeHolder.value >= 2000, but actual is " + timeHolder.value + ".", timeHolder.value >= 2000); Assert.assertTrue("Assert timeHolder.value <= 3000, but actual is " + timeHolder.value + ".", timeHolder.value <= 3000); }
/** * Replaces a `new oldClass()` expression in the target class or methods with `new newClass(); * oldClass and one of newClass or code must be specified. * * @param oldClass Type of new expression to replace * @param newClass (optional) New type to construct * @param code (optional) $_ = new newClass(); */ @Patch( requiredAttributes = "oldClass", emptyConstructor = false ) public void replaceNewExpression(Object o, Map<String, String> attributes) throws CannotCompileException, NotFoundException { final String type = attributes.get("oldClass"); final String code = attributes.get("code"); final String clazz = attributes.get("newClass"); if (code == null && clazz == null) { throw new NullPointerException("Must give code or class"); } final String newInitializer = code == null ? "$_ = new " + clazz + "();" : code; final Set<CtBehavior> allBehaviours = new HashSet<>(); if (o instanceof CtClass) { CtClass ctClass = (CtClass) o; allBehaviours.addAll(Arrays.asList(ctClass.getDeclaredBehaviors())); } else { allBehaviours.add((CtBehavior) o); } final IntHolder done = new IntHolder(); for (CtBehavior ctBehavior : allBehaviours) { ctBehavior.instrument(new ExprEditor() { @Override public void edit(NewExpr e) throws CannotCompileException { if (e.getClassName().equals(type)) { e.replace(newInitializer); done.value++; } } }); } if (done.value == 0) { PatcherLog.error("No new expressions found for replacement."); } }
@Test public void shouldDebounceAFunction() throws InterruptedException, ExecutionException { IntHolder callCount = new IntHolder(0); Function<Character, Character> debounced = Lodash.debounce(v -> { callCount.value++; return v; } , 32, new DebounceOptions<Character>().executor(executor)); Character[] results = new Character[] { debounced.apply('a'), debounced.apply('b'), debounced.apply('c') }; Assert.assertArrayEquals(new Character[] { null, null, null }, results); Assert.assertEquals(0, callCount.value); Future<?> f1 = executor.schedule(() -> { Assert.assertEquals(1, callCount.value); Character[] results2 = new Character[] { debounced.apply('a'), debounced.apply('b'), debounced.apply('c') }; Assert.assertArrayEquals(new Character[] { 'c', 'c', 'c' }, results2); Assert.assertEquals(1, callCount.value); } , 128, TimeUnit.MILLISECONDS); Future<?> f2 = executor.schedule(() -> { Assert.assertEquals(2, callCount.value); } , 256, TimeUnit.MILLISECONDS); f1.get(); f2.get(); }
@Test public void shouldNotImmediatelyCallFuncWhenWaitIs0() throws InterruptedException, ExecutionException { IntHolder callCount = new IntHolder(0); DebounceOptions<Void> o = new DebounceOptions<Void>().executor(executor); Runnable debounced = Lodash.debounce(() -> { callCount.value++; } , 0, o); debounced.run(); debounced.run(); Assert.assertEquals(0, callCount.value); o.setTimeout().apply(() -> { Assert.assertEquals(1, callCount.value); } , 5L).get(); }
@Test public void shouldSupportALeadingOption() throws InterruptedException, ExecutionException { DebounceOptions<Void> o0 = new DebounceOptions<Void>().executor(executor).leading(true); DebounceOptions<Void> o1 = new DebounceOptions<Void>().executor(executor).leading(true).trailing(true); IntHolder callCount0 = new IntHolder(0); IntHolder callCount1 = new IntHolder(0); Runnable withLeading = Lodash.debounce(() -> { callCount0.value++; } , 32, o0); Runnable withLeadingAndTrailing = Lodash.debounce(() -> { callCount1.value++; } , 32, o1); withLeading.run(); Assert.assertEquals(1, callCount0.value); withLeadingAndTrailing.run(); withLeadingAndTrailing.run(); Assert.assertEquals(1, callCount1.value); o1.setTimeout().apply(() -> { Assert.assertEquals(1, callCount0.value); Assert.assertEquals(2, callCount1.value); withLeading.run(); Assert.assertEquals(2, callCount0.value); } , 64L).get(); }
@Test public void shouldSupportMaxWaitInATightLoop() throws InterruptedException, ExecutionException { int limit = 320; DebounceOptions<Void> o0 = new DebounceOptions<Void>().executor(executor).maxWait(128); DebounceOptions<Void> o1 = new DebounceOptions<Void>().executor(executor); IntHolder withCount = new IntHolder(0); IntHolder withoutCount = new IntHolder(0); Runnable withMaxWait = Lodash.debounce(() -> { withCount.value++; } , 64, o0); Runnable withoutMaxWait = Lodash.debounce(() -> { withoutCount.value++; } , 96, o1); long start = System.currentTimeMillis(); while (System.currentTimeMillis() - start < limit) { withMaxWait.run(); withoutMaxWait.run(); } o0.setTimeout().apply(() -> { Assert.assertTrue(withCount.value > 0); Assert.assertEquals(0, withoutCount.value); } , 64L).get(); }