BrightSide Workbench Full Report + Source Code
TargetArray.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2013 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.organigram;
20 
21 import java.io.Serializable;
22 import java.util.ArrayList;
23 import java.util.EnumSet;
24 
29 public class TargetArray extends ArrayList<TargetItem> implements Serializable {
30 
31  private static final long serialVersionUID = -1;
32 
33  private LastItem lastItem;
34 
35  public TargetArray() {
36  lastItem = LastItem.LI_NONE;
37  }
38 
39  public LastItem getLastItem() {
40  checkLastOperator();
41  return lastItem;
42  }
43 
44  public void setLastItem(LastItem lastItem) {
45  this.lastItem = lastItem;
46  }
47 
48  @Override
49  public boolean add(TargetItem e) {
50  if(exists(e)) {
51  return false;
52  }
53  boolean result = super.add(e);
54  checkLastOperator();
55  return result;
56  }
57 
58  @Override
59  public void add(int index, TargetItem e) {
60  if(exists(e)) {
61  return;
62  }
63  super.add(index, e);
64  checkLastOperator();
65  }
66 
67  @Override
68  public boolean remove(Object o) {
69  boolean result = super.remove(o);
70  checkLastOperator();
71  return result;
72  }
73 
74  public EnumSet<RelationType> getRelationTypes() {
75  EnumSet<RelationType> es = EnumSet.noneOf(RelationType.class);
76  for(TargetItem ti : this) {
77  es.add(ti.getRelationType());
78  }
79  return es;
80  }
81 
82  private boolean exists(TargetItem e) {
83  for(TargetItem ti : this) {
84  if(e.equals(ti)) {
85  return true;
86  }
87  }
88  return false;
89  }
90 
91  private void checkLastOperator() {
92  if(size() > 0) {
93  TargetItem ti = get(size() - 1);
94  ti.setNextOperator(NextOperator.NO_OTHERWISE);
95  }
96  }
97 
98 }
void add(int index, TargetItem e)
EnumSet< RelationType > getRelationTypes()
void setNextOperator(NextOperator nextOperator)
Definition: TargetItem.java:56