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
package org.litesoft.core.simpletypes.temporal;

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

public abstract class AbstractCalendarYMD<T extends AbstractCalendarYMD, M extends AbstractCalendarYMD.Mutable> extends AbstractCalendarYM<T, M>
        implements CalendarAccessorYMD
{
    private static final long serialVersionUID = 1L;

    private /* final */ int mDay;

    protected AbstractCalendarYMD( int pYear, int pMonth, int pDay )
    {
        super( pYear, pMonth );
        mDay = validateDay( getYear(), getMonth(), pDay );
    }

    @Override
    public final int getDay()
    {
        return mDay;
    }

    /**
     * Return a T with the Day set to the parameter.
     */
    public final T day( int pDay )
    {
        validateDay( getYear(), getMonth(), pDay );
        return (mDay == pDay) ? us() : LLsetDay( pDay );
    }

    /**
     * Return T with the parameter pDay added.
     */
    public final T addDays( int pDays )
    {
        return (pDays == 0) ? us() : LLsetDay( mDay + pDays );
    }

    /**
     * Return T with the parameter pDay subtracted.
     */
    public final T minusDays( int pDays )
    {
        return (pDays == 0) ? us() : LLsetDay( mDay - pDays );
    }

    // vvvvvvvvvvvvvvvvvv Support methods vvvvvvvvvvvvvvvvv

    protected static final int parseDay(String pStringForm, String... pParts)
    {
        return parseInt( pParts[2], "Day", pStringForm );
    }

    protected static final StringBuilder formatSQLvalueYMD( CalendarAccessorYMD pAccessor, StringBuilder pSB )
    {
        formatSQLvalueYM( pAccessor, pSB ).append( '-' );
        return formatDay( pAccessor, pSB );
    }

    protected static final StringBuilder formatSortableDisplayFormYMD( CalendarAccessorYMD pAccessor, StringBuilder pSB )
    {
        formatSortableDisplayFormYM( pAccessor, pSB ).append( '/' );
        return formatDay( pAccessor, pSB );
    }

    protected static final StringBuilder formatDay( CalendarAccessorYMD pAccessor, StringBuilder pSB )
    {
        dd_Chunk.appendTo( pSB, pAccessor );
        return pSB;
    }

    protected static final int validateDay( int pYear, int pMonth, int pDay )
            throws IllegalArgumentException
    {
        if ( (pDay < 1) || (Month.daysIn( pYear, pMonth ) < pDay) )
        {
            throw new IllegalArgumentException( "Invalid Day provided (Year: " + pYear + ", Month: " + pMonth + "): " + pDay );
        }
        return pDay;
    }

    protected int LL_hashCode()
    {
        return hashCodeEm( super.hashCode(), mDay );
    }

    /**
     * T is non-null and of the same class, so should check everything!
     */
    protected boolean LL_equalsNonNullStrict( T them )
    {
        return super.LL_equalsNonNullStrict( them ) && this.getDay() == them.getDay();
    }

    protected Compare LL_compareNonNullLoose( Object them )
    {
        Compare zCompare = super.LL_compareNonNullLoose( them );
        if ( (zCompare.result() == EQUAL) && (them instanceof CalendarAccessorYMD) )
        {
            zCompare = zCompare.then( this.getDay(), ((CalendarAccessorYMD) them).getDay() );
        }
        return zCompare;
    }

    /**
     * @param pNewDay != current
     */
    protected final T LLsetDay( int pNewDay )
    {
        M zMutable = createMutable( getYear(), getMonth(), pNewDay );
        zMutable.normalize();
        return createTypeFrom( zMutable );
    }

    protected abstract M createMutable( int pYear, int pMonth, int pNewDay );

    protected static class Mutable extends AbstractCalendarYM.Mutable implements CalendarAccessorYMD
    {
        private int mDay;
        private boolean mDayConstrained;

        public Mutable( int pYear, int pMonth, int pDay, boolean pDayConstrained )
        {
            super( pYear, pMonth );
            mDay = pDay;
            mDayConstrained = pDayConstrained;
        }

        public final int getDay()
        {
            return mDay;
        }

        public void normalize()
        {
            super.normalize();
            while ( mDay < 1 )
            {
                tooSmallDay();
            }
            while ( 28 < mDay )
            {
                if ( !tooLargeDay() )
                {
                    break;
                }
            }
        }

        protected final void decrementDay()
        {
            if ( --mDay < 1 )
            {
                tooSmallDay();
            }
        }

        protected final void incrementDay()
        {
            if ( 28 < ++mDay )
            {
                tooLargeDay();
            }
        }

        private void tooSmallDay()
        {
            decrementMonth();
            mDay += Month.daysIn( getYear(), getMonth() );
        }

        private boolean tooLargeDay()
        {
            int zDaysInMonth = Month.daysIn( getYear(), getMonth() );
            if ( mDay <= zDaysInMonth )
            {
                return false;
            }
            if ( mDayConstrained )
            {
                mDay = zDaysInMonth;
                return false;
            }
            mDay -= zDaysInMonth;
            incrementMonth();
            return true;
        }
    }
}

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

Diff revisions: vs.
Revision Author Commited Message
873 Diff Diff GeorgeS picture GeorgeS Tue 27 Nov, 2012 17:41:11 +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 Diff Diff GeorgeS picture GeorgeS Mon 05 Nov, 2012 13:55:32 +0000
859 GeorgeS picture GeorgeS Mon 05 Nov, 2012 01:26:38 +0000