001    package ifs.example.rpp;
002    
003    import ifs.model.*;
004    
005    /**
006     * Location (value, i.e., a single placement of the rectangle). Location encodes X and Y 
007     * coordinate.
008     * 
009     * @author <a href="mailto:muller@ktiml.mff.cuni.cz">Tomáš Müller</a>
010     * @version 1.0
011     */
012    public class Location extends Value {
013        private int iX, iY;
014        /**
015         * Constructor
016         * @param rectangle parent variable
017         * @param x x coordinate
018         * @param y y coordinate
019         */
020        public Location(Rectangle rectangle, int x, int y) {
021            super(rectangle);
022            iX = x;
023            iY = y;
024        }
025        
026        /** Gets x coordinate */
027        public int getX() {
028            return  iX;
029        }
030        
031        /** Gets y coordinate */
032        public int getY() {
033            return iY;
034        }
035        
036        /** Compare two coordinates. It is based on comparison of the parent rectangle and x,y coordinates */
037        public boolean equals(Object object) {
038            if (object == null || !(object instanceof Location)) {
039                return false;
040            }
041            Location location = (Location) object;
042            
043            return (variable().equals(location.variable()) && location.getX() == getX() && location.getY() == getY());
044        }
045        
046        /** String representation (for debugging and printing purposes). 
047         * For example, rect43=[12,10] where rect43 is the name of the parent rectangle and [12,10] is the location.
048         */
049        public String toString() {
050            return variable().getName() + "=[" + getX() + "," + getY() + "]";
051        }
052        
053        /** Location's name. E.g., [12,10] where x=12 and y=10.*/
054        public String getName() {
055            return "[" + getX() + "," + getY() + "]";
056        }
057        
058        /** Returns true if the given location intersects with this location */
059        public boolean hasIntersection(Location anotherLocation) {
060            if (getX() + ((Rectangle) variable()).getWidth() <= anotherLocation.getX()) {
061                return false;
062            }
063            if (getY() + ((Rectangle) variable()).getHeight() <= anotherLocation.getY()) {
064                return false;
065            }
066            if (anotherLocation.getX() + ((Rectangle) anotherLocation.variable()).getWidth() <= getX()) {
067                return false;
068            }
069            if (anotherLocation.getY() + ((Rectangle) anotherLocation.variable()).getHeight() <= getY()) {
070                return false;
071            }
072            return true;
073        }
074    }