public static long gcd(List<Long> numbers) { if (numbers == null) { throw new IllegalArgumentException("Null list provided"); } if (numbers.size() == 0) { throw new IllegalArgumentException("List of size 0 provided"); } long result = numbers.get(0); for (int i = 1; i < numbers.size(); i++) { result = gcd(result, numbers.get(i)); } return result; }
public static long gcd(List<Long> numbers) { if (numbers == null) { throw new IllegalArgumentException("Null list provided"); } if (numbers.size() == 0) { throw new IllegalArgumentException("List of size 0 provided"); } long result = numbers.get(0); for (int i = 1; i < numbers.size(); i++) { result = gcd(result, numbers.get(i)); } return result; }
public static long gcd(List<Long> numbers) { if (numbers == null) { throw new IllegalArgumentException("Null list provided"); } if (numbers.size() == 0) { throw new IllegalArgumentException("List of size 0 provided"); } long result = numbers.get(0); for (int i = 1; i < numbers.size(); i++) { result = gcd(result, numbers.get(i)); } return result; }
public static long gcd(List<Long> numbers) { if (numbers == null) { throw new IllegalArgumentException("Null list provided"); } if (numbers.size() == 0) { throw new IllegalArgumentException("List of size 0 provided"); } long result = numbers.get(0); for (int i = 1; i < numbers.size(); i++) { result = gcd(result, numbers.get(i)); } return result; }
public static long gcd(List<Long> numbers) { if (numbers == null) { throw new IllegalArgumentException("Null list provided"); } if (numbers.size() == 0) { throw new IllegalArgumentException("List of size 0 provided"); } long result = numbers.get(0); for (int i = 1; i < numbers.size(); i++) { result = gcd(result, numbers.get(i)); } return result; }
/** * Computes the triggering interval to use during the execution of this {@link JobNode} */ private long computeTriggerInterval(Collection<OperatorSpec> reachableOperators) { List<Long> windowTimerIntervals = reachableOperators.stream() .filter(spec -> spec.getOpCode() == OperatorSpec.OpCode.WINDOW) .map(spec -> ((WindowOperatorSpec) spec).getDefaultTriggerMs()) .collect(Collectors.toList()); // Filter out the join operators, and obtain a list of their ttl values List<Long> joinTtlIntervals = reachableOperators.stream() .filter(spec -> spec instanceof JoinOperatorSpec) .map(spec -> ((JoinOperatorSpec) spec).getTtlMs()) .collect(Collectors.toList()); // Combine both the above lists List<Long> candidateTimerIntervals = new ArrayList<>(joinTtlIntervals); candidateTimerIntervals.addAll(windowTimerIntervals); if (candidateTimerIntervals.isEmpty()) { return -1; } // Compute the gcd of the resultant list return MathUtil.gcd(candidateTimerIntervals); }
/** * Computes the triggering interval to use during the execution of this {@link JobNode} */ private long computeTriggerInterval(Collection<OperatorSpec> reachableOperators) { List<Long> windowTimerIntervals = reachableOperators.stream() .filter(spec -> spec.getOpCode() == OperatorSpec.OpCode.WINDOW) .map(spec -> ((WindowOperatorSpec) spec).getDefaultTriggerMs()) .collect(Collectors.toList()); // Filter out the join operators, and obtain a list of their ttl values List<Long> joinTtlIntervals = reachableOperators.stream() .filter(spec -> spec instanceof JoinOperatorSpec) .map(spec -> ((JoinOperatorSpec) spec).getTtlMs()) .collect(Collectors.toList()); // Combine both the above lists List<Long> candidateTimerIntervals = new ArrayList<>(joinTtlIntervals); candidateTimerIntervals.addAll(windowTimerIntervals); if (candidateTimerIntervals.isEmpty()) { return -1; } // Compute the gcd of the resultant list return MathUtil.gcd(candidateTimerIntervals); }
/** * Computes the triggering interval to use during the execution of this {@link JobNode} */ private long computeTriggerInterval(Collection<OperatorSpec> reachableOperators) { List<Long> windowTimerIntervals = reachableOperators.stream() .filter(spec -> spec.getOpCode() == OperatorSpec.OpCode.WINDOW) .map(spec -> ((WindowOperatorSpec) spec).getDefaultTriggerMs()) .collect(Collectors.toList()); // Filter out the join operators, and obtain a list of their ttl values List<Long> joinTtlIntervals = reachableOperators.stream() .filter(spec -> spec instanceof JoinOperatorSpec) .map(spec -> ((JoinOperatorSpec) spec).getTtlMs()) .collect(Collectors.toList()); // Combine both the above lists List<Long> candidateTimerIntervals = new ArrayList<>(joinTtlIntervals); candidateTimerIntervals.addAll(windowTimerIntervals); if (candidateTimerIntervals.isEmpty()) { return -1; } // Compute the gcd of the resultant list return MathUtil.gcd(candidateTimerIntervals); }
/** * Computes the triggering interval to use during the execution of this {@link JobNode} */ private long computeTriggerInterval(Collection<OperatorSpec> reachableOperators) { List<Long> windowTimerIntervals = reachableOperators.stream() .filter(spec -> spec.getOpCode() == OperatorSpec.OpCode.WINDOW) .map(spec -> ((WindowOperatorSpec) spec).getDefaultTriggerMs()) .collect(Collectors.toList()); // Filter out the join operators, and obtain a list of their ttl values List<Long> joinTtlIntervals = reachableOperators.stream() .filter(spec -> spec instanceof JoinOperatorSpec) .map(spec -> ((JoinOperatorSpec) spec).getTtlMs()) .collect(Collectors.toList()); // Combine both the above lists List<Long> candidateTimerIntervals = new ArrayList<>(joinTtlIntervals); candidateTimerIntervals.addAll(windowTimerIntervals); if (candidateTimerIntervals.isEmpty()) { return -1; } // Compute the gcd of the resultant list return MathUtil.gcd(candidateTimerIntervals); }
/** * Computes the triggering interval to use during the execution of this {@link JobNode} */ private long computeTriggerInterval(Collection<OperatorSpec> reachableOperators) { List<Long> windowTimerIntervals = reachableOperators.stream() .filter(spec -> spec.getOpCode() == OperatorSpec.OpCode.WINDOW) .map(spec -> ((WindowOperatorSpec) spec).getDefaultTriggerMs()) .collect(Collectors.toList()); // Filter out the join operators, and obtain a list of their ttl values List<Long> joinTtlIntervals = reachableOperators.stream() .filter(spec -> spec instanceof JoinOperatorSpec) .map(spec -> ((JoinOperatorSpec) spec).getTtlMs()) .collect(Collectors.toList()); // Combine both the above lists List<Long> candidateTimerIntervals = new ArrayList<>(joinTtlIntervals); candidateTimerIntervals.addAll(windowTimerIntervals); if (candidateTimerIntervals.isEmpty()) { return -1; } // Compute the gcd of the resultant list return MathUtil.gcd(candidateTimerIntervals); }
@Test(expected = IllegalArgumentException.class) public void testGcdWithEmptyInputs() { MathUtil.gcd(Collections.emptyList()); }
@Test(expected = IllegalArgumentException.class) public void testGcdWithNullInputs() { MathUtil.gcd(null); }
/** * Get the default triggering interval for this {@link WindowOperatorSpec} * * This is defined as the GCD of all triggering intervals across all {@link TimeBasedTrigger}s configured for * this {@link WindowOperatorSpec}. * * @return the default triggering interval */ public long getDefaultTriggerMs() { List<TimeBasedTrigger> timeBasedTriggers = new ArrayList<>(); if (window.getDefaultTrigger() != null) { timeBasedTriggers.addAll(getTimeBasedTriggers(window.getDefaultTrigger())); } if (window.getEarlyTrigger() != null) { timeBasedTriggers.addAll(getTimeBasedTriggers(window.getEarlyTrigger())); } if (window.getLateTrigger() != null) { timeBasedTriggers.addAll(getTimeBasedTriggers(window.getLateTrigger())); } LOG.info("Got {} time-based triggers", timeBasedTriggers.size()); List<Long> candidateDurations = timeBasedTriggers.stream() .map(timeBasedTrigger -> timeBasedTrigger.getDuration().toMillis()) .collect(Collectors.toList()); return MathUtil.gcd(candidateDurations); }
/** * Get the default triggering interval for this {@link WindowOperatorSpec} * * This is defined as the GCD of all triggering intervals across all {@link TimeBasedTrigger}s configured for * this {@link WindowOperatorSpec}. * * @return the default triggering interval */ public long getDefaultTriggerMs() { List<TimeBasedTrigger> timeBasedTriggers = new ArrayList<>(); if (window.getDefaultTrigger() != null) { timeBasedTriggers.addAll(getTimeBasedTriggers(window.getDefaultTrigger())); } if (window.getEarlyTrigger() != null) { timeBasedTriggers.addAll(getTimeBasedTriggers(window.getEarlyTrigger())); } if (window.getLateTrigger() != null) { timeBasedTriggers.addAll(getTimeBasedTriggers(window.getLateTrigger())); } LOG.info("Got {} time-based triggers", timeBasedTriggers.size()); List<Long> candidateDurations = timeBasedTriggers.stream() .map(timeBasedTrigger -> timeBasedTrigger.getDuration().toMillis()) .collect(Collectors.toList()); return MathUtil.gcd(candidateDurations); }
@Test public void testGcdWithValidInputs() { // gcd(x, x) = x assertEquals(2, MathUtil.gcd(ImmutableList.of(2L, 2L))); assertEquals(15, MathUtil.gcd(ImmutableList.of(15L))); assertEquals(1, MathUtil.gcd(ImmutableList.of(1L))); // gcd(0,x) = x assertEquals(2, MathUtil.gcd(ImmutableList.of(2L, 0L))); // gcd(1,x) = 1 assertEquals(1, MathUtil.gcd(ImmutableList.of(10L, 20L, 30L, 40L, 50L, 1L))); // other happy path test cases assertEquals(10, MathUtil.gcd(ImmutableList.of(10L, 20L, 30L, 40L, 50L, 0L))); assertEquals(10, MathUtil.gcd(ImmutableList.of(10L, 20L, 30L, 40L, 50L))); assertEquals(5, MathUtil.gcd(ImmutableList.of(25L, 35L, 45L, 55L))); assertEquals(1, MathUtil.gcd(ImmutableList.of(25L, 35L, 45L, 55L, 13L))); }
/** * Get the default triggering interval for this {@link WindowOperatorSpec} * * This is defined as the GCD of all triggering intervals across all {@link TimeBasedTrigger}s configured for * this {@link WindowOperatorSpec}. * * @return the default triggering interval */ public long getDefaultTriggerMs() { List<TimeBasedTrigger> timeBasedTriggers = new ArrayList<>(); if (window.getDefaultTrigger() != null) { timeBasedTriggers.addAll(getTimeBasedTriggers(window.getDefaultTrigger())); } if (window.getEarlyTrigger() != null) { timeBasedTriggers.addAll(getTimeBasedTriggers(window.getEarlyTrigger())); } if (window.getLateTrigger() != null) { timeBasedTriggers.addAll(getTimeBasedTriggers(window.getLateTrigger())); } LOG.info("Got {} time-based triggers", timeBasedTriggers.size()); List<Long> candidateDurations = timeBasedTriggers.stream() .map(timeBasedTrigger -> timeBasedTrigger.getDuration().toMillis()) .collect(Collectors.toList()); return MathUtil.gcd(candidateDurations); }
/** * Get the default triggering interval for this {@link WindowOperatorSpec} * * This is defined as the GCD of all triggering intervals across all {@link TimeBasedTrigger}s configured for * this {@link WindowOperatorSpec}. * * @return the default triggering interval */ public long getDefaultTriggerMs() { List<TimeBasedTrigger> timeBasedTriggers = new ArrayList<>(); if (window.getDefaultTrigger() != null) { timeBasedTriggers.addAll(getTimeBasedTriggers(window.getDefaultTrigger())); } if (window.getEarlyTrigger() != null) { timeBasedTriggers.addAll(getTimeBasedTriggers(window.getEarlyTrigger())); } if (window.getLateTrigger() != null) { timeBasedTriggers.addAll(getTimeBasedTriggers(window.getLateTrigger())); } LOG.info("Got {} time-based triggers", timeBasedTriggers.size()); List<Long> candidateDurations = timeBasedTriggers.stream() .map(timeBasedTrigger -> timeBasedTrigger.getDuration().toMillis()) .collect(Collectors.toList()); return MathUtil.gcd(candidateDurations); }
/** * Get the default triggering interval for this {@link WindowOperatorSpec} * * This is defined as the GCD of all triggering intervals across all {@link TimeBasedTrigger}s configured for * this {@link WindowOperatorSpec}. * * @return the default triggering interval */ public long getDefaultTriggerMs() { List<TimeBasedTrigger> timeBasedTriggers = new ArrayList<>(); if (window.getDefaultTrigger() != null) { timeBasedTriggers.addAll(getTimeBasedTriggers(window.getDefaultTrigger())); } if (window.getEarlyTrigger() != null) { timeBasedTriggers.addAll(getTimeBasedTriggers(window.getEarlyTrigger())); } if (window.getLateTrigger() != null) { timeBasedTriggers.addAll(getTimeBasedTriggers(window.getLateTrigger())); } LOG.info("Got {} time-based triggers", timeBasedTriggers.size()); List<Long> candidateDurations = timeBasedTriggers.stream() .map(timeBasedTrigger -> timeBasedTrigger.getDuration().toMillis()) .collect(Collectors.toList()); return MathUtil.gcd(candidateDurations); }