1 5 package chipchat; 6 7 import java.util.HashMap ; 8 import java.util.Iterator ; 9 import java.util.Map ; 10 11 14 public final class Channel { 15 16 private ChipChat parent; 17 18 21 22 23 private Map rooms = new HashMap (); 24 25 34 public Long makeRoom( 35 final String roomname, 36 final int maxman, 37 final String passwd, 38 final int master) 39 throws Exception { 40 Long num = getUniqueNumber(); 41 Room room = new Room(this, num, roomname, maxman, passwd, master); 42 synchronized (rooms) { 43 rooms.put(num, room); 44 } 45 return num; 46 } 47 48 52 public void removeRoom(final Long num) { 53 synchronized (rooms) { 54 rooms.remove(num); 55 } 56 } 57 58 63 public Room getRoom(final Long num) { 64 synchronized (rooms) { 65 return (Room) rooms.get(num); 66 } 67 } 68 69 72 73 private Object uniqueNumberLock = new Object (); 74 75 private long uniqueNum = 0; 76 77 81 private Long getUniqueNumber() { 82 long r; 83 synchronized (uniqueNumberLock) { 84 r = uniqueNum++; 85 } 86 return new Long (r); 87 } 88 89 92 93 private RoomInfo[] roomList; 94 95 private boolean listChanged = true; 96 97 100 protected void setListChanged() { 101 listChanged = true; 102 } 103 104 108 public RoomInfo[] getRoomList() { 109 if (listChanged) { 110 makeRoomList(); 111 } 112 return roomList; 113 } 114 115 118 private void makeRoomList() { 119 synchronized (rooms) { 120 if (listChanged) { 121 RoomInfo[] tmpRoomList = new RoomInfo[rooms.size()]; 122 int c = 0; 123 Iterator i = rooms.values().iterator(); 124 while (i.hasNext()) { 125 Object value = i.next(); 126 tmpRoomList[c] = new RoomInfo((Room) value); 127 c++; 128 } 129 roomList = tmpRoomList; 130 } 131 } 132 } 133 } 134 | Popular Tags |