BrightSide Workbench Full Report + Source Code
Workers.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2020 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.contacts.relation;
20 
21 import java.util.ArrayList;
22 import java.util.Date;
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.function.Predicate;
26 import org.amic.util.date.Dates;
27 import org.turro.contacts.BusinessRelation;
28 import org.turro.contacts.Contact;
29 import org.turro.contacts.util.ContactList;
30 
35 public class Workers extends HashSet<BusinessRelation> {
36 
37  private final Contact contact;
38  private final Date now;
39 
40  public Workers(Contact contact) {
41  this.contact = contact;
42  this.now = new Date();
43  load();
44  }
45 
46  public List<Contact> getWorkers() {
47  return getWorkers(now);
48  }
49 
50  public List<Contact> getWorkers(Date date) {
51  ContactList cs = new ContactList();
52  for(BusinessRelation br : this) {
53  if(Dates.inRange(br.getStartDate(), br.getEndDate(), date)) {
54  cs.add(br.getContact());
55  }
56  }
57  return cs;
58  }
59 
60  public boolean isWorker(Contact contact) {
61  return contact != null && getWorkers().contains(contact);
62  }
63 
64  public boolean isWorker(Contact contact, Date date) {
65  return contact != null && getWorkers(date).contains(contact);
66  }
67 
68  public boolean isByCondition(Contact contact, Predicate<BusinessRelation> predicate) {
69  return contact != null && getByCondition(predicate).contains(contact);
70  }
71 
72  public boolean isByCondition(Contact contact, Predicate<BusinessRelation> predicate, Date date) {
73  return contact != null && getByCondition(predicate, date).contains(contact);
74  }
75 
76  public List<Contact> getByCondition(Predicate<BusinessRelation> predicate) {
77  return getByCondition(predicate, now);
78  }
79 
80  public List<Contact> getByCondition(Predicate<BusinessRelation> predicate, Date date) {
81  ContactList cs = new ContactList();
82  for(BusinessRelation br : this) {
83  if(predicate.test(br) && Dates.inRange(br.getStartDate(), br.getEndDate(), date)) {
84  cs.add(br.getContact());
85  }
86  }
87  return cs;
88  }
89 
90  public List<BusinessRelation> getRelations(Contact contact) {
91  return getRelations(contact, now);
92  }
93 
94  public List<BusinessRelation> getRelations(Contact contact, Date date) {
95  List<BusinessRelation> list = new ArrayList<>();
96  for(BusinessRelation br : this) {
97  if(Dates.inRange(br.getStartDate(), br.getEndDate(), date)) {
98  list.add(br);
99  }
100  }
101  return list;
102  }
103 
104  private void load() {
105  addAll(contact.getWorkerRelations());
106  }
107 
108 }
Set< BusinessRelation > getWorkerRelations()
Definition: Contact.java:391
boolean isWorker(Contact contact, Date date)
Definition: Workers.java:64
List< BusinessRelation > getRelations(Contact contact)
Definition: Workers.java:90
boolean isWorker(Contact contact)
Definition: Workers.java:60
List< BusinessRelation > getRelations(Contact contact, Date date)
Definition: Workers.java:94
List< Contact > getWorkers(Date date)
Definition: Workers.java:50
List< Contact > getByCondition(Predicate< BusinessRelation > predicate, Date date)
Definition: Workers.java:80
List< Contact > getByCondition(Predicate< BusinessRelation > predicate)
Definition: Workers.java:76
boolean isByCondition(Contact contact, Predicate< BusinessRelation > predicate, Date date)
Definition: Workers.java:72
boolean isByCondition(Contact contact, Predicate< BusinessRelation > predicate)
Definition: Workers.java:68