DataHandler angepasst
[staff/due1/sed-hs15-srs-purple.git] / src / main / java / ch / bfh / ti / srs / datahandler / DataHandlerForTests.java
CommitLineData
7903e50e
M
1/*
2 * Copyright (c) 2015 Berner Fachhochschule, Switzerland.
3 *
4 * Project Smart Reservation System.
5 *
6 * Distributable under GPL license. See terms of license at gnu.org.
7 */
8package ch.bfh.ti.srs.datahandler;
9
be594ada 10import java.util.ArrayList;
7903e50e 11import java.util.HashSet;
be594ada 12import java.util.List;
7903e50e 13import java.util.Set;
be594ada 14import java.util.UUID;
7903e50e
M
15
16import ch.bfh.ti.daterange.DateRange;
17import ch.bfh.ti.srs.dataschema.Reservation;
18import ch.bfh.ti.srs.dataschema.Resource;
19import ch.bfh.ti.srs.dataschema.User;
20
25d43fa9 21public class DataHandlerForTests implements DataHandler {
1e4d6813
M
22 private Set<User> userList = new HashSet<User>();
23 private Set<Resource> resourceList = new HashSet<Resource>();
24 private Set<Reservation> reservationList = new HashSet<Reservation>();
25d43fa9
M
25
26 public DataHandlerForTests() {
27
7903e50e
M
28 }
29
a77189a6
M
30 /**
31 * {@inheritDoc}
7903e50e
M
32 */
33 @Override
34 public User findUser(String userName) {
35 for (User user : userList) {
25d43fa9
M
36 if (user.getUserName().equals(userName))
37 return user;
7903e50e
M
38 }
39 return null;
40 }
41
a77189a6
M
42 /**
43 * {@inheritDoc}
7903e50e
M
44 */
45 @Override
46 public void addUser(User user) throws Exception {
25d43fa9
M
47 if (user == null)
48 throw new IllegalArgumentException("User object must not be null!");
49 if (userList.contains(user))
50 throw new IllegalArgumentException("User already exists!");
7903e50e
M
51 userList.add(user);
52 }
53
a77189a6
M
54 /**
55 * {@inheritDoc}
7903e50e
M
56 */
57 @Override
58 public Resource findResource(String resourceName) {
59 for (Resource resource : resourceList) {
25d43fa9
M
60 if (resource.getName().equals(resourceName))
61 return resource;
7903e50e
M
62 }
63 return null;
64 }
25d43fa9 65
a77189a6
M
66 /**
67 * {@inheritDoc}
7903e50e
M
68 */
69 @Override
70 public void addResource(Resource resource) throws Exception {
25d43fa9
M
71 if (resource == null)
72 throw new IllegalArgumentException("Resource object must not be null!");
73 if (resourceList.contains(resource))
74 throw new IllegalArgumentException("Resource already exists!");
7903e50e
M
75 resourceList.add(resource);
76 }
77
a77189a6
M
78 /**
79 * {@inheritDoc}
7903e50e
M
80 */
81 @Override
be594ada 82 public Reservation findReservation(UUID id) {
7903e50e 83 for (Reservation reservation : reservationList) {
25d43fa9
M
84 if (reservation.getId().equals(id))
85 return reservation;
7903e50e
M
86 }
87 return null;
88 }
25d43fa9 89
a77189a6
M
90 /**
91 * {@inheritDoc}
25d43fa9 92 */
be594ada 93 @Override
a77189a6
M
94 public List<Reservation> findGroupReservations(UUID groupId) {
95 if (groupId == null)
96 throw new IllegalArgumentException("DateRange object must not be null!");
be594ada
M
97 List<Reservation> groupReservations = new ArrayList<Reservation>();
98 for (Reservation reservation : reservationList) {
a77189a6 99 if (reservation.getGroupId().equals(groupId))
be594ada
M
100 groupReservations.add(reservation);
101 }
102 return groupReservations;
103 }
104
a77189a6
M
105 /**
106 * {@inheritDoc}
107 */
108 @Override
109 public List<Reservation> userReservations(User user) {
110 if (user == null)
111 throw new IllegalArgumentException("User object must not be null!");
112 List<Reservation> allUserReservations = new ArrayList<Reservation>();
113 for (Reservation reservation : reservationList) {
114 if (reservation.getUser().equals(user))
115 allUserReservations.add(reservation);
116 }
117 return allUserReservations;
118 }
119
120 /**
121 * {@inheritDoc}
25d43fa9 122 */
be594ada
M
123 @Override
124 public List<Reservation> overlaps(DateRange range) {
a77189a6
M
125 if (range == null)
126 throw new IllegalArgumentException("DateRange object must not be null!");
25d43fa9
M
127 List<Reservation> overlappingReservations = new ArrayList<Reservation>();
128 for (Reservation reservation : reservationList) {
129 if (reservation.getDateRange().overlaps(range))
130 overlappingReservations.add(reservation);
131 }
132 return overlappingReservations;
be594ada 133 }
25d43fa9 134
a77189a6
M
135 /**
136 * {@inheritDoc}
137 */
138 @Override
139 public List<Reservation> findAllReservations() {
140 List<Reservation> allReservations = new ArrayList<Reservation>();
141 for (Reservation reservation : reservationList) {
142 allReservations.add(reservation);
143 }
144 return allReservations;
145 }
146
147 /**
148 * {@inheritDoc}
7903e50e
M
149 */
150 @Override
151 public void addReservation(Reservation reservation) throws Exception {
25d43fa9
M
152 if (reservation == null)
153 throw new IllegalArgumentException("Reservation object must not be null!");
154 if (resourceList.contains(reservation))
155 throw new IllegalArgumentException("Reservation already exists!");
156 reservationList.add(reservation);
7903e50e 157 }
be594ada 158
7903e50e 159}