Subversion Repository Public Repository

litesoft

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// This Source Code is in the Public Domain per: http://unlicense.org
package org.litesoft.core.simpletypes.temporal;

import java.util.*;

import org.litesoft.core.*;
import org.litesoft.commonfoundation.typeutils.gregorian.*;

/**
 * A Calendar "date" (e.g. Year, Month, & Day) ("Wall" relative).
 * <p/>
 * Note: This class supports Gregorian dates only and has no concept of time or TimeZones.  As such when working with
 * Java Util Date(s) (or it's descendants) the local (or "Wall") date is all that is considered!
 */
public final class CalendarYMD extends AbstractCalendarYMD<CalendarYMD, AbstractCalendarYMD.Mutable> implements SQLdateable
{
    private static final long serialVersionUID = 1L;

    @SuppressWarnings({"deprecation", "UnusedDeclaration"}) @Deprecated /** for Serialization */
    protected CalendarYMD()
    {
        this( 0, 1, 1 );
    }

    public CalendarYMD( int pYear, int pMonth, int pDay )
    {
        super( pYear, pMonth, pDay );
    }

    public CalendarYMD( CalendarAccessorYMD pAccessor )
    {
        this( pAccessor.getYear(), pAccessor.getMonth(), pAccessor.getDay() );
    }

    public CalendarYMD( Date pDate )
            throws IllegalArgumentException
    {
        this( new UtilDateAdaptor( pDate ) );
    }

    @Override
    public TemporalResolution getResolution()
    {
        return TemporalResolution.ToDay;
    }

    @Override
    public String toString()
    {
        return YearMonthDayFormatAccessor.get().format( this );
    }

    public static String toString( CalendarYMD pCalendar )
    {
        return (pCalendar != null) ? pCalendar.toString() : null;
    }

    public static String toYMD( CalendarYMD pCalendar )
    {
        return (pCalendar != null) ? pCalendar.toYMD() : null;
    }

    public String toYMD()
    {
        return formatSortableDisplayFormYMD( this, new StringBuilder() ).toString();
    }

    public static CalendarYMD fromYMD( String pToYMD )
            throws IllegalArgumentException
    {
        return fromStringForm( pToYMD, "//" );
    }

    @Override
    public final String toSQLvalue()
    {
        return formatSQLvalueYMD( this, new StringBuilder() ).toString();
    }

    public static CalendarYMD fromSQLvalue( String pToSQLvalue )
    {
        return fromStringForm( pToSQLvalue, "--" );
    }

    private static CalendarYMD fromStringForm( String pStringForm, String pSequentialSeperators )
    {
        String[] parts = parseSimple( pStringForm, pSequentialSeperators );
        return (parts == null) ? null : //
               new CalendarYMD( parseYear( pStringForm, parts ), //
                                parseMonth( pStringForm, parts ),
                                parseDay( pStringForm, parts ) );
    }

    @Override
    public UtilDateAdaptor toUtilDateAdaptor()
    {
        return UtilDateAdaptor.currentWall( getYear(), getMonth(), getDay() );
    }

    // vvvvvvvvvvvvvvvvvv Non-common Public methods vvvvvvvvvvvvvvvvv

    public static CalendarYMD today()
    {
        return new CalendarYMD( new Date() );
    }

    /**
     * Return a CalendarYMD with the day set to the 1st day of the current month, this will always set the 'Day' field to 1.
     *
     * @throws IllegalArgumentException if the Resolution does not extend to Day.
     */
    public CalendarYMD forceFirstOfMonth()
            throws IllegalArgumentException
    {
        return day( 1 );
    }

    /**
     * Return a CalendarYMD with the day set to the last day of the current month (either 28, 29, 30, or 31).
     *
     * @throws IllegalArgumentException if the Resolution does not extend to Day.
     */
    public CalendarYMD forceEndOfMonth()
            throws IllegalArgumentException
    {
        return day( Month.daysIn( getYear(), getMonth() ) );
    }

    /**
     * Return a CalendarYMD who's <code>getWeekDay()</code> will be <code>pWeekday</code>, where the resulting date will <b>NOT</b> be in the Future relative to
     * <code>this</code>.
     *
     * @param pWeekDay !null
     */
    public CalendarYMD notFuture( WeekDay pWeekDay )
    {
        return minusDays( getWeekDay().deltaTillBackward( pWeekDay ) );
    }

