View Javadoc

1   /*
2    * Copyright 2006 - 2012 Christina Bohk and Roland Ewald
3    *  
4    * Licensed under the Apache License, Version 2.0 (the "License"); 
5    * you may not use this file except in compliance with the License. 
6    * You may obtain a copy of the License at 
7    *  
8    *  http://www.apache.org/licenses/LICENSE-2.0
9    *  
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
13   * See the License for the specific language governing permissions and 
14   * limitations under the License. 
15   */
16  package p3j.pppm;
17  
18  import java.io.Serializable;
19  
20  /**
21   * Represents a sub-population.
22   * 
23   * @author Christina Bohk
24   * @author Roland Ewald
25   */
26  public class SubPopulation implements Serializable {
27  
28    /** The Constant serialVersionUID. */
29    private static final long serialVersionUID = 1501278366716906252L;
30  
31    /** The name of the sub-population. */
32    private String name = "";
33  
34    /**
35     * Flag to determine whether this sub-population adds to the overall
36     * population.
37     */
38    private boolean additive = true;
39  
40    /**
41     * Flag to determine whether this sub-population consists of descendant
42     * generations as well.
43     */
44    private boolean consistingOfDescendantGenerations = false;
45  
46    /**
47     * Flag to determine whether this sub-population is part of the jump-off
48     * population.
49     */
50    private boolean jumpOffPopulation = false;
51  
52    /** Default constructor. */
53    public SubPopulation() {
54    }
55  
56    /**
57     * Full constructor.
58     * 
59     * @param name
60     *          the name of the sub-population
61     * @param jumpOffPop
62     *          flag to indicate that this population is part of the jump-off
63     *          population
64     * @param additive
65     *          the flag to determine whether this sub-population adds to the
66     *          overall population
67     * @param consistOfDescGenerations
68     *          the flag to determine whether this sub-population consists of
69     *          descendant generations
70     */
71    public SubPopulation(String name, boolean jumpOffPop, boolean additive,
72        boolean consistOfDescGenerations) {
73      this.name = name;
74      this.jumpOffPopulation = jumpOffPop;
75      this.additive = additive;
76      this.consistingOfDescendantGenerations = consistOfDescGenerations;
77    }
78  
79    /**
80     * Creates new sub-population with same properties.
81     * 
82     * @return the new sub-population
83     */
84    public SubPopulation newSubPopulation() {
85      return new SubPopulation(name, jumpOffPopulation, additive,
86          consistingOfDescendantGenerations);
87    }
88  
89    /**
90     * Gets the name.
91     * 
92     * @return the name
93     */
94    public String getName() {
95      return name;
96    }
97  
98    /**
99     * Sets the name.
100    * 
101    * @param name
102    *          the new name
103    */
104   public void setName(String name) {
105     this.name = name;
106   }
107 
108   /**
109    * Checks if sub-population is additive.
110    * 
111    * @return true, if is additive
112    */
113   public boolean isAdditive() {
114     return additive;
115   }
116 
117   /**
118    * Sets the 'additive'-flag.
119    * 
120    * @param additive
121    *          the new additive
122    */
123   public void setAdditive(boolean additive) {
124     this.additive = additive;
125   }
126 
127   /**
128    * Checks if sub-population is consisting of descendant generations.
129    * 
130    * @return true, if is consisting of descendant generations
131    */
132   public boolean isConsistingOfDescendantGenerations() {
133     return consistingOfDescendantGenerations;
134   }
135 
136   /**
137    * Sets the 'consisting of descendant generations'-flag.
138    * 
139    * @param consistingOfDescendantGenerations
140    *          the new consisting of descendant generations
141    */
142   public void setConsistingOfDescendantGenerations(
143       boolean consistingOfDescendantGenerations) {
144     this.consistingOfDescendantGenerations = consistingOfDescendantGenerations;
145   }
146 
147   @Override
148   public String toString() {
149     return getName()
150         + " ("
151         + (additive ? "+" : "-")
152         + (jumpOffPopulation ? ", jump-off" : "")
153         + (consistingOfDescendantGenerations ? ", has descendant generations"
154             : "") + ")";
155   }
156 
157   public boolean isJumpOffPopulation() {
158     return jumpOffPopulation;
159   }
160 
161   public void setJumpOffPopulation(boolean jumpOffPopulation) {
162     this.jumpOffPopulation = jumpOffPopulation;
163   }
164 
165   /**
166    * Gets the simplified name of the sub-population. This can be used, for
167    * example, in file names.
168    * 
169    * @return the simplified name (lower cases, no spaces, etc.)
170    */
171   public String getSimplifiedName() {
172     return name.toLowerCase().replace(" ", "").replace(":", "")
173         .replace("\\", "_").replace("/", "_");
174   }
175 
176   @Override
177   public int hashCode() {
178     return name.hashCode()
179         ^ ("" + additive + consistingOfDescendantGenerations + jumpOffPopulation)
180             .hashCode();
181   }
182 
183   @Override
184   public boolean equals(Object o) {
185     if (o == null || !(o instanceof SubPopulation))
186       return false;
187     SubPopulation oSubPop = (SubPopulation) o;
188     return name.equals(oSubPop.getName())
189         && additive == oSubPop.additive
190         && consistingOfDescendantGenerations == oSubPop.consistingOfDescendantGenerations
191         && jumpOffPopulation == oSubPop.jumpOffPopulation;
192   }
193 }