Point.java revision 3db9393ba06bbf70fa7b4a6db1ef60396979a1d4
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Eclipse Public License, Version 1.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.eclipse.org/org/documents/epl-v10.php
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.ide.common.api;
18
19import com.google.common.annotations.Beta;
20
21
22/**
23 * Mutable point.
24 * <p>
25 * <b>NOTE: This is not a public or final API; if you rely on this be prepared
26 * to adjust your code for the next tools release.</b>
27 * </p>
28 */
29@Beta
30public class Point {
31    public int x, y;
32
33    public Point(int x, int y) {
34        this.x = x;
35        this.y = y;
36    }
37
38    public Point(Point p) {
39        x = p.x;
40        y = p.y;
41    }
42
43    /** Sets the point to the given coordinates. */
44    public void set(int x, int y) {
45        this.x = x;
46        this.y = y;
47    }
48
49    /** Returns a new instance of a point with the same values. */
50    public Point copy() {
51        return new Point(x, y);
52    }
53
54    /**
55     * Offsets this point by adding the given x,y deltas to the x,y coordinates.
56     * @return Returns self, for chaining.
57     */
58    public Point offsetBy(int x, int y) {
59        this.x += x;
60        this.y += y;
61        return this;
62    }
63
64    @Override
65    public boolean equals(Object obj) {
66        if (obj instanceof Point) {
67            Point rhs = (Point) obj;
68            return this.x == rhs.x && this.y == rhs.y;
69        }
70        return false;
71    }
72
73    @Override
74    public int hashCode() {
75        int h = x ^ ((y >> 16) & 0x0FFFF) ^ ((y & 0x0FFFF) << 16);
76        return h;
77    }
78
79    @Override
80    public String toString() {
81        return String.format("Point [%dx%d]", x, y);
82    }
83}
84