| 
0
 | 
     1 package datastructures;
 | 
| 
 | 
     2 
 | 
| 
 | 
     3 /**
 | 
| 
 | 
     4  * Used for various purposes.
 | 
| 
 | 
     5  * 
 | 
| 
 | 
     6  * @author Ali Abdallah
 | 
| 
 | 
     7  * @version 01.2011
 | 
| 
 | 
     8  * @since jdk 1.6.0
 | 
| 
 | 
     9  */
 | 
| 
 | 
    10 public class Frame {
 | 
| 
 | 
    11 	
 | 
| 
 | 
    12 	// Self explanatory.
 | 
| 
 | 
    13 	protected int start; 
 | 
| 
 | 
    14 	protected int end;
 | 
| 
 | 
    15 	protected int length;
 | 
| 
 | 
    16 	
 | 
| 
 | 
    17 	/**
 | 
| 
 | 
    18 	 * Constructs a frame with start position "start", length "length" and
 | 
| 
 | 
    19 	 * end position "start+length-1".
 | 
| 
 | 
    20 	 * @param start the start position of the frame.
 | 
| 
 | 
    21 	 * @param length the length of the frame.
 | 
| 
 | 
    22 	 */
 | 
| 
 | 
    23 	public Frame(int start, int length) {
 | 
| 
 | 
    24 		this.start = start; end = start+length-1;
 | 
| 
 | 
    25 		this.length = length;
 | 
| 
 | 
    26 	}
 | 
| 
 | 
    27 	
 | 
| 
 | 
    28 	/**
 | 
| 
 | 
    29 	 * Verify if this frame overlaps the frame specified in the parameter.
 | 
| 
 | 
    30 	 * @param f the frame which potentially overlaps the calling frame.
 | 
| 
 | 
    31 	 * @return true if the two frames overlap each other and false otherwise.
 | 
| 
 | 
    32 	 */
 | 
| 
 | 
    33 	public boolean overlaps(Frame f){
 | 
| 
 | 
    34 		return (end >= f.start() && start < f.end());
 | 
| 
 | 
    35 	}
 | 
| 
 | 
    36 	
 | 
| 
 | 
    37 	/**
 | 
| 
 | 
    38 	 * Computes the size of the overlap area.
 | 
| 
 | 
    39 	 * @param f the frame overlapping the calling frame.
 | 
| 
 | 
    40 	 * @return the size of the overlap if an overlap exists and -1 otherwise.
 | 
| 
 | 
    41 	 */
 | 
| 
 | 
    42 	public int overlapSize(Frame f){
 | 
| 
 | 
    43 		if(this.overlaps(f)){
 | 
| 
 | 
    44 			int s = Math.max(start, f.start());
 | 
| 
 | 
    45 			int e = Math.min(end, f.end());
 | 
| 
 | 
    46 			return e-s+1;
 | 
| 
 | 
    47 		}
 | 
| 
 | 
    48 		return -1;
 | 
| 
 | 
    49 	}
 | 
| 
 | 
    50 	
 | 
| 
 | 
    51 	/**
 | 
| 
 | 
    52 	 * Unify the calling frame with the frame specified in the parameter.
 | 
| 
 | 
    53 	 * @param f the frame to be unified with the calling frame.
 | 
| 
 | 
    54 	 * @return the union of the calling frame and f.
 | 
| 
 | 
    55 	 */
 | 
| 
 | 
    56 	public Frame unify(Frame f){
 | 
| 
 | 
    57 		return 
 | 
| 
 | 
    58 			new Frame(Math.min(this.start, f.start), 
 | 
| 
 | 
    59 					  Math.max(this.end,f.end)-Math.min(this.start, f.start)+1);	
 | 
| 
 | 
    60 	}
 | 
| 
 | 
    61 
 | 
| 
 | 
    62 	/**
 | 
| 
 | 
    63 	 * self explanatory
 | 
| 
 | 
    64 	 * @return start of the frame.
 | 
| 
 | 
    65 	 */
 | 
| 
 | 
    66 	public int start(){
 | 
| 
 | 
    67 		return start;
 | 
| 
 | 
    68 	}
 | 
| 
 | 
    69 	
 | 
| 
 | 
    70 	/**
 | 
| 
 | 
    71 	 * self explanatory
 | 
| 
 | 
    72 	 * @return end of the frame.
 | 
| 
 | 
    73 	 */
 | 
| 
 | 
    74 	public int end(){
 | 
| 
 | 
    75 		return end;
 | 
| 
 | 
    76 	}
 | 
| 
 | 
    77 	
 | 
| 
 | 
    78 	/**
 | 
| 
 | 
    79 	 * self explanatory
 | 
| 
 | 
    80 	 * @return length of the frame.
 | 
| 
 | 
    81 	 */
 | 
| 
 | 
    82 	public int length(){
 | 
| 
 | 
    83 		return length;
 | 
| 
 | 
    84 	}
 | 
| 
 | 
    85 	
 | 
| 
 | 
    86 	/**
 | 
| 
 | 
    87 	 * Make a string representation of the frame.
 | 
| 
 | 
    88 	 * @return a string representation of the string.
 | 
| 
 | 
    89 	 */
 | 
| 
 | 
    90 	public String toString(){
 | 
| 
 | 
    91 		return start+":"+end;
 | 
| 
 | 
    92 	}
 | 
| 
 | 
    93 
 | 
| 
 | 
    94 }
 |