@Test @SuppressWarnings("unchecked") public void testTopEmpty() { Pipeline p = TestPipeline.create(); PCollection<String> input = p.apply(Create.of(Arrays.asList(EMPTY_COLLECTION)).withCoder(StringUtf8Coder.of())); PCollection<List<String>> top1 = input.apply(Top.of(1, new OrderByLength())); PCollection<List<String>> top2 = input.apply(Top.<String>largest(2)); PCollection<List<String>> top3 = input.apply(Top.<String>smallest(3)); PCollection<KV<String, Integer>> inputTable = createEmptyInputTable(p); PCollection<KV<String, List<Integer>>> largestPerKey = inputTable.apply(Top.<String, Integer>largestPerKey(2)); PCollection<KV<String, List<Integer>>> smallestPerKey = inputTable.apply(Top.<String, Integer>smallestPerKey(2)); DataflowAssert.thatSingletonIterable(top1).empty(); DataflowAssert.thatSingletonIterable(top2).empty(); DataflowAssert.thatSingletonIterable(top3).empty(); DataflowAssert.that(largestPerKey).empty(); DataflowAssert.that(smallestPerKey).empty(); p.run(); }
@Test @SuppressWarnings("unchecked") public void testTop() { Pipeline p = TestPipeline.create(); PCollection<String> input = p.apply(Create.of(Arrays.asList(COLLECTION)).withCoder(StringUtf8Coder.of())); PCollection<List<String>> top1 = input.apply(Top.of(1, new OrderByLength())); PCollection<List<String>> top2 = input.apply(Top.<String>largest(2)); PCollection<List<String>> top3 = input.apply(Top.<String>smallest(3)); PCollection<KV<String, Integer>> inputTable = createInputTable(p); PCollection<KV<String, List<Integer>>> largestPerKey = inputTable.apply(Top.<String, Integer>largestPerKey(2)); PCollection<KV<String, List<Integer>>> smallestPerKey = inputTable.apply(Top.<String, Integer>smallestPerKey(2)); DataflowAssert.thatSingletonIterable(top1).containsInAnyOrder(Arrays.asList("bb")); DataflowAssert.thatSingletonIterable(top2).containsInAnyOrder("z", "c"); DataflowAssert.thatSingletonIterable(top3).containsInAnyOrder("a", "bb", "c"); DataflowAssert.that(largestPerKey) .containsInAnyOrder(KV.of("a", Arrays.asList(3, 2)), KV.of("b", Arrays.asList(100, 10))); DataflowAssert.that(smallestPerKey) .containsInAnyOrder(KV.of("a", Arrays.asList(1, 2)), KV.of("b", Arrays.asList(1, 10))); p.run(); }
// This is a purely compile-time test. If the code compiles, then it worked. @Test public void testPerKeySerializabilityRequirement() { Pipeline p = TestPipeline.create(); p.apply( "CreateCollection", Create.of(Arrays.asList(COLLECTION)).withCoder(StringUtf8Coder.of())); PCollection<KV<String, Integer>> inputTable = createInputTable(p); inputTable.apply(Top.<String, Integer, IntegerComparator>perKey(1, new IntegerComparator())); inputTable.apply( "PerKey2", Top.<String, Integer, IntegerComparator2>perKey(1, new IntegerComparator2())); }
/** * Show the specified message in a modal dialog. If the user clicks on the "Cancel" button, then * throw an exception. This gives the user the option of not continuing the execution, something * that is particularly useful if continuing execution will result in repeated warnings. NOTE: If * this is called outside the swing event thread, then no cancel button is presented and no * CancelException will be thrown. This is because the displaying of the message must be deferred * to the swing event thread, according to the swing architecture, or we could get deadlock or * rendering problems. * * @param info The message. * @exception ptolemy.util.CancelException If the user clicks on the "Cancel" button. */ protected void _warning(final String info) throws CancelException { // In swing, updates to showing graphics must be done in the // event thread. If we are in the event thread, then proceed. // Otherwise, defer. if (EventQueue.isDispatchThread()) { super._warning(info); } else { Runnable doWarning = new Runnable() { public void run() { Object[] options = {"OK"}; Object[] message = new Object[1]; // If the message lines are longer than 80 characters, we split it // into shorter new line separated strings. // Running vergil on a HSIF .xml file will create a line longer // than 80 characters message[0] = StringUtilities.ellipsis(info, StringUtilities.ELLIPSIS_LENGTH_LONG); // Show the MODAL dialog /*int selected =*/ JOptionPane.showOptionDialog( getContext(), message, "Warning", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]); } }; Top.deferIfNecessary(doWarning); } }
@Test public void testDisplayData() { Top.Largest<Integer> comparer = new Top.Largest<Integer>(); Combine.Globally<Integer, List<Integer>> top = Top.of(1234, comparer); DisplayData displayData = DisplayData.from(top); assertThat(displayData, hasDisplayItem("count", 1234)); assertThat(displayData, hasDisplayItem("comparer", comparer.getClass())); }
/** * Show the specified error message. This is deferred to execute in the swing event thread if it * is called outside that thread. * * @param info The message. */ protected void _error(final String info) { Runnable doMessage = new Runnable() { public void run() { GraphicalMessageHandler.super._error(info); } }; Top.deferIfNecessary(doMessage); }
@Test public void testCountConstraint() { Pipeline p = TestPipeline.create(); PCollection<String> input = p.apply(Create.of(Arrays.asList(COLLECTION)).withCoder(StringUtf8Coder.of())); expectedEx.expect(IllegalArgumentException.class); expectedEx.expectMessage(Matchers.containsString(">= 0")); input.apply(Top.of(-1, new OrderByLength())); }
@Test public void testTopGetNames() { assertEquals("Top.Globally", Top.of(1, new OrderByLength()).getName()); assertEquals("Smallest.Globally", Top.smallest(1).getName()); assertEquals("Largest.Globally", Top.largest(2).getName()); assertEquals("Top.PerKey", Top.perKey(1, new IntegerComparator()).getName()); assertEquals("Smallest.PerKey", Top.<String, Integer>smallestPerKey(1).getName()); assertEquals("Largest.PerKey", Top.<String, Integer>largestPerKey(2).getName()); }
@Test public void testTopEmptyWithIncompatibleWindows() { Pipeline p = TestPipeline.create(); Bound<String> windowingFn = Window.<String>into(FixedWindows.of(Duration.standardDays(10L))); PCollection<String> input = p.apply(Create.timestamped(Collections.<String>emptyList(), Collections.<Long>emptyList())) .apply(windowingFn); expectedEx.expect(IllegalStateException.class); expectedEx.expectMessage("Top"); expectedEx.expectMessage("GlobalWindows"); expectedEx.expectMessage("withoutDefaults"); expectedEx.expectMessage("asSingletonView"); input.apply(Top.of(1, new OrderByLength())); }
/** * Show the specified message and throwable information in a modal dialog. If the user clicks on * the "Cancel" button, then throw an exception. This gives the user the option of not continuing * the execution, something that is particularly useful if continuing execution will result in * repeated warnings. By default, only the message of the throwable is shown. The stack trace * information is only shown if the user clicks on the "Display Stack Trace" button. NOTE: If this * is called outside the swing event thread, then no cancel button is presented and no * CancelException will be thrown. This is because the displaying of the message must be deferred * to the swing event thread, according to the swing architecture, or we could get deadlock or * rendering problems. * * @param info The message. * @param throwable The throwable. * @exception ptolemy.util.CancelException If the user clicks on the "Cancel" button. */ protected void _warning(final String info, final Throwable throwable) throws CancelException { // In swing, updates to showing graphics must be done in the // event thread. If we are in the event thread, then proceed. // Otherwise, defer. if (EventQueue.isDispatchThread()) { super._warning(info, throwable); } else { Runnable doWarning = new Runnable() { public void run() { Object[] message = new Object[1]; message[0] = StringUtilities.ellipsis(info, StringUtilities.ELLIPSIS_LENGTH_LONG); Object[] options = {"OK", "Display Stack Trace"}; // Show the MODAL dialog int selected = JOptionPane.showOptionDialog( getContext(), message, "Warning", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]); if (selected == 1) { _showStackTrace(throwable, info); } } }; Top.deferIfNecessary(doWarning); } }
public static void main(String[] args) throws IOException { BufferedReader bufferedReader = new BufferedReader(new FileReader(new File("islands.in"))); PrintWriter printWriter = new PrintWriter(new File("islands.out")); int N = Integer.parseInt(bufferedReader.readLine()); int landscape[] = new int[N]; for (int i = 0; i < N; i++) { landscape[i] = Integer.parseInt(bufferedReader.readLine()); } for (int i = 0; i < N; i++) { int prev, next, current = landscape[i]; if (i == 0) { prev = next = landscape[i + 1]; } else if (i == N - 1) { prev = next = landscape[i - 1]; } else { prev = landscape[i - 1]; next = landscape[i + 1]; } if (prev < current && next < current) { if (!tops.containsKey(current)) tops.put(current, new LinkedList<Top>()); Top newTop = new Top(); if (Bottom.lastBottom != null) { newTop.left = Bottom.lastBottom; Bottom.lastBottom.right = newTop; } tops.get(current).add(newTop); Top.lastTop = newTop; } else if (prev > current && next > current) { if (!bottoms.containsKey(current)) bottoms.put(current, new LinkedList<Bottom>()); Bottom newBottom = new Bottom(); if (Top.lastTop != null) { newBottom.left = Top.lastTop; Top.lastTop.right = newBottom; } bottoms.get(current).add(newBottom); Bottom.lastBottom = newBottom; } else if (prev > current && next == current) { while (i < N - 1 && landscape[i + 1] == current) { i++; } if (i == N - 1 || landscape[i + 1] > current) { if (!bottoms.containsKey(current)) bottoms.put(current, new LinkedList<Bottom>()); Bottom newBottom = new Bottom(); if (Top.lastTop != null) { newBottom.left = Top.lastTop; Top.lastTop.right = newBottom; } bottoms.get(current).add(newBottom); Bottom.lastBottom = newBottom; } } else if (prev < current && next == current) { while (i < N - 1 && landscape[i + 1] == current) { i++; } if (i == N - 1 || landscape[i + 1] < current) { if (!tops.containsKey(current)) tops.put(current, new LinkedList<Top>()); Top newTop = new Top(); if (Bottom.lastBottom != null) { newTop.left = Bottom.lastBottom; Bottom.lastBottom.right = newTop; } tops.get(current).add(newTop); Top.lastTop = newTop; } } } Arrays.sort(landscape); int maxActive = 0; int prev = 0; for (int i : landscape) { if (i != prev) { if (bottoms.containsKey(i)) for (Bottom b : bottoms.get(i)) { if (b.right != null) b.right.activate(); if (b.left != null) b.left.activate(); } if (tops.containsKey(i)) for (Top t : tops.get(i)) { t.sink(); } maxActive = Math.max(maxActive, Top.totalActive); } } printWriter.println(maxActive); printWriter.flush(); }