1 package Map; 2 3 public class PointManagerImpl 4 extends PointManagerPOA 5 { 6 private Point[] _points = new Point[256]; 7 private int _num_points; 8 private String _manager_name; 9 private boolean _verbose; 10 11 public PointManagerImpl(String manager_name, boolean verbose) 12 { 13 _manager_name = manager_name; 14 _num_points = 0; 15 _verbose = verbose; 16 } 17 18 public Point create_point (int x, int y, String lab, boolean connectable) 19 throws DuplicatePoint, InvalidPoint 20 { 21 if (lab == null) { 22 throw new InvalidPoint(); 23 } 24 25 if (_verbose) 26 { 27 String connect_string = (connectable ? "connected" : "disconnected"); 28 System.out.println("PointManager::creating [" + connect_string + 29 "] point for [" + lab + ", " + x + ", " + y + "]"); 30 } 31 32 Point the_p = _find_point(lab); 33 if (the_p != null) { 34 throw new DuplicatePoint(); 35 } 36 37 Point[] empty_points = new Point[0]; 38 if (connectable) { 39 the_p = new ConnectedPointImpl(x, y, lab, empty_points); 40 } 41 else { 42 the_p = new PointImpl(x, y, lab); 43 } 44 45 _add_point(the_p); 46 47 if (_verbose) { 48 System.out.println("PointManager::created [" + the_p + "]"); 49 } 50 51 return the_p; 52 } 53 54 public Point get_point (String lab) 55 throws UnknownPoint, InvalidPoint 56 { 57 if (lab == null) { 58 throw new InvalidPoint(); 59 } 60 61 Point the_p = _find_point(lab); 62 63 if (the_p == null) 64 { 65 throw new UnknownPoint(); 66 } 67 return the_p; 68 } 69 70 public void destroy_point (String lab) 71 throws UnknownPoint, InvalidPoint 72 { 73 if (lab == null) { 74 throw new InvalidPoint(); 75 } 76 77 if (_verbose) { 78 System.out.println("PointManager::destroy_point for [" + lab + "]"); 79 } 80 81 if (!_remove_point(lab)) { 82 throw new UnknownPoint(); 83 } 84 } 85 86 public Point[] list_points (int scaling_factor) 87 { 88 if (_verbose) 89 { 90 System.out.println("PointManager::list_points with scaling [" + 91 scaling_factor + "]"); 92 } 93 94 Point[] ret = new Point[_num_points ]; 96 97 for (int i = 0; i < _num_points; i++) 98 { 99 ret[i] = _points[i]; 100 } 105 return ret; 106 } 107 108 public void connect_point (String lab, String [] connectTo) 109 throws UnknownPoint, InvalidPoint 110 { 111 if (lab == null) { 112 throw new InvalidPoint(); 113 } 114 115 Point the_p = _find_point(lab); 116 if (the_p == null) { 117 throw new UnknownPoint(); 118 } 119 120 if (!(the_p instanceof ConnectedPoint)) { 121 throw new InvalidPoint(); 122 } 123 124 ConnectedPoint the_cp = (ConnectedPoint)the_p; 125 126 for (int i = 0; i < connectTo.length; i++) { 127 Point target = _find_point(connectTo[i]); 128 if (target == null) { 129 throw new UnknownPoint(lab); 130 } 131 the_cp.add_connection(target); 132 } 133 } 134 135 public String get_name() { 136 return _manager_name; 137 } 138 139 private int _find(String label) 140 { 141 142 int i; 143 for (i = 0; i < _num_points; i++) 144 { 145 if (label.equals(_points[i].label)) 146 { 147 break; 148 } 149 } 150 return i; 151 } 152 153 private boolean _remove_point(String label) 154 { 155 int index = _find(label); 156 if (index < _num_points) 157 { 158 if (index < _num_points-1) 159 { 160 _points[index] = _points[_num_points-1]; 161 } 162 else 163 { 164 _points[index] = null; 165 } 166 _num_points--; 167 return true; 168 } 169 else 170 { 171 return false; 172 } 173 } 174 175 private Point _find_point(String label) 176 { 177 int index = _find(label); 178 if (index < _num_points) 179 { 180 return _points[index]; 181 } 182 return null; 183 } 184 185 private void _add_point(Point p) { 186 if (_num_points < 256) { 187 _points[_num_points++] = p; 188 } 189 } 190 } 191 | Popular Tags |