target
stringlengths
20
113k
src_fm
stringlengths
11
86.3k
src_fm_fc
stringlengths
21
86.4k
src_fm_fc_co
stringlengths
30
86.4k
src_fm_fc_ms
stringlengths
42
86.8k
src_fm_fc_ms_ff
stringlengths
43
86.8k
@Test public void testScheduledExecutorServiceCaptures() throws InterruptedException { ScheduledExecutorService e = Executors.newScheduledThreadPool(10); ScheduledExecutorService f = StateCapture.capturingDecorator(e); CapturedState mockCapturedState = mock(CapturedState.class); Runnable mockRunnable = mock(Runnable.class); ThreadLocalStateCaptor.THREAD_LOCAL.set(mockCapturedState); f.execute(mockRunnable); e.shutdown(); e.awaitTermination(10, TimeUnit.HOURS); verifyStandardCaptures(mockCapturedState, mockRunnable); }
public static Executor capturingDecorator(final Executor executor) { if (stateCaptors.isEmpty() || executor instanceof CapturingExecutor) { return executor; } else { return new CapturingExecutor(executor); } }
StateCapture { public static Executor capturingDecorator(final Executor executor) { if (stateCaptors.isEmpty() || executor instanceof CapturingExecutor) { return executor; } else { return new CapturingExecutor(executor); } } }
StateCapture { public static Executor capturingDecorator(final Executor executor) { if (stateCaptors.isEmpty() || executor instanceof CapturingExecutor) { return executor; } else { return new CapturingExecutor(executor); } } private StateCapture(); }
StateCapture { public static Executor capturingDecorator(final Executor executor) { if (stateCaptors.isEmpty() || executor instanceof CapturingExecutor) { return executor; } else { return new CapturingExecutor(executor); } } private StateCapture(); static Executor capturingDecorator(final Executor executor); static ExecutorService capturingDecorator(final ExecutorService executorService); static ScheduledExecutorService capturingDecorator(final ScheduledExecutorService executorService); static CompletionService<T> capturingDecorator(final CompletionService<T> completionService); static Runnable capturingDecorator(Runnable delegate); static Callable<V> capturingDecorator(Callable<V> delegate); }
StateCapture { public static Executor capturingDecorator(final Executor executor) { if (stateCaptors.isEmpty() || executor instanceof CapturingExecutor) { return executor; } else { return new CapturingExecutor(executor); } } private StateCapture(); static Executor capturingDecorator(final Executor executor); static ExecutorService capturingDecorator(final ExecutorService executorService); static ScheduledExecutorService capturingDecorator(final ScheduledExecutorService executorService); static CompletionService<T> capturingDecorator(final CompletionService<T> completionService); static Runnable capturingDecorator(Runnable delegate); static Callable<V> capturingDecorator(Callable<V> delegate); }
@Test public void benchmark() throws Exception { final long iterations = 1000000; long duration_StringFormat = run_benchmark(iterations, (a) -> String.format("%.6f", a)); NumberFormat javaFormatter = NumberFormat.getInstance(); javaFormatter.setMinimumFractionDigits(0); javaFormatter.setMaximumFractionDigits(6); javaFormatter.setGroupingUsed(false); long duration_JavaFormat = run_benchmark(iterations, (a) -> javaFormatter.format(a)); long duration_JavaStringBuilder = run_benchmark(iterations, (a) -> (new StringBuilder()).append(a)); long duration_DoubleFormat = run_benchmark(iterations, (a) -> DoubleFormat.format(new StringBuilder(), a)); System.out.println("DoubleFormat Performance comparison: " + iterations +" iterations"); System.out.println("\tJava String.format: " + duration_StringFormat + " ms"); System.out.println("\tJava NumberFormat: " + duration_JavaFormat + " ms"); System.out.println("\tJava StringBuilder: " + duration_JavaStringBuilder + " ms"); System.out.println("\tDoubleFormat: " + duration_DoubleFormat + " ms"); assertTrue(duration_DoubleFormat < duration_StringFormat); assertTrue(duration_DoubleFormat < duration_JavaFormat); assertTrue(duration_DoubleFormat < duration_JavaStringBuilder); }
public static void format(Appendable sb, double value) { try { format_exception(sb, value); } catch (IOException e) { throw new IllegalStateException("Appendable must not throw IOException", e); } }
DoubleFormat { public static void format(Appendable sb, double value) { try { format_exception(sb, value); } catch (IOException e) { throw new IllegalStateException("Appendable must not throw IOException", e); } } }
DoubleFormat { public static void format(Appendable sb, double value) { try { format_exception(sb, value); } catch (IOException e) { throw new IllegalStateException("Appendable must not throw IOException", e); } } private DoubleFormat(); }
DoubleFormat { public static void format(Appendable sb, double value) { try { format_exception(sb, value); } catch (IOException e) { throw new IllegalStateException("Appendable must not throw IOException", e); } } private DoubleFormat(); static void format(Appendable sb, double value); }
DoubleFormat { public static void format(Appendable sb, double value) { try { format_exception(sb, value); } catch (IOException e) { throw new IllegalStateException("Appendable must not throw IOException", e); } } private DoubleFormat(); static void format(Appendable sb, double value); }
@Test public void currentContextIsPropagatedToTask() throws Exception { ThreadContext context = ThreadContext.emptyContext().with(KEY, "value"); try (ThreadContext.CloseableContext ignored = context.open()) { executor.execute(captureTask); } latch.await(); assertSame(context, contextCapture.get()); }
@Override public void execute(Runnable command) { executor.execute(ThreadContext.current().wrap(command)); }
ContextAwareExecutor implements Executor { @Override public void execute(Runnable command) { executor.execute(ThreadContext.current().wrap(command)); } }
ContextAwareExecutor implements Executor { @Override public void execute(Runnable command) { executor.execute(ThreadContext.current().wrap(command)); } ContextAwareExecutor(Executor executor); }
ContextAwareExecutor implements Executor { @Override public void execute(Runnable command) { executor.execute(ThreadContext.current().wrap(command)); } ContextAwareExecutor(Executor executor); @Override void execute(Runnable command); }
ContextAwareExecutor implements Executor { @Override public void execute(Runnable command) { executor.execute(ThreadContext.current().wrap(command)); } ContextAwareExecutor(Executor executor); @Override void execute(Runnable command); }
@Test public void withNoContextDefaultIsUsed() throws Exception { executor.execute(captureTask); latch.await(); assertSame(ThreadContext.emptyContext(), contextCapture.get()); }
@Override public void execute(Runnable command) { executor.execute(ThreadContext.current().wrap(command)); }
ContextAwareExecutor implements Executor { @Override public void execute(Runnable command) { executor.execute(ThreadContext.current().wrap(command)); } }
ContextAwareExecutor implements Executor { @Override public void execute(Runnable command) { executor.execute(ThreadContext.current().wrap(command)); } ContextAwareExecutor(Executor executor); }
ContextAwareExecutor implements Executor { @Override public void execute(Runnable command) { executor.execute(ThreadContext.current().wrap(command)); } ContextAwareExecutor(Executor executor); @Override void execute(Runnable command); }
ContextAwareExecutor implements Executor { @Override public void execute(Runnable command) { executor.execute(ThreadContext.current().wrap(command)); } ContextAwareExecutor(Executor executor); @Override void execute(Runnable command); }
@Test public void keyset() throws Exception { final TypedMap.Key<Double> ka = TypedMap.key("Alpha", Double.class); final TypedMap.Key<String> kb = TypedMap.key("Beta", String.class); final TypedMap.Key<UUID> kc = TypedMap.key("Gamma", UUID.class); final TypedMap.Key<Object> kd = TypedMap.key("Delta", Object.class); final Double va = Double.valueOf(867.5309); final String vb = "Here is a string, with...\nmultiple lines and stuff."; final UUID vc = UUID.randomUUID(); final Object vd = new Object(); TypedMap data = ImmutableTypedMap.Builder .with(kc, vc) .add(ka, va) .add(kb, vb) .add(kd, vd) .build(); Set<TypedMap.Key> s = data.keySet(); assertEquals("Set of keys has wrong size", 4, s.size()); assertTrue("Set of keys is missing a key", s.contains(ka)); assertTrue("Set of keys is missing a key", s.contains(kb)); assertTrue("Set of keys is missing a key", s.contains(kc)); assertTrue("Set of keys is missing a key", s.contains(kd)); }
@Override public Set<Key> keySet() { return this.dataMap.keySet(); }
ImmutableTypedMap implements TypedMap { @Override public Set<Key> keySet() { return this.dataMap.keySet(); } }
ImmutableTypedMap implements TypedMap { @Override public Set<Key> keySet() { return this.dataMap.keySet(); } private ImmutableTypedMap(Map<Key<?>, Entry<?>> dataMap); }
ImmutableTypedMap implements TypedMap { @Override public Set<Key> keySet() { return this.dataMap.keySet(); } private ImmutableTypedMap(Map<Key<?>, Entry<?>> dataMap); @Override final T get(Key<T> key); @Override int size(); @Override boolean isEmpty(); @Override Iterator<Entry> iterator(); @Override Iterator<Entry<T>> typedIterator(Class<T> clazz); @Override Set<Key> keySet(); @Override Set<Key<T>> typedKeySet(final Class<T> clazz); @Override boolean containsKey(final Key<?> key); @Override boolean containsValue(final Object value); }
ImmutableTypedMap implements TypedMap { @Override public Set<Key> keySet() { return this.dataMap.keySet(); } private ImmutableTypedMap(Map<Key<?>, Entry<?>> dataMap); @Override final T get(Key<T> key); @Override int size(); @Override boolean isEmpty(); @Override Iterator<Entry> iterator(); @Override Iterator<Entry<T>> typedIterator(Class<T> clazz); @Override Set<Key> keySet(); @Override Set<Key<T>> typedKeySet(final Class<T> clazz); @Override boolean containsKey(final Key<?> key); @Override boolean containsValue(final Object value); }
@Test public void typedKeyset() throws Exception { final TypedMap.Key<Double> ka = TypedMap.key("A", Double.class); final TypedMap.Key<String> kb = TypedMap.key("B", String.class); final TypedMap.Key<String> kc = TypedMap.key("C", String.class); final TypedMap.Key<Object> kd = TypedMap.key("D", Object.class); final Double va = Double.valueOf(3.14159); final String vb = "Mmm tasty pi"; final String vc = "but no love for tau"; final Object vd = "or a type safe string"; TypedMap data = ImmutableTypedMap.Builder .with(kc, vc) .add(ka, va) .add(kb, vb) .add(kd, vd) .build(); Set<TypedMap.Key<String>> s = data.typedKeySet(String.class); assertEquals("Typed set of keys has wrong size", 2, s.size()); assertFalse("Typed set of keys has unexpected member", s.contains(ka)); assertTrue("Typed set of keys is missing a key", s.contains(kb)); assertTrue("Typed set of keys is missing a key", s.contains(kc)); assertFalse("Typed set of keys has unexpected member", s.contains(kd)); }
@Override public <T> Set<Key<T>> typedKeySet(final Class<T> clazz) { Set<Key<T>> keys = new HashSet<>(); for (Key k : this.keySet()) { if (k.valueType.equals(clazz)) { keys.add(k); } } return Collections.unmodifiableSet(keys); }
ImmutableTypedMap implements TypedMap { @Override public <T> Set<Key<T>> typedKeySet(final Class<T> clazz) { Set<Key<T>> keys = new HashSet<>(); for (Key k : this.keySet()) { if (k.valueType.equals(clazz)) { keys.add(k); } } return Collections.unmodifiableSet(keys); } }
ImmutableTypedMap implements TypedMap { @Override public <T> Set<Key<T>> typedKeySet(final Class<T> clazz) { Set<Key<T>> keys = new HashSet<>(); for (Key k : this.keySet()) { if (k.valueType.equals(clazz)) { keys.add(k); } } return Collections.unmodifiableSet(keys); } private ImmutableTypedMap(Map<Key<?>, Entry<?>> dataMap); }
ImmutableTypedMap implements TypedMap { @Override public <T> Set<Key<T>> typedKeySet(final Class<T> clazz) { Set<Key<T>> keys = new HashSet<>(); for (Key k : this.keySet()) { if (k.valueType.equals(clazz)) { keys.add(k); } } return Collections.unmodifiableSet(keys); } private ImmutableTypedMap(Map<Key<?>, Entry<?>> dataMap); @Override final T get(Key<T> key); @Override int size(); @Override boolean isEmpty(); @Override Iterator<Entry> iterator(); @Override Iterator<Entry<T>> typedIterator(Class<T> clazz); @Override Set<Key> keySet(); @Override Set<Key<T>> typedKeySet(final Class<T> clazz); @Override boolean containsKey(final Key<?> key); @Override boolean containsValue(final Object value); }
ImmutableTypedMap implements TypedMap { @Override public <T> Set<Key<T>> typedKeySet(final Class<T> clazz) { Set<Key<T>> keys = new HashSet<>(); for (Key k : this.keySet()) { if (k.valueType.equals(clazz)) { keys.add(k); } } return Collections.unmodifiableSet(keys); } private ImmutableTypedMap(Map<Key<?>, Entry<?>> dataMap); @Override final T get(Key<T> key); @Override int size(); @Override boolean isEmpty(); @Override Iterator<Entry> iterator(); @Override Iterator<Entry<T>> typedIterator(Class<T> clazz); @Override Set<Key> keySet(); @Override Set<Key<T>> typedKeySet(final Class<T> clazz); @Override boolean containsKey(final Key<?> key); @Override boolean containsValue(final Object value); }
@Test public void iterator() throws Exception { final TypedMap.Key<Long> ka = TypedMap.key("1", Long.class); final TypedMap.Key<Short> kb = TypedMap.key("2", Short.class); final TypedMap.Key<BigInteger> kc = TypedMap.key("5", BigInteger.class); final Long va = Long.valueOf(999999999); final Short vb = Short.valueOf((short)1); final BigInteger vc = BigInteger.valueOf(Long.MAX_VALUE+Short.MAX_VALUE); TypedMap data = ImmutableTypedMap.Builder .with(ka, va) .add(kb, vb) .add(kc, vc) .build(); assertEquals(3, data.size()); int i = 0; boolean seen_a = false; boolean seen_b = false; boolean seen_c = false; for (TypedMap.Entry e : data) { i++; if (e.getKey().equals(ka) && e.getValue().equals(va)) { seen_a = true; } if (e.getKey().equals(kb) && e.getValue().equals(vb)) { seen_b = true; } if (e.getKey().equals(kc) && e.getValue().equals(vc)) { seen_c = true; } } assertEquals("Iterator iterated incorrect increments", 3, i); assertTrue("Iterator missed an entry", seen_a); assertTrue("Iterator missed an entry", seen_b); assertTrue("Iterator missed an entry", seen_c); }
@Override public Iterator<Entry> iterator() { return this.dataMap.values().iterator(); }
ImmutableTypedMap implements TypedMap { @Override public Iterator<Entry> iterator() { return this.dataMap.values().iterator(); } }
ImmutableTypedMap implements TypedMap { @Override public Iterator<Entry> iterator() { return this.dataMap.values().iterator(); } private ImmutableTypedMap(Map<Key<?>, Entry<?>> dataMap); }
ImmutableTypedMap implements TypedMap { @Override public Iterator<Entry> iterator() { return this.dataMap.values().iterator(); } private ImmutableTypedMap(Map<Key<?>, Entry<?>> dataMap); @Override final T get(Key<T> key); @Override int size(); @Override boolean isEmpty(); @Override Iterator<Entry> iterator(); @Override Iterator<Entry<T>> typedIterator(Class<T> clazz); @Override Set<Key> keySet(); @Override Set<Key<T>> typedKeySet(final Class<T> clazz); @Override boolean containsKey(final Key<?> key); @Override boolean containsValue(final Object value); }
ImmutableTypedMap implements TypedMap { @Override public Iterator<Entry> iterator() { return this.dataMap.values().iterator(); } private ImmutableTypedMap(Map<Key<?>, Entry<?>> dataMap); @Override final T get(Key<T> key); @Override int size(); @Override boolean isEmpty(); @Override Iterator<Entry> iterator(); @Override Iterator<Entry<T>> typedIterator(Class<T> clazz); @Override Set<Key> keySet(); @Override Set<Key<T>> typedKeySet(final Class<T> clazz); @Override boolean containsKey(final Key<?> key); @Override boolean containsValue(final Object value); }
@Test(expected=UnsupportedOperationException.class) public void iterator_remove() throws Exception { final TypedMap.Key<Long> ka = TypedMap.key("1", Long.class); final Long va = Long.valueOf(999999999); TypedMap data = ImmutableTypedMap.Builder.with(ka, va).build(); Iterator<TypedMap.Entry> it = data.iterator(); it.remove(); }
@Override public Iterator<Entry> iterator() { return this.dataMap.values().iterator(); }
ImmutableTypedMap implements TypedMap { @Override public Iterator<Entry> iterator() { return this.dataMap.values().iterator(); } }
ImmutableTypedMap implements TypedMap { @Override public Iterator<Entry> iterator() { return this.dataMap.values().iterator(); } private ImmutableTypedMap(Map<Key<?>, Entry<?>> dataMap); }
ImmutableTypedMap implements TypedMap { @Override public Iterator<Entry> iterator() { return this.dataMap.values().iterator(); } private ImmutableTypedMap(Map<Key<?>, Entry<?>> dataMap); @Override final T get(Key<T> key); @Override int size(); @Override boolean isEmpty(); @Override Iterator<Entry> iterator(); @Override Iterator<Entry<T>> typedIterator(Class<T> clazz); @Override Set<Key> keySet(); @Override Set<Key<T>> typedKeySet(final Class<T> clazz); @Override boolean containsKey(final Key<?> key); @Override boolean containsValue(final Object value); }
ImmutableTypedMap implements TypedMap { @Override public Iterator<Entry> iterator() { return this.dataMap.values().iterator(); } private ImmutableTypedMap(Map<Key<?>, Entry<?>> dataMap); @Override final T get(Key<T> key); @Override int size(); @Override boolean isEmpty(); @Override Iterator<Entry> iterator(); @Override Iterator<Entry<T>> typedIterator(Class<T> clazz); @Override Set<Key> keySet(); @Override Set<Key<T>> typedKeySet(final Class<T> clazz); @Override boolean containsKey(final Key<?> key); @Override boolean containsValue(final Object value); }
@Test public void forEach() throws Exception { final TypedMap.Key<Long> ka = TypedMap.key("1", Long.class); final TypedMap.Key<Short> kb = TypedMap.key("2", Short.class); final TypedMap.Key<BigInteger> kc = TypedMap.key("5", BigInteger.class); final Long va = Long.valueOf(999999999); final Short vb = Short.valueOf((short)1); final BigInteger vc = BigInteger.valueOf(Long.MAX_VALUE+Short.MAX_VALUE); TypedMap data = ImmutableTypedMap.Builder .with(ka, va) .add(kb, vb) .add(kc, vc) .build(); assertEquals(3, data.size()); final boolean[] seen = {false,false,false,false}; data.forEach((e) -> { if (e.getKey().equals(ka) && e.getValue().equals(va)) { seen[0] = true; } else if (e.getKey().equals(kb) && e.getValue().equals(vb)) { seen[1] = true; } else if (e.getKey().equals(kc) && e.getValue().equals(vc)) { seen[2] = true; } else { seen[3] = true; } }); assertTrue("forEach missed an entry", seen[0]); assertTrue("forEach missed an entry", seen[1]); assertTrue("forEach missed an entry", seen[2]); assertFalse("forEach hit an unexpected entry", seen[3]); }
@Override public int size() { return this.dataMap.size(); }
ImmutableTypedMap implements TypedMap { @Override public int size() { return this.dataMap.size(); } }
ImmutableTypedMap implements TypedMap { @Override public int size() { return this.dataMap.size(); } private ImmutableTypedMap(Map<Key<?>, Entry<?>> dataMap); }
ImmutableTypedMap implements TypedMap { @Override public int size() { return this.dataMap.size(); } private ImmutableTypedMap(Map<Key<?>, Entry<?>> dataMap); @Override final T get(Key<T> key); @Override int size(); @Override boolean isEmpty(); @Override Iterator<Entry> iterator(); @Override Iterator<Entry<T>> typedIterator(Class<T> clazz); @Override Set<Key> keySet(); @Override Set<Key<T>> typedKeySet(final Class<T> clazz); @Override boolean containsKey(final Key<?> key); @Override boolean containsValue(final Object value); }
ImmutableTypedMap implements TypedMap { @Override public int size() { return this.dataMap.size(); } private ImmutableTypedMap(Map<Key<?>, Entry<?>> dataMap); @Override final T get(Key<T> key); @Override int size(); @Override boolean isEmpty(); @Override Iterator<Entry> iterator(); @Override Iterator<Entry<T>> typedIterator(Class<T> clazz); @Override Set<Key> keySet(); @Override Set<Key<T>> typedKeySet(final Class<T> clazz); @Override boolean containsKey(final Key<?> key); @Override boolean containsValue(final Object value); }
@Test public void typedIterator() throws Exception { final TypedMap.Key<String> ka = TypedMap.key("y", String.class); final TypedMap.Key<Object> kb = TypedMap.key("n", Object.class); final TypedMap.Key<String> kc = TypedMap.key("m", String.class); final String va = "yes"; final String vb = "no"; final String vc = "maybe"; TypedMap data = ImmutableTypedMap.Builder .with(ka, va) .add(kb, vb) .add(kc, vc) .build(); assertEquals(3, data.size()); int i = 0; boolean seen_a = false; boolean seen_b = false; boolean seen_c = false; Iterator<TypedMap.Entry<String>> it = data.typedIterator(String.class); while (it.hasNext()) { i++; final TypedMap.Entry<String> e = it.next(); TypedMap.Key<String> k = e.getKey(); String v = e.getValue(); if (k.equals(ka) && v.equals(va)) { seen_a = true; } if (k.equals(kb) && v.equals(vb)) { seen_b = true; } if (k.equals(kc) && v.equals(vc)) { seen_c = true; } } assertEquals("Iterator iterated incorrect increments", 2, i); assertTrue("Typed iterator missed an entry", seen_a); assertFalse("Typed iterator returned an entry of wrong type", seen_b); assertTrue("Typed iterator missed an entry", seen_c); }
@Override public <T> Iterator<Entry<T>> typedIterator(Class<T> clazz) { List<Entry<T>> entries = new ArrayList(); for (Iterator<Entry> it = this.iterator(); it.hasNext(); ) { final Entry e = it.next(); if (e.getKey().valueType.equals(clazz)) { entries.add(e); } } return Collections.unmodifiableCollection(entries).iterator(); }
ImmutableTypedMap implements TypedMap { @Override public <T> Iterator<Entry<T>> typedIterator(Class<T> clazz) { List<Entry<T>> entries = new ArrayList(); for (Iterator<Entry> it = this.iterator(); it.hasNext(); ) { final Entry e = it.next(); if (e.getKey().valueType.equals(clazz)) { entries.add(e); } } return Collections.unmodifiableCollection(entries).iterator(); } }
ImmutableTypedMap implements TypedMap { @Override public <T> Iterator<Entry<T>> typedIterator(Class<T> clazz) { List<Entry<T>> entries = new ArrayList(); for (Iterator<Entry> it = this.iterator(); it.hasNext(); ) { final Entry e = it.next(); if (e.getKey().valueType.equals(clazz)) { entries.add(e); } } return Collections.unmodifiableCollection(entries).iterator(); } private ImmutableTypedMap(Map<Key<?>, Entry<?>> dataMap); }
ImmutableTypedMap implements TypedMap { @Override public <T> Iterator<Entry<T>> typedIterator(Class<T> clazz) { List<Entry<T>> entries = new ArrayList(); for (Iterator<Entry> it = this.iterator(); it.hasNext(); ) { final Entry e = it.next(); if (e.getKey().valueType.equals(clazz)) { entries.add(e); } } return Collections.unmodifiableCollection(entries).iterator(); } private ImmutableTypedMap(Map<Key<?>, Entry<?>> dataMap); @Override final T get(Key<T> key); @Override int size(); @Override boolean isEmpty(); @Override Iterator<Entry> iterator(); @Override Iterator<Entry<T>> typedIterator(Class<T> clazz); @Override Set<Key> keySet(); @Override Set<Key<T>> typedKeySet(final Class<T> clazz); @Override boolean containsKey(final Key<?> key); @Override boolean containsValue(final Object value); }
ImmutableTypedMap implements TypedMap { @Override public <T> Iterator<Entry<T>> typedIterator(Class<T> clazz) { List<Entry<T>> entries = new ArrayList(); for (Iterator<Entry> it = this.iterator(); it.hasNext(); ) { final Entry e = it.next(); if (e.getKey().valueType.equals(clazz)) { entries.add(e); } } return Collections.unmodifiableCollection(entries).iterator(); } private ImmutableTypedMap(Map<Key<?>, Entry<?>> dataMap); @Override final T get(Key<T> key); @Override int size(); @Override boolean isEmpty(); @Override Iterator<Entry> iterator(); @Override Iterator<Entry<T>> typedIterator(Class<T> clazz); @Override Set<Key> keySet(); @Override Set<Key<T>> typedKeySet(final Class<T> clazz); @Override boolean containsKey(final Key<?> key); @Override boolean containsValue(final Object value); }
@Test(expected=UnsupportedOperationException.class) public void typedIterator_remove() throws Exception { final TypedMap.Key<String> ka = TypedMap.key("y", String.class); final String va = "yes"; TypedMap data = ImmutableTypedMap.Builder.with(ka, va).build(); Iterator<TypedMap.Entry<String>> it = data.typedIterator(String.class); it.remove(); }
@Override public <T> Iterator<Entry<T>> typedIterator(Class<T> clazz) { List<Entry<T>> entries = new ArrayList(); for (Iterator<Entry> it = this.iterator(); it.hasNext(); ) { final Entry e = it.next(); if (e.getKey().valueType.equals(clazz)) { entries.add(e); } } return Collections.unmodifiableCollection(entries).iterator(); }
ImmutableTypedMap implements TypedMap { @Override public <T> Iterator<Entry<T>> typedIterator(Class<T> clazz) { List<Entry<T>> entries = new ArrayList(); for (Iterator<Entry> it = this.iterator(); it.hasNext(); ) { final Entry e = it.next(); if (e.getKey().valueType.equals(clazz)) { entries.add(e); } } return Collections.unmodifiableCollection(entries).iterator(); } }
ImmutableTypedMap implements TypedMap { @Override public <T> Iterator<Entry<T>> typedIterator(Class<T> clazz) { List<Entry<T>> entries = new ArrayList(); for (Iterator<Entry> it = this.iterator(); it.hasNext(); ) { final Entry e = it.next(); if (e.getKey().valueType.equals(clazz)) { entries.add(e); } } return Collections.unmodifiableCollection(entries).iterator(); } private ImmutableTypedMap(Map<Key<?>, Entry<?>> dataMap); }
ImmutableTypedMap implements TypedMap { @Override public <T> Iterator<Entry<T>> typedIterator(Class<T> clazz) { List<Entry<T>> entries = new ArrayList(); for (Iterator<Entry> it = this.iterator(); it.hasNext(); ) { final Entry e = it.next(); if (e.getKey().valueType.equals(clazz)) { entries.add(e); } } return Collections.unmodifiableCollection(entries).iterator(); } private ImmutableTypedMap(Map<Key<?>, Entry<?>> dataMap); @Override final T get(Key<T> key); @Override int size(); @Override boolean isEmpty(); @Override Iterator<Entry> iterator(); @Override Iterator<Entry<T>> typedIterator(Class<T> clazz); @Override Set<Key> keySet(); @Override Set<Key<T>> typedKeySet(final Class<T> clazz); @Override boolean containsKey(final Key<?> key); @Override boolean containsValue(final Object value); }
ImmutableTypedMap implements TypedMap { @Override public <T> Iterator<Entry<T>> typedIterator(Class<T> clazz) { List<Entry<T>> entries = new ArrayList(); for (Iterator<Entry> it = this.iterator(); it.hasNext(); ) { final Entry e = it.next(); if (e.getKey().valueType.equals(clazz)) { entries.add(e); } } return Collections.unmodifiableCollection(entries).iterator(); } private ImmutableTypedMap(Map<Key<?>, Entry<?>> dataMap); @Override final T get(Key<T> key); @Override int size(); @Override boolean isEmpty(); @Override Iterator<Entry> iterator(); @Override Iterator<Entry<T>> typedIterator(Class<T> clazz); @Override Set<Key> keySet(); @Override Set<Key<T>> typedKeySet(final Class<T> clazz); @Override boolean containsKey(final Key<?> key); @Override boolean containsValue(final Object value); }
@Test public void testCompletionServiceRunnableCaptures() throws InterruptedException, Exception { ExecutorService executor = Executors.newCachedThreadPool(); CompletionService<Object> delegate = new ExecutorCompletionService<>(executor); CompletionService<Object> cs = StateCapture.capturingDecorator(delegate); CapturedState mockCapturedState = mock(CapturedState.class); Runnable mockRunnable = mock(Runnable.class); ThreadLocalStateCaptor.THREAD_LOCAL.set(mockCapturedState); Object result = new Object(); Future<Object> futureResult = cs.submit(mockRunnable, result); assertThat("Expected the delegate response to return", result, sameInstance(futureResult.get())); executor.shutdown(); verifyStandardCaptures(mockCapturedState, mockRunnable); }
public static Executor capturingDecorator(final Executor executor) { if (stateCaptors.isEmpty() || executor instanceof CapturingExecutor) { return executor; } else { return new CapturingExecutor(executor); } }
StateCapture { public static Executor capturingDecorator(final Executor executor) { if (stateCaptors.isEmpty() || executor instanceof CapturingExecutor) { return executor; } else { return new CapturingExecutor(executor); } } }
StateCapture { public static Executor capturingDecorator(final Executor executor) { if (stateCaptors.isEmpty() || executor instanceof CapturingExecutor) { return executor; } else { return new CapturingExecutor(executor); } } private StateCapture(); }
StateCapture { public static Executor capturingDecorator(final Executor executor) { if (stateCaptors.isEmpty() || executor instanceof CapturingExecutor) { return executor; } else { return new CapturingExecutor(executor); } } private StateCapture(); static Executor capturingDecorator(final Executor executor); static ExecutorService capturingDecorator(final ExecutorService executorService); static ScheduledExecutorService capturingDecorator(final ScheduledExecutorService executorService); static CompletionService<T> capturingDecorator(final CompletionService<T> completionService); static Runnable capturingDecorator(Runnable delegate); static Callable<V> capturingDecorator(Callable<V> delegate); }
StateCapture { public static Executor capturingDecorator(final Executor executor) { if (stateCaptors.isEmpty() || executor instanceof CapturingExecutor) { return executor; } else { return new CapturingExecutor(executor); } } private StateCapture(); static Executor capturingDecorator(final Executor executor); static ExecutorService capturingDecorator(final ExecutorService executorService); static ScheduledExecutorService capturingDecorator(final ScheduledExecutorService executorService); static CompletionService<T> capturingDecorator(final CompletionService<T> completionService); static Runnable capturingDecorator(Runnable delegate); static Callable<V> capturingDecorator(Callable<V> delegate); }
@Test public void forEachTyped() throws Exception { final TypedMap.Key<String> ka = TypedMap.key("y", String.class); final TypedMap.Key<Object> kb = TypedMap.key("n", Object.class); final TypedMap.Key<String> kc = TypedMap.key("m", String.class); final TypedMap.Key<Integer> kd = TypedMap.key("s", Integer.class); final String va = "yes"; final String vb = "no"; final String vc = "maybe"; final Integer vd = 50; TypedMap data = ImmutableTypedMap.Builder .with(ka, va) .add(kb, vb) .add(kc, vc) .add(kd, vd) .build(); assertEquals(4, data.size()); final boolean[] seen = {false,false,false,false,false}; data.forEachTyped(String.class, (e) -> { TypedMap.Key<String> k = e.getKey(); String v = e.getValue(); if (k.equals(ka) && v.equals(va)) { seen[0] = true; } else if (k.equals(kb) && v.equals(vb)) { seen[1] = true; } else if (k.equals(kc) && v.equals(vc)) { seen[2] = true; } else if (k.equals(kd) && v.equals(vd)) { seen[3] = true; } else { seen[4] = true; } }); assertTrue("forEachTyped missed an entry", seen[0]); assertFalse("forEachTyped hit an unexpected entry", seen[1]); assertTrue("forEachTyped missed an entry", seen[2]); assertFalse("forEachTyped hit an unexpected entry", seen[3]); assertFalse("forEachTyped hit an unexpected entry", seen[4]); }
@Override public int size() { return this.dataMap.size(); }
ImmutableTypedMap implements TypedMap { @Override public int size() { return this.dataMap.size(); } }
ImmutableTypedMap implements TypedMap { @Override public int size() { return this.dataMap.size(); } private ImmutableTypedMap(Map<Key<?>, Entry<?>> dataMap); }
ImmutableTypedMap implements TypedMap { @Override public int size() { return this.dataMap.size(); } private ImmutableTypedMap(Map<Key<?>, Entry<?>> dataMap); @Override final T get(Key<T> key); @Override int size(); @Override boolean isEmpty(); @Override Iterator<Entry> iterator(); @Override Iterator<Entry<T>> typedIterator(Class<T> clazz); @Override Set<Key> keySet(); @Override Set<Key<T>> typedKeySet(final Class<T> clazz); @Override boolean containsKey(final Key<?> key); @Override boolean containsValue(final Object value); }
ImmutableTypedMap implements TypedMap { @Override public int size() { return this.dataMap.size(); } private ImmutableTypedMap(Map<Key<?>, Entry<?>> dataMap); @Override final T get(Key<T> key); @Override int size(); @Override boolean isEmpty(); @Override Iterator<Entry> iterator(); @Override Iterator<Entry<T>> typedIterator(Class<T> clazz); @Override Set<Key> keySet(); @Override Set<Key<T>> typedKeySet(final Class<T> clazz); @Override boolean containsKey(final Key<?> key); @Override boolean containsValue(final Object value); }
@Test public void testCompletionServiceCallableCaptures() throws InterruptedException, Exception { ExecutorService executor = Executors.newCachedThreadPool(); CompletionService<Object> delegate = new ExecutorCompletionService<>(executor); CompletionService<Object> cs = StateCapture.capturingDecorator(delegate); CapturedState mockCapturedState = mock(CapturedState.class); Object expectedResult = new Object(); @SuppressWarnings("unchecked") Callable<Object> mockCallable = mock(Callable.class); when(mockCallable.call()).thenReturn(expectedResult); ThreadLocalStateCaptor.THREAD_LOCAL.set(mockCapturedState); Future<Object> futureResult = cs.submit(mockCallable); executor.shutdown(); verifyStandardCaptures(mockCapturedState, mockCallable, expectedResult, futureResult.get()); }
public static Executor capturingDecorator(final Executor executor) { if (stateCaptors.isEmpty() || executor instanceof CapturingExecutor) { return executor; } else { return new CapturingExecutor(executor); } }
StateCapture { public static Executor capturingDecorator(final Executor executor) { if (stateCaptors.isEmpty() || executor instanceof CapturingExecutor) { return executor; } else { return new CapturingExecutor(executor); } } }
StateCapture { public static Executor capturingDecorator(final Executor executor) { if (stateCaptors.isEmpty() || executor instanceof CapturingExecutor) { return executor; } else { return new CapturingExecutor(executor); } } private StateCapture(); }
StateCapture { public static Executor capturingDecorator(final Executor executor) { if (stateCaptors.isEmpty() || executor instanceof CapturingExecutor) { return executor; } else { return new CapturingExecutor(executor); } } private StateCapture(); static Executor capturingDecorator(final Executor executor); static ExecutorService capturingDecorator(final ExecutorService executorService); static ScheduledExecutorService capturingDecorator(final ScheduledExecutorService executorService); static CompletionService<T> capturingDecorator(final CompletionService<T> completionService); static Runnable capturingDecorator(Runnable delegate); static Callable<V> capturingDecorator(Callable<V> delegate); }
StateCapture { public static Executor capturingDecorator(final Executor executor) { if (stateCaptors.isEmpty() || executor instanceof CapturingExecutor) { return executor; } else { return new CapturingExecutor(executor); } } private StateCapture(); static Executor capturingDecorator(final Executor executor); static ExecutorService capturingDecorator(final ExecutorService executorService); static ScheduledExecutorService capturingDecorator(final ScheduledExecutorService executorService); static CompletionService<T> capturingDecorator(final CompletionService<T> completionService); static Runnable capturingDecorator(Runnable delegate); static Callable<V> capturingDecorator(Callable<V> delegate); }
@Test public void nullContextCanHaveAttributes() { TypedMap.Key<String> ID = TypedMap.key("ID", String.class); TypedMap attributes = ImmutableTypedMap.Builder.with(ID, "id").build(); NullContext context = new NullContext(attributes); assertSame(attributes, context.attributes()); }
@Override public TypedMap attributes() { return attributes; }
NullContext implements MetricContext { @Override public TypedMap attributes() { return attributes; } }
NullContext implements MetricContext { @Override public TypedMap attributes() { return attributes; } NullContext(TypedMap attributes); NullContext(TypedMap attributes, MetricContext parent); }
NullContext implements MetricContext { @Override public TypedMap attributes() { return attributes; } NullContext(TypedMap attributes); NullContext(TypedMap attributes, MetricContext parent); static NullContext empty(); @Override TypedMap attributes(); @Override void record(Metric label, Number value, Unit unit, Instant time); @Override void count(Metric label, long delta); @Override void close(); @Override MetricContext newChild(TypedMap attributes); @Override MetricContext parent(); }
NullContext implements MetricContext { @Override public TypedMap attributes() { return attributes; } NullContext(TypedMap attributes); NullContext(TypedMap attributes, MetricContext parent); static NullContext empty(); @Override TypedMap attributes(); @Override void record(Metric label, Number value, Unit unit, Instant time); @Override void count(Metric label, long delta); @Override void close(); @Override MetricContext newChild(TypedMap attributes); @Override MetricContext parent(); }
@Test public void validation() throws Exception { final String[] goods = { "snake_case_metric", "camelCaseMetric", "hyphenated-metric", "spaced out metric", "digits 0123456789", "G\u00FCnther", "\t\n!\"#$%&'()*+,./:;<=>?@[\\]^`{|}~", " " }; for (String s : goods) { Metric.define(s); } final String[] bads = { null, "" }; for (String s : bads) { try { Metric.define(""); } catch (IllegalArgumentException expected) { continue; } fail("Expected exception not thrown for bad name '"+s+"'"); } }
public static Metric define(final String name) { return new Metric(name); }
Metric { public static Metric define(final String name) { return new Metric(name); } }
Metric { public static Metric define(final String name) { return new Metric(name); } private Metric(final String name); }
Metric { public static Metric define(final String name) { return new Metric(name); } private Metric(final String name); static Metric define(final String name); @Override final String toString(); @Override boolean equals(final Object o); @Override int hashCode(); }
Metric { public static Metric define(final String name) { return new Metric(name); } private Metric(final String name); static Metric define(final String name); @Override final String toString(); @Override boolean equals(final Object o); @Override int hashCode(); }
@Test public void recordIsSentToAllDelegates() { MetricContext context = recorder.context(attributes); context.record(METRIC, 42L, Unit.NONE, timestamp); verify(delegate1).record(eq(METRIC), eq(42L), eq(Unit.NONE), eq(timestamp), argThat(t -> attributes == t.attributes())); verify(delegate2).record(eq(METRIC), eq(42L), eq(Unit.NONE), eq(timestamp), argThat(t -> attributes == t.attributes())); }
@Override protected void record(Metric label, Number value, Unit unit, Instant time, MultiRecorderContext context) { context.contexts.parallelStream().forEach(t -> t.record(label, value, unit, time)); }
MultiRecorder extends MetricRecorder<MultiRecorder.MultiRecorderContext> { @Override protected void record(Metric label, Number value, Unit unit, Instant time, MultiRecorderContext context) { context.contexts.parallelStream().forEach(t -> t.record(label, value, unit, time)); } }
MultiRecorder extends MetricRecorder<MultiRecorder.MultiRecorderContext> { @Override protected void record(Metric label, Number value, Unit unit, Instant time, MultiRecorderContext context) { context.contexts.parallelStream().forEach(t -> t.record(label, value, unit, time)); } MultiRecorder(List<MetricRecorder<?>> recorders); }
MultiRecorder extends MetricRecorder<MultiRecorder.MultiRecorderContext> { @Override protected void record(Metric label, Number value, Unit unit, Instant time, MultiRecorderContext context) { context.contexts.parallelStream().forEach(t -> t.record(label, value, unit, time)); } MultiRecorder(List<MetricRecorder<?>> recorders); }
MultiRecorder extends MetricRecorder<MultiRecorder.MultiRecorderContext> { @Override protected void record(Metric label, Number value, Unit unit, Instant time, MultiRecorderContext context) { context.contexts.parallelStream().forEach(t -> t.record(label, value, unit, time)); } MultiRecorder(List<MetricRecorder<?>> recorders); }
@Test public void countIsSentToAllDelegates() { MetricContext context = recorder.context(attributes); context.count(METRIC, 42L); verify(delegate1).count(eq(METRIC), eq(42L), argThat(t -> attributes == t.attributes())); verify(delegate2).count(eq(METRIC), eq(42L), argThat(t -> attributes == t.attributes())); }
@Override protected void count(Metric label, long delta, MultiRecorderContext context) { context.contexts.parallelStream().forEach(t -> t.count(label, delta)); }
MultiRecorder extends MetricRecorder<MultiRecorder.MultiRecorderContext> { @Override protected void count(Metric label, long delta, MultiRecorderContext context) { context.contexts.parallelStream().forEach(t -> t.count(label, delta)); } }
MultiRecorder extends MetricRecorder<MultiRecorder.MultiRecorderContext> { @Override protected void count(Metric label, long delta, MultiRecorderContext context) { context.contexts.parallelStream().forEach(t -> t.count(label, delta)); } MultiRecorder(List<MetricRecorder<?>> recorders); }
MultiRecorder extends MetricRecorder<MultiRecorder.MultiRecorderContext> { @Override protected void count(Metric label, long delta, MultiRecorderContext context) { context.contexts.parallelStream().forEach(t -> t.count(label, delta)); } MultiRecorder(List<MetricRecorder<?>> recorders); }
MultiRecorder extends MetricRecorder<MultiRecorder.MultiRecorderContext> { @Override protected void count(Metric label, long delta, MultiRecorderContext context) { context.contexts.parallelStream().forEach(t -> t.count(label, delta)); } MultiRecorder(List<MetricRecorder<?>> recorders); }
@Test public void closeIsSentToAllDelegates() { MetricContext context = recorder.context(attributes); context.close(); verify(delegate1).close(argThat(t -> attributes == t.attributes())); verify(delegate2).close(argThat(t -> attributes == t.attributes())); }
@Override protected void close(MultiRecorderContext context) { context.contexts.parallelStream().forEach(MetricContext::close); }
MultiRecorder extends MetricRecorder<MultiRecorder.MultiRecorderContext> { @Override protected void close(MultiRecorderContext context) { context.contexts.parallelStream().forEach(MetricContext::close); } }
MultiRecorder extends MetricRecorder<MultiRecorder.MultiRecorderContext> { @Override protected void close(MultiRecorderContext context) { context.contexts.parallelStream().forEach(MetricContext::close); } MultiRecorder(List<MetricRecorder<?>> recorders); }
MultiRecorder extends MetricRecorder<MultiRecorder.MultiRecorderContext> { @Override protected void close(MultiRecorderContext context) { context.contexts.parallelStream().forEach(MetricContext::close); } MultiRecorder(List<MetricRecorder<?>> recorders); }
MultiRecorder extends MetricRecorder<MultiRecorder.MultiRecorderContext> { @Override protected void close(MultiRecorderContext context) { context.contexts.parallelStream().forEach(MetricContext::close); } MultiRecorder(List<MetricRecorder<?>> recorders); }
@Test public void senses() throws Exception { final MetricRecorder recorder = new NullRecorder(); final Sensor sensor1 = mock(Sensor.class); final Sensor sensor2 = mock(Sensor.class); when(sensor1.addContext(any(TypedMap.class))).then(invocation -> invocation.getArgument(0)); when(sensor2.addContext(any(TypedMap.class))).then(invocation -> invocation.getArgument(0)); List<Sensor> sensors = new ArrayList<>(); sensors.add(sensor1); sensors.add(sensor2); MXBeanPoller poller = new MXBeanPoller(recorder, 1, sensors); Thread.sleep(1500); poller.shutdown(); final ArgumentMatcher<TypedMap> dataMatch = new DataMatcher(); verify(sensor1).addContext(argThat(dataMatch)); verify(sensor2).addContext(argThat(dataMatch)); verify(sensor1, atLeastOnce()).sense(any(MetricContext.class)); verify(sensor2, atLeastOnce()).sense(any(MetricContext.class)); }
public void shutdown() { running.set(false); executor.shutdown(); try { executor.awaitTermination(SHUTDOWN_TIMEOUT, TimeUnit.SECONDS); } catch (InterruptedException e) { executor.shutdownNow(); Thread.currentThread().interrupt(); } }
MXBeanPoller { public void shutdown() { running.set(false); executor.shutdown(); try { executor.awaitTermination(SHUTDOWN_TIMEOUT, TimeUnit.SECONDS); } catch (InterruptedException e) { executor.shutdownNow(); Thread.currentThread().interrupt(); } } }
MXBeanPoller { public void shutdown() { running.set(false); executor.shutdown(); try { executor.awaitTermination(SHUTDOWN_TIMEOUT, TimeUnit.SECONDS); } catch (InterruptedException e) { executor.shutdownNow(); Thread.currentThread().interrupt(); } } MXBeanPoller(final MetricRecorder metricRecorder, final int pollingIntervalSeconds, final List<Sensor> sensors); MXBeanPoller(final ScheduledExecutorService executor, final MetricRecorder metricRecorder, final int pollingIntervalSeconds, final List<Sensor> sensors); }
MXBeanPoller { public void shutdown() { running.set(false); executor.shutdown(); try { executor.awaitTermination(SHUTDOWN_TIMEOUT, TimeUnit.SECONDS); } catch (InterruptedException e) { executor.shutdownNow(); Thread.currentThread().interrupt(); } } MXBeanPoller(final MetricRecorder metricRecorder, final int pollingIntervalSeconds, final List<Sensor> sensors); MXBeanPoller(final ScheduledExecutorService executor, final MetricRecorder metricRecorder, final int pollingIntervalSeconds, final List<Sensor> sensors); static final MXBeanPoller withAutoShutdown( final MetricRecorder metricRecorder, int pollingIntervalSeconds, List<Sensor> sensors); void shutdown(); }
MXBeanPoller { public void shutdown() { running.set(false); executor.shutdown(); try { executor.awaitTermination(SHUTDOWN_TIMEOUT, TimeUnit.SECONDS); } catch (InterruptedException e) { executor.shutdownNow(); Thread.currentThread().interrupt(); } } MXBeanPoller(final MetricRecorder metricRecorder, final int pollingIntervalSeconds, final List<Sensor> sensors); MXBeanPoller(final ScheduledExecutorService executor, final MetricRecorder metricRecorder, final int pollingIntervalSeconds, final List<Sensor> sensors); static final MXBeanPoller withAutoShutdown( final MetricRecorder metricRecorder, int pollingIntervalSeconds, List<Sensor> sensors); void shutdown(); }
@Test public void testBasic() throws IOException, SAXException { String s = "the quick brown fox as well as some other fox and quick"; SpanQuery spanQuery = new SpanOrQuery(foxQuick, foxAs, new SpanTermQuery(new Term(field, "fox"))); Analyzer analyzer = new WhitespaceAnalyzer(); ContentHandler handler = new ToHTMLContentHandler(); RhapsodeXHTMLHandler xhtml = new RhapsodeXHTMLHandler(handler); RhapsodeXHTMLHandler.simpleInit(xhtml); Set<HighlightingQuery> highlightingQueries = new HashSet<>(); highlightingQueries.add(new HighlightingQuery(spanQuery, 1, "this-the-one")); DocHighlighter h = new DocHighlighter(); h.highlightSingleFieldValue(field, s, highlightingQueries, analyzer, new AtomicBoolean(false), xhtml); xhtml.endElement(H.BODY); xhtml.endDocument(); assertTrue(handler.toString().contains("<span id=\"first\" class=\"this-the-one\">quick brown fox</span>")); assertTrue(handler.toString().contains("<span class=\"this-the-one\">fox and quick</span>")); }
protected void highlightSingleFieldValue(String field, String s, Set<HighlightingQuery> highlightingQueries, Analyzer analyzer, AtomicBoolean anythingHighlighted, RhapsodeXHTMLHandler xhtml) throws IOException, SAXException { if (highlightingQueries == null || highlightingQueries.size() == 0) { xhtml.characters(s); return; } List<PriorityOffset> tokenOffsets = new ArrayList<>(); Map<Integer, Offset> charOffsets = new HashMap<>(); MemoryIndex index = new MemoryIndex(true); index.addField(field, s, analyzer); index.freeze(); IndexSearcher searcher = index.createSearcher(); for (HighlightingQuery hq : highlightingQueries) { addPriorityOffsets(searcher, hq, tokenOffsets, charOffsets); } List<PriorityOffset> winnowed = PriorityOffsetUtil.removeOverlapsAndSort(tokenOffsets); highlight(winnowed, charOffsets, s, anythingHighlighted, xhtml); }
DocHighlighter { protected void highlightSingleFieldValue(String field, String s, Set<HighlightingQuery> highlightingQueries, Analyzer analyzer, AtomicBoolean anythingHighlighted, RhapsodeXHTMLHandler xhtml) throws IOException, SAXException { if (highlightingQueries == null || highlightingQueries.size() == 0) { xhtml.characters(s); return; } List<PriorityOffset> tokenOffsets = new ArrayList<>(); Map<Integer, Offset> charOffsets = new HashMap<>(); MemoryIndex index = new MemoryIndex(true); index.addField(field, s, analyzer); index.freeze(); IndexSearcher searcher = index.createSearcher(); for (HighlightingQuery hq : highlightingQueries) { addPriorityOffsets(searcher, hq, tokenOffsets, charOffsets); } List<PriorityOffset> winnowed = PriorityOffsetUtil.removeOverlapsAndSort(tokenOffsets); highlight(winnowed, charOffsets, s, anythingHighlighted, xhtml); } }
DocHighlighter { protected void highlightSingleFieldValue(String field, String s, Set<HighlightingQuery> highlightingQueries, Analyzer analyzer, AtomicBoolean anythingHighlighted, RhapsodeXHTMLHandler xhtml) throws IOException, SAXException { if (highlightingQueries == null || highlightingQueries.size() == 0) { xhtml.characters(s); return; } List<PriorityOffset> tokenOffsets = new ArrayList<>(); Map<Integer, Offset> charOffsets = new HashMap<>(); MemoryIndex index = new MemoryIndex(true); index.addField(field, s, analyzer); index.freeze(); IndexSearcher searcher = index.createSearcher(); for (HighlightingQuery hq : highlightingQueries) { addPriorityOffsets(searcher, hq, tokenOffsets, charOffsets); } List<PriorityOffset> winnowed = PriorityOffsetUtil.removeOverlapsAndSort(tokenOffsets); highlight(winnowed, charOffsets, s, anythingHighlighted, xhtml); } }
DocHighlighter { protected void highlightSingleFieldValue(String field, String s, Set<HighlightingQuery> highlightingQueries, Analyzer analyzer, AtomicBoolean anythingHighlighted, RhapsodeXHTMLHandler xhtml) throws IOException, SAXException { if (highlightingQueries == null || highlightingQueries.size() == 0) { xhtml.characters(s); return; } List<PriorityOffset> tokenOffsets = new ArrayList<>(); Map<Integer, Offset> charOffsets = new HashMap<>(); MemoryIndex index = new MemoryIndex(true); index.addField(field, s, analyzer); index.freeze(); IndexSearcher searcher = index.createSearcher(); for (HighlightingQuery hq : highlightingQueries) { addPriorityOffsets(searcher, hq, tokenOffsets, charOffsets); } List<PriorityOffset> winnowed = PriorityOffsetUtil.removeOverlapsAndSort(tokenOffsets); highlight(winnowed, charOffsets, s, anythingHighlighted, xhtml); } void highlightDocsToFile(Path p, String defaultContentField, String embeddedPathField, List<String> fields, List<Document> docs, Collection<ComplexQuery> queries, Analyzer analyzer, String optionalStyleString, int targetAttachmentOffset); void highlightDocToFile(Path p, String defaultContentField, List<String> fields, Document doc, Collection<ComplexQuery> queries, Analyzer analyzer, String optionalStyleString); void highlightDocs(String defaultContentField, String embeddedPathField, List<String> fields, List<Document> docs, Collection<ComplexQuery> queries, Analyzer analyzer, AtomicBoolean anythingHighlighted, int targetOffset, RhapsodeXHTMLHandler xhtml); void highlightDoc(String contentField, List<String> fields, Document doc, Collection<ComplexQuery> queries, Analyzer analyzer, AtomicBoolean anythingHighlighted, RhapsodeXHTMLHandler xhtml); void setTableClass(String tableClass); void setTDClass(String tdClass); }
DocHighlighter { protected void highlightSingleFieldValue(String field, String s, Set<HighlightingQuery> highlightingQueries, Analyzer analyzer, AtomicBoolean anythingHighlighted, RhapsodeXHTMLHandler xhtml) throws IOException, SAXException { if (highlightingQueries == null || highlightingQueries.size() == 0) { xhtml.characters(s); return; } List<PriorityOffset> tokenOffsets = new ArrayList<>(); Map<Integer, Offset> charOffsets = new HashMap<>(); MemoryIndex index = new MemoryIndex(true); index.addField(field, s, analyzer); index.freeze(); IndexSearcher searcher = index.createSearcher(); for (HighlightingQuery hq : highlightingQueries) { addPriorityOffsets(searcher, hq, tokenOffsets, charOffsets); } List<PriorityOffset> winnowed = PriorityOffsetUtil.removeOverlapsAndSort(tokenOffsets); highlight(winnowed, charOffsets, s, anythingHighlighted, xhtml); } void highlightDocsToFile(Path p, String defaultContentField, String embeddedPathField, List<String> fields, List<Document> docs, Collection<ComplexQuery> queries, Analyzer analyzer, String optionalStyleString, int targetAttachmentOffset); void highlightDocToFile(Path p, String defaultContentField, List<String> fields, Document doc, Collection<ComplexQuery> queries, Analyzer analyzer, String optionalStyleString); void highlightDocs(String defaultContentField, String embeddedPathField, List<String> fields, List<Document> docs, Collection<ComplexQuery> queries, Analyzer analyzer, AtomicBoolean anythingHighlighted, int targetOffset, RhapsodeXHTMLHandler xhtml); void highlightDoc(String contentField, List<String> fields, Document doc, Collection<ComplexQuery> queries, Analyzer analyzer, AtomicBoolean anythingHighlighted, RhapsodeXHTMLHandler xhtml); void setTableClass(String tableClass); void setTDClass(String tdClass); }
@Test public void testPriority() throws IOException, SAXException { String s = "the quick brown fox as well as some other fox and quick"; SpanQuery spanQuery = new SpanOrQuery(foxQuick, foxAs); Analyzer analyzer = new WhitespaceAnalyzer(); ContentHandler handler = new ToHTMLContentHandler(); RhapsodeXHTMLHandler xhtml = new RhapsodeXHTMLHandler(handler); RhapsodeXHTMLHandler.simpleInit(xhtml); Set<HighlightingQuery> highlightingQueries = new HashSet<>(); highlightingQueries.add(new HighlightingQuery(spanQuery, 2, "this-the-one")); highlightingQueries.add(new HighlightingQuery(new SpanTermQuery(new Term(field, "fox")), 1, "this-the-two")); DocHighlighter h = new DocHighlighter(); h.highlightSingleFieldValue(field, s, highlightingQueries, analyzer, new AtomicBoolean(false), xhtml); xhtml.endElement(H.BODY); xhtml.endDocument(); assertTrue(handler.toString().contains("brown <span id=\"first\" class=\"this-the-two\">fox</span>")); assertTrue(handler.toString().contains(" other <span class=\"this-the-two\">fox</span>")); }
protected void highlightSingleFieldValue(String field, String s, Set<HighlightingQuery> highlightingQueries, Analyzer analyzer, AtomicBoolean anythingHighlighted, RhapsodeXHTMLHandler xhtml) throws IOException, SAXException { if (highlightingQueries == null || highlightingQueries.size() == 0) { xhtml.characters(s); return; } List<PriorityOffset> tokenOffsets = new ArrayList<>(); Map<Integer, Offset> charOffsets = new HashMap<>(); MemoryIndex index = new MemoryIndex(true); index.addField(field, s, analyzer); index.freeze(); IndexSearcher searcher = index.createSearcher(); for (HighlightingQuery hq : highlightingQueries) { addPriorityOffsets(searcher, hq, tokenOffsets, charOffsets); } List<PriorityOffset> winnowed = PriorityOffsetUtil.removeOverlapsAndSort(tokenOffsets); highlight(winnowed, charOffsets, s, anythingHighlighted, xhtml); }
DocHighlighter { protected void highlightSingleFieldValue(String field, String s, Set<HighlightingQuery> highlightingQueries, Analyzer analyzer, AtomicBoolean anythingHighlighted, RhapsodeXHTMLHandler xhtml) throws IOException, SAXException { if (highlightingQueries == null || highlightingQueries.size() == 0) { xhtml.characters(s); return; } List<PriorityOffset> tokenOffsets = new ArrayList<>(); Map<Integer, Offset> charOffsets = new HashMap<>(); MemoryIndex index = new MemoryIndex(true); index.addField(field, s, analyzer); index.freeze(); IndexSearcher searcher = index.createSearcher(); for (HighlightingQuery hq : highlightingQueries) { addPriorityOffsets(searcher, hq, tokenOffsets, charOffsets); } List<PriorityOffset> winnowed = PriorityOffsetUtil.removeOverlapsAndSort(tokenOffsets); highlight(winnowed, charOffsets, s, anythingHighlighted, xhtml); } }
DocHighlighter { protected void highlightSingleFieldValue(String field, String s, Set<HighlightingQuery> highlightingQueries, Analyzer analyzer, AtomicBoolean anythingHighlighted, RhapsodeXHTMLHandler xhtml) throws IOException, SAXException { if (highlightingQueries == null || highlightingQueries.size() == 0) { xhtml.characters(s); return; } List<PriorityOffset> tokenOffsets = new ArrayList<>(); Map<Integer, Offset> charOffsets = new HashMap<>(); MemoryIndex index = new MemoryIndex(true); index.addField(field, s, analyzer); index.freeze(); IndexSearcher searcher = index.createSearcher(); for (HighlightingQuery hq : highlightingQueries) { addPriorityOffsets(searcher, hq, tokenOffsets, charOffsets); } List<PriorityOffset> winnowed = PriorityOffsetUtil.removeOverlapsAndSort(tokenOffsets); highlight(winnowed, charOffsets, s, anythingHighlighted, xhtml); } }
DocHighlighter { protected void highlightSingleFieldValue(String field, String s, Set<HighlightingQuery> highlightingQueries, Analyzer analyzer, AtomicBoolean anythingHighlighted, RhapsodeXHTMLHandler xhtml) throws IOException, SAXException { if (highlightingQueries == null || highlightingQueries.size() == 0) { xhtml.characters(s); return; } List<PriorityOffset> tokenOffsets = new ArrayList<>(); Map<Integer, Offset> charOffsets = new HashMap<>(); MemoryIndex index = new MemoryIndex(true); index.addField(field, s, analyzer); index.freeze(); IndexSearcher searcher = index.createSearcher(); for (HighlightingQuery hq : highlightingQueries) { addPriorityOffsets(searcher, hq, tokenOffsets, charOffsets); } List<PriorityOffset> winnowed = PriorityOffsetUtil.removeOverlapsAndSort(tokenOffsets); highlight(winnowed, charOffsets, s, anythingHighlighted, xhtml); } void highlightDocsToFile(Path p, String defaultContentField, String embeddedPathField, List<String> fields, List<Document> docs, Collection<ComplexQuery> queries, Analyzer analyzer, String optionalStyleString, int targetAttachmentOffset); void highlightDocToFile(Path p, String defaultContentField, List<String> fields, Document doc, Collection<ComplexQuery> queries, Analyzer analyzer, String optionalStyleString); void highlightDocs(String defaultContentField, String embeddedPathField, List<String> fields, List<Document> docs, Collection<ComplexQuery> queries, Analyzer analyzer, AtomicBoolean anythingHighlighted, int targetOffset, RhapsodeXHTMLHandler xhtml); void highlightDoc(String contentField, List<String> fields, Document doc, Collection<ComplexQuery> queries, Analyzer analyzer, AtomicBoolean anythingHighlighted, RhapsodeXHTMLHandler xhtml); void setTableClass(String tableClass); void setTDClass(String tdClass); }
DocHighlighter { protected void highlightSingleFieldValue(String field, String s, Set<HighlightingQuery> highlightingQueries, Analyzer analyzer, AtomicBoolean anythingHighlighted, RhapsodeXHTMLHandler xhtml) throws IOException, SAXException { if (highlightingQueries == null || highlightingQueries.size() == 0) { xhtml.characters(s); return; } List<PriorityOffset> tokenOffsets = new ArrayList<>(); Map<Integer, Offset> charOffsets = new HashMap<>(); MemoryIndex index = new MemoryIndex(true); index.addField(field, s, analyzer); index.freeze(); IndexSearcher searcher = index.createSearcher(); for (HighlightingQuery hq : highlightingQueries) { addPriorityOffsets(searcher, hq, tokenOffsets, charOffsets); } List<PriorityOffset> winnowed = PriorityOffsetUtil.removeOverlapsAndSort(tokenOffsets); highlight(winnowed, charOffsets, s, anythingHighlighted, xhtml); } void highlightDocsToFile(Path p, String defaultContentField, String embeddedPathField, List<String> fields, List<Document> docs, Collection<ComplexQuery> queries, Analyzer analyzer, String optionalStyleString, int targetAttachmentOffset); void highlightDocToFile(Path p, String defaultContentField, List<String> fields, Document doc, Collection<ComplexQuery> queries, Analyzer analyzer, String optionalStyleString); void highlightDocs(String defaultContentField, String embeddedPathField, List<String> fields, List<Document> docs, Collection<ComplexQuery> queries, Analyzer analyzer, AtomicBoolean anythingHighlighted, int targetOffset, RhapsodeXHTMLHandler xhtml); void highlightDoc(String contentField, List<String> fields, Document doc, Collection<ComplexQuery> queries, Analyzer analyzer, AtomicBoolean anythingHighlighted, RhapsodeXHTMLHandler xhtml); void setTableClass(String tableClass); void setTDClass(String tdClass); }