View Javadoc

1   package org.kite9.diagram.position;
2   
3   
4   /***
5    * This extends the idea of dimension, but allows you to associate a cost with the dimension
6    * other than simply the size.
7    * 
8    * @author robmoffat
9    *
10   */
11  public class CostedDimension extends Dimension2D implements Comparable<CostedDimension> {
12  
13  	private static final long serialVersionUID = 5835848361276316308L;
14  
15  	public static final CostedDimension ZERO = new CostedDimension(0,0,0);
16  	
17  	public static final CostedDimension NOT_DISPLAYABLE = new CostedDimension(-1,-1,Integer.MAX_VALUE);
18  	
19  	public static final CostedDimension UNBOUNDED = new CostedDimension(Integer.MAX_VALUE,Integer.MAX_VALUE,0);
20  	
21  
22  	public long cost;
23  
24  	
25  	public CostedDimension() {
26  		super();
27  	}
28  	
29  	public CostedDimension(Dimension2D d) {
30  		super(d.getWidth(), d.getHeight());
31  	}
32  
33  	public CostedDimension(double arg0, double arg1, long cost) {
34  		super(arg0, arg1);
35  		this.cost = cost;
36  	}
37  
38  	/***
39  	 * Works out cost based on how well the new {@link CostedDimension} fits into within.
40  	 */
41  	public CostedDimension(double width, double height, Dimension2D within) {
42  		super(width, height);
43  		double extraHeight = Math.max(height - within.height, 0);
44  		double extraWidth = Math.max(width - within.width, 0);
45  		
46  		cost = (long)( (extraHeight * width) + (extraWidth * height) +  (extraHeight * extraWidth));
47  	}
48  
49  	public long getCost() {
50  		return cost;
51  	}
52  
53  	public void setCost(int cost) {
54  		this.cost = cost;
55  	}
56  	
57  	public static CostedDimension chooseBest(CostedDimension a, CostedDimension b) {
58  		if (a == CostedDimension.NOT_DISPLAYABLE) {
59  			if (b == CostedDimension.NOT_DISPLAYABLE) {
60  				return CostedDimension.NOT_DISPLAYABLE;
61  			}
62  			return b;
63  		} else {
64  			if (a.cost < b.cost) {
65  				return a;
66  			} else if (a.cost > b.cost) {
67  				return b;
68  			}
69  			
70  			if (a.height< b.height) {
71  				return a;
72  			} else if (a.height > b.height) {
73  				return b;
74  			}
75  		
76  			if (a.width< b.width) {
77  				return a;
78  			} else if (a.width > b.width) {
79  				return b;
80  			}
81  	
82  			return a;
83  		}
84  	}
85  
86  	public int compareTo(CostedDimension o) {
87  		return ((Long)this.cost).compareTo(o.cost);
88  	}
89  }