InterpolatingVertexSizeTransformer.java :  » Science » jung-2-2_0_1 » edu » uci » ics » jung » visualization » decorators » Java Open Source

Java Open Source » Science » jung 2 2_0_1 
jung 2 2_0_1 » edu » uci » ics » jung » visualization » decorators » InterpolatingVertexSizeTransformer.java
/*
 * Created on Nov 3, 2005
 *
 * Copyright (c) 2005, the JUNG Project and the Regents of the University 
 * of California
 * All rights reserved.
 *
 * This software is open-source under the BSD license; see either
 * "license.txt" or
 * http://jung.sourceforge.net/license.txt for a description.
 */
package edu.uci.ics.jung.visualization.decorators;

import org.apache.commons.collections15.Transformer;


/**
 * Provides vertex sizes that are spaced proportionally between 
 * min_size and max_size depending on 
 * 
 * @author Joshua O'Madadhain
 */
public class InterpolatingVertexSizeTransformer<V> implements Transformer<V,Integer>
{
    protected double min;
    protected double max;
    protected Transformer<V, ? extends Number> values;
    protected int min_size;
    protected int size_diff;
    
    public InterpolatingVertexSizeTransformer(Transformer<V, ? extends Number> values, 
            int min_size, int max_size)
    {
        super();
        if (min_size < 0 || max_size < 0)
            throw new IllegalArgumentException("sizes must be non-negative");
        if (min_size > max_size)
            throw new IllegalArgumentException("min_size must be <= max_size");
        this.min = 0;
        this.max = 0;
        this.values = values;
        setMinSize(min_size);
        setMaxSize(max_size);
    }

    public Integer transform(V v)
    {
        Number n = values.transform(v);
        double value = min;
        if (n != null)
            value = n.doubleValue();
        min = Math.min(this.min, value);
        max = Math.max(this.max, value);
        
        if (min == max)
            return min_size;
        
        // interpolate between min and max sizes based on how big value is 
        // with respect to min and max values
        return min_size + (int)(((value - min) / (max - min)) * size_diff);
    }
    
    public void setMinSize(int min_size)
    {
        this.min_size = min_size;
    }

    public void setMaxSize(int max_size)
    {
        this.size_diff = max_size - this.min_size;
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.