001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.social.model;
016    
017    import com.liferay.portal.kernel.util.Time;
018    
019    import java.util.Calendar;
020    import java.util.Date;
021    import java.util.GregorianCalendar;
022    
023    /**
024     * @author Zsolt Berentey
025     */
026    public class SocialEquityValue {
027    
028            public SocialEquityValue(double k, double b) {
029                    _k = k;
030                    _b = b;
031            }
032    
033            public void add(SocialEquityValue socialEquityValue) {
034                    _k = _k + socialEquityValue._k;
035                    _b = _b + socialEquityValue._b;
036            }
037    
038            public double getB() {
039                    return _b;
040            }
041    
042            public double getK() {
043                    return _k;
044            }
045    
046            public double getValue() {
047                    return getValue(getEquityDate(new Date()));
048            }
049    
050            public double getValue(int equityDate) {
051                    return _k * equityDate + _b;
052            }
053    
054            public void subtract(SocialEquityValue socialEquityValue) {
055                    _k = _k - socialEquityValue._k;
056                    _b = _b - socialEquityValue._b;
057            }
058    
059            protected int getEquityDate(Date date) {
060                    Calendar calendar = new GregorianCalendar();
061    
062                    calendar.setTime(date);
063    
064                    long d = calendar.getTimeInMillis() - _BASE_TIME;
065    
066                    return (int)(d / Time.DAY);
067            }
068    
069            private static final long _BASE_TIME =
070                    new GregorianCalendar(2010, Calendar.JANUARY, 1).getTimeInMillis();
071    
072            private double _b = 0;
073            private double _k = 0;
074    
075    }