001    /**
002     * Copyright 2007 Mike Kroutikov.
003     *
004     * This program is free software; you can redistribute it and/or modify
005     *   it under the terms of the Lesser GNU General Public License as 
006     *   published by the Free Software Foundation; either version 3 of
007     *   the License, or (at your option) any later version.
008     *
009     *   This program is distributed in the hope that it will be useful,
010     *   but WITHOUT ANY WARRANTY; without even the implied warranty of
011     *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012     *   Lesser GNU General Public License for more details.
013     *
014     *   You should have received a copy of the Lesser GNU General Public License
015     *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
016     *   
017     *   Derived from code developed by Opentick Corporation, http://www.opentick.com.
018     */
019    
020    package org.otfeed.event;
021    
022    import static org.otfeed.event.IdentityUtil.equalsTo;
023    import static org.otfeed.event.IdentityUtil.enumSetHashCode;
024    import static org.otfeed.event.IdentityUtil.compareEnumSet;
025    import static org.otfeed.event.IdentityUtil.safeCompare;
026    import static org.otfeed.event.IdentityUtil.safeHashCode;
027    
028    import java.io.Serializable;
029    import java.util.Date;
030    import java.util.Set;
031    import java.util.HashSet;
032    
033    /**
034     * This class provides OTDividend events and member functions to reference the data provided by the object.
035     */
036    public final class OTDividend implements Comparable<OTDividend>, Serializable {
037            
038            private static final long serialVersionUID = -2243821011592708552L;
039    
040            private double price;
041        private Date declarationDate;
042        private Date executionDate;
043        private Date recordDate;
044        private Date paymentDate;
045        private Set<DividendPropertyEnum> properties = new HashSet<DividendPropertyEnum>();
046    
047        /**
048         * Default constructor.
049         */
050        public OTDividend() { }
051    
052        /**
053         * Constructor.
054         * @param price          Dividend (value).
055         * @param declaratonDate Declaration date.
056         * @param executionDate  Execution date.
057         * @param recordDate     Record date.
058         * @param paymentDate    Payment date.
059         * @param properties     set of properties, see {@link DividendPropertyEnum}
060         */
061        public OTDividend(double price, 
062                    Date declaratonDate, 
063                    Date executionDate,
064                            Date recordDate, 
065                            Date paymentDate,
066                            Set<DividendPropertyEnum> properties) {
067    
068            this.price = price;
069            this.declarationDate = declaratonDate;
070            this.executionDate = executionDate;
071            this.recordDate = recordDate;
072            this.paymentDate = paymentDate;
073            this.properties = properties;
074        }
075    
076        /**
077         * @return Dividend (value).
078         */
079        public double getPrice() {
080            return price;
081        }
082    
083        /**
084         * Sets dividend (value).
085         *
086         * @param price Dividend (value).
087         */
088        public void setPrice(double price) {
089            this.price = price;
090        }
091    
092        /**
093         * @return Date of declaration.
094         */
095        public Date getDeclarationDate() {
096            return declarationDate;
097        }
098    
099        /**
100         * Sets declaration date.
101         *
102         * @param declarationDate Declaration date.
103         */
104        public void setDeclarationDate(Date declarationDate) {
105            this.declarationDate = declarationDate;
106        }
107    
108        /**
109         * @return Date of execution.
110         */
111        public Date getExecutionDate() {
112            return executionDate;
113        }
114    
115        /**
116         * Sets execution date.
117         *
118         * @param executionDate Execution date.
119         */
120        public void setExecutionDate(Date executionDate) {
121            this.executionDate = executionDate;
122        }
123    
124        /**
125         * @return Date of record.
126         */
127        public Date getRecordDate() {
128            return recordDate;
129        }
130    
131        /**
132         * Sets record date.
133         *
134         * @param recordDate Record date.
135         */
136        public void setRecordDate(Date recordDate) {
137            this.recordDate = recordDate;
138        }
139    
140        /**
141         * @return Date of payment.
142         */
143        public Date getPaymentDate() {
144            return paymentDate;
145        }
146    
147        /**
148         * Sets payment date.
149         *
150         * @param paymentDate Payment date.
151         */
152        public void setPaymentDate(Date paymentDate) {
153            this.paymentDate = paymentDate;
154        }
155    
156        /**
157         * @return Set of properties, see {@link DividendPropertyEnum}.
158         */
159        public Set<DividendPropertyEnum> getProperties() {
160            return properties;
161        }
162        
163        public void setProperties(Set<DividendPropertyEnum> val) {
164            properties = val;
165        }
166        
167        /**
168         * @return Is approximate.
169         */
170        public boolean isApproximate() {
171            return properties.contains(DividendPropertyEnum.APPROXIMATE);
172        }
173    
174        /**
175         * @return Is annual.
176         */
177        public boolean isAnnual() {
178            return properties.contains(DividendPropertyEnum.ANNUAL);
179        }
180    
181        /**
182         * @return Is canadian.
183         */
184        public boolean isCanadian() {
185            return properties.contains(DividendPropertyEnum.CANADIAN);
186        }
187    
188        /**
189         * @return Is extra.
190         */
191        public boolean isExtra() {
192            return properties.contains(DividendPropertyEnum.EXTRA);
193        }
194    
195        /**
196         * @return Is final.
197         */
198        public boolean isFinal() {
199            return properties.contains(DividendPropertyEnum.FINAL);
200        }
201    
202        /**
203         * @return Is increase.
204         */
205        public boolean isIncrease() {
206            return properties.contains(DividendPropertyEnum.INCREASE);
207        }
208    
209        /**
210         * @return IS semiannual.
211         */
212        public boolean isSemiannual() {
213            return properties.contains(DividendPropertyEnum.SEMIANNUAL);
214        }
215    
216        /**
217         * @return Is stock.
218         */
219        public boolean isStock() {
220            return properties.contains(DividendPropertyEnum.STOCK);
221        }
222    
223        /**
224         * @return Is special.
225         */
226        public boolean isSpecial() {
227            return properties.contains(DividendPropertyEnum.SPECIAL);
228        }
229    
230        @Override
231            public String toString() {
232            String out = "OTDividend: price=" + price 
233            + ", decldate=" + declarationDate 
234            + ", execdate=" + executionDate 
235            + ", recdate=" + recordDate 
236            + ", paydate=" + paymentDate;
237            
238            if(!properties.isEmpty()) {
239                    out += ", properties=";
240                    boolean first = true;
241                    for(DividendPropertyEnum p : properties) {
242                            if(first) { first = false; }
243                            else      { out += "|"; }
244                            out += p;
245                    }
246            }
247            
248            return out;
249        }
250    
251        @Override
252            public int hashCode() {
253            return safeHashCode(price) 
254                    + 3 * safeHashCode(declarationDate) 
255                    + 5 * safeHashCode(executionDate) 
256                    + 7 * safeHashCode(recordDate) 
257                    + 11 * safeHashCode(paymentDate) 
258                    + 13 * enumSetHashCode(properties); 
259        }
260            
261        @Override
262            public boolean equals(Object o) {
263            return equalsTo(this, o);
264        }
265        
266            public int compareTo(OTDividend other) {
267                    int rc;
268    
269                    if((rc = safeCompare(price, other.price)) != 0) {
270                            return rc;
271                    }
272                    
273                    if((rc = safeCompare(declarationDate, other.declarationDate)) != 0) {
274                            return rc;
275                    }
276    
277                    if((rc = safeCompare(executionDate, other.executionDate)) != 0) {
278                            return rc;
279                    }
280    
281                    if((rc = safeCompare(recordDate, other.recordDate)) != 0) {
282                            return rc;
283                    }
284    
285                    if((rc = safeCompare(paymentDate, other.paymentDate)) != 0) {
286                            return rc;
287                    }
288    
289                    if((rc = compareEnumSet(DividendPropertyEnum.class, properties, other.properties)) != 0) {
290                            return rc;
291                    }
292    
293                    return 0;
294            }
295    }