Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.archmage.toolbox;
- import java.util.ArrayList;
- import java.util.HashMap;
- /**
- * I'd hoped I'd never have to write this class, but here we are.
- *
- * @author Rowan
- *
- * @param <E>
- */
- public class GridMap<E> {
- private final HashMap<Point,E> map = new HashMap<>();
- private E empty;
- public GridMap(E empty) {
- this.empty = empty;
- }
- public E get(Point point) {
- if(map.containsKey(point)) {
- return map.get(point);
- }
- else return empty;
- }
- public E get(int x, int y) {
- return get(new Point(x, y));
- }
- public void set(Point point, E value) {
- map.put(point, value);
- }
- public void set(int x, int y, E value) {
- set(new Point(x, y), value);
- }
- public void set(Point point) {
- set(point, empty);
- }
- public void set(int x, int y) {
- set(new Point(x, y), empty);
- }
- public void clear() {
- map.clear();
- }
- public int getMinX() {
- int output = Integer.MAX_VALUE;
- for(Point point : map.keySet()) {
- output = Integer.min(output, point.x);
- }
- return output;
- }
- public int getMinY() {
- int output = Integer.MAX_VALUE;
- for(Point point : map.keySet()) {
- output = Integer.min(output, point.y);
- }
- return output;
- }
- public int getMaxX() {
- int output = Integer.MIN_VALUE;
- for(Point point : map.keySet()) {
- output = Integer.max(output, point.x);
- }
- return output;
- }
- public int getMaxY() {
- int output = Integer.MIN_VALUE;
- for(Point point : map.keySet()) {
- output = Integer.max(output, point.y);
- }
- return output;
- }
- public int getWidth() {
- return getMaxX() - getMinX();
- }
- public int getHeight() {
- return getMaxY() - getMinY();
- }
- public Point[] keySet() {
- return map.keySet().toArray(new Point[0]);
- }
- public Point[] keySetRange(int x1, int y1, int x2, int y2) {
- ArrayList<Point> output = new ArrayList<>();
- for(Point point : map.keySet()) {
- if(point.x >= x1 && point.y >= y1 && point.x <= x2 && point.y <= y2) output.add(point);
- }
- return output.toArray(new Point[0]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement