All pastes #257193 Raw Edit

Miscellany

public text v1 · immutable
#257193 ·published 2006-11-25 01:27 UTC
rendered paste body
package com.rscd.model;

import java.util.HashMap;

//supports ONLY positive numbers

public final class Point {
	private static HashMap<Integer, Point> points = new HashMap<Integer, Point>();
	private int x;
	private int y;

	public static Point location(int x, int y) {
		if (x < 0 || y < 0) {
			throw new IllegalArgumentException("Point may not contain non negative values x:" + x + " y:" + y);
		}
		int hashcode = x << 16 | y;
		Point p = points.get(hashcode);
		if (p == null) {
			p = new Point(x, y);
			points.put(hashcode, p);
		}
		return p;
	}

	private Point(int x, int y) {
		this.x = x;
		this.y = y;
	}

	public final int getY() {
		return y;
	}

	public final int getX() {
		return x;
	}

	public final boolean equals(Object o) {
		if (o instanceof Point) {
			return this.x == ((Point) o).x && this.y == ((Point) o).y;
		}
		return false;
	}

	public int hashCode() {
		return x << 16 | y;
	}

}