Example usage for com.google.common.math IntMath gcd

List of usage examples for com.google.common.math IntMath gcd

Introduction

In this page you can find the example usage for com.google.common.math IntMath gcd.

Prototype

public static int gcd(int a, int b) 

Source Link

Document

Returns the greatest common divisor of a, b .

Usage

From source file:org.mayocat.image.util.ImageUtils.java

/**
 * @param width the width of the image to compute the ratio for
 * @param height the height of the image to compute the ratio for
 * @return the image ratio, under the form <code</>x:y</code> where x represents the horizontal dimension and y the
 *         vertical dimension of the ratio
 *///  www .j a  v  a  2  s. com
public static String imageRatio(Integer width, Integer height) {
    Preconditions.checkArgument(!(width == 0 && height == 0),
            "Cannot compute image ration when both width and height are zero");

    Integer gcd = IntMath.gcd(width, height);
    return (width / gcd) + ":" + (height / gcd);
}

From source file:com.datatorrent.stram.plan.physical.StreamMapping.java

public static PTOperator createSlidingUnifier(StreamMeta streamMeta, PhysicalPlan plan,
        int operatorApplicationWindowCount, int slidingWindowCount) {
    int gcd = IntMath.gcd(operatorApplicationWindowCount, slidingWindowCount);
    OperatorMeta um = streamMeta.getSource().getSlidingUnifier(operatorApplicationWindowCount / gcd, gcd,
            slidingWindowCount / gcd);/*from  w w  w.j  a  va2  s  .c om*/
    PTOperator pu = plan.newOperator(um, um.getName());

    Operator unifier = um.getOperator();
    PortMappingDescriptor mergeDesc = new PortMappingDescriptor();
    Operators.describe(unifier, mergeDesc);
    if (mergeDesc.outputPorts.size() != 1) {
        throw new AssertionError(
                "Unifier must have a single output port, instead found : " + mergeDesc.outputPorts);
    }
    pu.unifiedOperatorMeta = streamMeta.getSource().getOperatorMeta();
    pu.outputs.add(new PTOutput(mergeDesc.outputPorts.keySet().iterator().next(), streamMeta, pu));
    plan.newOpers.put(pu, unifier);
    return pu;
}

From source file:com.datatorrent.stram.engine.Node.java

@SuppressWarnings("unchecked")
public void activate() {
    alive = true;//from www .  java 2 s.co m
    APPLICATION_WINDOW_COUNT = context.getValue(OperatorContext.APPLICATION_WINDOW_COUNT);
    if (context.getValue(OperatorContext.SLIDE_BY_WINDOW_COUNT) != null) {
        int slidingWindowCount = context.getValue(OperatorContext.SLIDE_BY_WINDOW_COUNT);
        APPLICATION_WINDOW_COUNT = IntMath.gcd(APPLICATION_WINDOW_COUNT, slidingWindowCount);
    }
    DAG_CHECKPOINT_WINDOW_COUNT = context.getValue(Context.DAGContext.CHECKPOINT_WINDOW_COUNT);
    CHECKPOINT_WINDOW_COUNT = context.getValue(OperatorContext.CHECKPOINT_WINDOW_COUNT);
    Collection<StatsListener> statsListeners = context.getValue(OperatorContext.STATS_LISTENERS);

    if (CHECKPOINT_WINDOW_COUNT % APPLICATION_WINDOW_COUNT != 0) {
        logger.warn(
                "{} is not exact multiple of {} for operator {}. This may cause side effects such as processing to begin without beginWindow preceding it in the first window after activation.",
                OperatorContext.CHECKPOINT_WINDOW_COUNT, OperatorContext.APPLICATION_WINDOW_COUNT, operator);
    }

    PROCESSING_MODE = context.getValue(OperatorContext.PROCESSING_MODE);
    if (PROCESSING_MODE == ProcessingMode.EXACTLY_ONCE && CHECKPOINT_WINDOW_COUNT != 1) {
        logger.warn("Ignoring {} attribute in favor of {} processing mode",
                OperatorContext.CHECKPOINT_WINDOW_COUNT.getSimpleName(), ProcessingMode.EXACTLY_ONCE.name());
        CHECKPOINT_WINDOW_COUNT = 1;
    }

    activateSinks();
    if (operator instanceof Operator.ActivationListener) {
        ((Operator.ActivationListener<OperatorContext>) operator).activate(context);
    }

    if (statsListeners != null) {
        Iterator<StatsListener> iterator = statsListeners.iterator();
        while (iterator.hasNext()) {
            DATA_TUPLE_AWARE = iterator.next().getClass()
                    .isAnnotationPresent(StatsListener.DataQueueSize.class);
            if (DATA_TUPLE_AWARE) {
                break;
            }
        }
    }

    if (!DATA_TUPLE_AWARE && (operator instanceof StatsListener)) {
        DATA_TUPLE_AWARE = operator.getClass().isAnnotationPresent(StatsListener.DataQueueSize.class);
    }
    /*
     * If there were any requests which needed to be executed before the operator started
     * its normal execution, execute those requests now - e.g. Restarting the operator
     * recording for the operators which failed while recording and being replaced.
     */
    handleRequests(currentWindowId);
}

From source file:edu.mit.streamjit.impl.common.MessageConstraint.java

private static int lcm(int a, int b) {
    //Divide before multiplying for overflow resistance.
    return a / IntMath.gcd(a, b) * b;
}