    public WeekDay getWeekDay()
    {
        return WeekDay.valueOf( toUtilDateAdaptor().dayOfWeek() );
    }

    /**
     * Number of days from this to pThem (could be negative).
     *
     * @param pThem !null
     *
     * @throws IllegalArgumentException if pThem is null or the resolution of either this' or pThem is not to the Day
     */
    public int daysTill( CalendarAccessorYMD pThem )
            throws IllegalArgumentException
    {
        undefinedResultIfNull( "daysTill", pThem );
        int result = compareEm( compare( this.getYear(), pThem.getYear() ),
                                compare( this.getMonth(), pThem.getMonth() ),
                                compare( this.getDay(), pThem.getDay() ) );
        if ( result == EQUAL )
        {
            return 0;
        }
        return (result == LESSER) ? new DaysDiff( this ).till( pThem ) : -new DaysDiff( pThem ).till( this );
    }

    // vvvvvvvvvvvvvvvvvv Support methods vvvvvvvvvvvvvvvvv

    @Override
    protected CalendarYMD createTypeFrom( Mutable pMutable )
    {
        return new CalendarYMD( pMutable.getYear(), pMutable.getMonth(), pMutable.getDay() );
    }

    @Override
    protected Mutable createMutable( int pNewYear )
    {
        return new Mutable( pNewYear, getMonth(), getDay(), true );
    }

    @Override
    protected Mutable createMutable( int pYear, int pNewMonth )
    {
        return new Mutable( pYear, pNewMonth, getDay(), true );
    }

    @Override
    protected Mutable createMutable( int pYear, int pMonth, int pNewDay )
    {
        return new Mutable( pYear, pMonth, pNewDay, false );
    }

    private static class DaysDiff
    {
        private int mYear, mMonth, mDay, mDiff = 0;

        public DaysDiff( CalendarAccessorYMD pYMDaccessor )
        {
            mYear = pYMDaccessor.getYear();
            mMonth = pYMDaccessor.getMonth();
            mDay = pYMDaccessor.getDay();
        }

        public int till( CalendarAccessorYMD pThem ) // !null and is in our future
        {
            if ( mYear != pThem.getYear() )
            {
                // advance to 1st of the year
                do
                {
                    advanceTo1stOfNextMonth();
                }
                while ( mMonth != 1 );

                for (; mYear < pThem.getYear(); mYear++ )
                {
                    mDiff += Year.isLeap( mYear ) ? 366 : 365;
                }
            }
            while ( mMonth < pThem.getMonth() )
            {
                advanceTo1stOfNextMonth();
            }
            return mDiff + (pThem.getDay() - mDay);
        }

        private void advanceTo1stOfNextMonth()
        {
            int zDaysInMonth = Month.daysIn( mYear, mMonth );
            mDiff += zDaysInMonth - (mDay - 1);
            mDay = 1;
            if ( ++mMonth > 12 )
            {
                mMonth = 1;
                mYear++;
            }
        }
    }
}

Commits for litesoft/trunk/Java/core/Anywhere/src/org/litesoft/core/simpletypes/temporal/CalendarYMD.java

Diff revisions: vs.
Revision Author Commited Message
947 Diff Diff GeorgeS picture GeorgeS Fri 06 Jun, 2014 23:36:56 +0000

Correct Spelling of package!

945 Diff Diff GeorgeS picture GeorgeS Fri 06 Jun, 2014 20:51:31 +0000

Extracted commonfoundation

939 Diff Diff GeorgeS picture GeorgeS Mon 02 Jun, 2014 21:30:31 +0000

Extracting commonfoundation

912 Diff Diff GeorgeS picture GeorgeS Fri 28 Jun, 2013 06:48:05 +0000

Revert to mixed & Working (PEDS & Prioritizer) code!

898 Diff Diff GeorgeS picture GeorgeS Sun 17 Mar, 2013 21:46:33 +0000

Temporal!!!

897 Diff Diff GeorgeS picture GeorgeS Mon 25 Feb, 2013 02:04:34 +0000
893 Diff Diff GeorgeS picture GeorgeS Mon 14 Jan, 2013 01:18:30 +0000
864 Diff Diff GeorgeS picture GeorgeS Mon 19 Nov, 2012 01:48:25 +0000
862 Diff Diff GeorgeS picture GeorgeS Thu 15 Nov, 2012 02:23:36 +0000

On the Way...

861 GeorgeS picture GeorgeS Mon 05 Nov, 2012 13:55:32 +0000