Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. package com.archmage.toolbox;
  2.  
  3.  
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6.  
  7. /**
  8. * I'd hoped I'd never have to write this class, but here we are.
  9. *
  10. * @author Rowan
  11. *
  12. * @param <E>
  13. */
  14. public class GridMap<E> {
  15.  
  16. private final HashMap<Point,E> map = new HashMap<>();
  17. private E empty;
  18.  
  19. public GridMap(E empty) {
  20. this.empty = empty;
  21. }
  22.  
  23. public E get(Point point) {
  24. if(map.containsKey(point)) {
  25. return map.get(point);
  26. }
  27. else return empty;
  28. }
  29.  
  30. public E get(int x, int y) {
  31. return get(new Point(x, y));
  32. }
  33.  
  34. public void set(Point point, E value) {
  35. map.put(point, value);
  36. }
  37.  
  38. public void set(int x, int y, E value) {
  39. set(new Point(x, y), value);
  40. }
  41.  
  42. public void set(Point point) {
  43. set(point, empty);
  44. }
  45.  
  46. public void set(int x, int y) {
  47. set(new Point(x, y), empty);
  48. }
  49.  
  50. public void clear() {
  51. map.clear();
  52. }
  53.  
  54. public int getMinX() {
  55. int output = Integer.MAX_VALUE;
  56. for(Point point : map.keySet()) {
  57. output = Integer.min(output, point.x);
  58. }
  59. return output;
  60. }
  61.  
  62. public int getMinY() {
  63. int output = Integer.MAX_VALUE;
  64. for(Point point : map.keySet()) {
  65. output = Integer.min(output, point.y);
  66. }
  67. return output;
  68. }
  69.  
  70. public int getMaxX() {
  71. int output = Integer.MIN_VALUE;
  72. for(Point point : map.keySet()) {
  73. output = Integer.max(output, point.x);
  74. }
  75. return output;
  76. }
  77.  
  78. public int getMaxY() {
  79. int output = Integer.MIN_VALUE;
  80. for(Point point : map.keySet()) {
  81. output = Integer.max(output, point.y);
  82. }
  83. return output;
  84. }
  85.  
  86. public int getWidth() {
  87. return getMaxX() - getMinX();
  88. }
  89.  
  90. public int getHeight() {
  91. return getMaxY() - getMinY();
  92. }
  93.  
  94. public Point[] keySet() {
  95. return map.keySet().toArray(new Point[0]);
  96. }
  97.  
  98. public Point[] keySetRange(int x1, int y1, int x2, int y2) {
  99. ArrayList<Point> output = new ArrayList<>();
  100. for(Point point : map.keySet()) {
  101. if(point.x >= x1 && point.y >= y1 && point.x <= x2 && point.y <= y2) output.add(point);
  102. }
  103. return output.toArray(new Point[0]);
  104. }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement