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
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt
package org.litesoft.core.util;

import org.litesoft.core.simpletypes.nonpublic.*;
import org.litesoft.core.simpletypes.temporal.*;
import org.litesoft.core.typeutils.*;
import org.litesoft.core.typeutils.gregorian.*;
import org.litesoft.logger.*;

public final class ExpirationLine extends CompareSupport<ExpirationLine>
{
    private static final Logger LOGGER = LoggerFactory.getLogger( ExpirationLine.class );

    public static DateFormat EXPIRATION_FORMAT = new DateFormat( "MM/yyyy" );

    private int mQuantity, mMonth, mYear;

    public ExpirationLine( int pQuantity, int pMonth, int pYear )
    {
        mQuantity = pQuantity;
        mMonth = pMonth;
        mYear = pYear;
    }

    public ExpirationLine( int pQuantity, int pDeltaMonths )
    {
        mQuantity = pQuantity;
        CalendarYMD zDate = CalendarYMD.today().addMonths( pDeltaMonths );
        mMonth = zDate.getMonth();
        mYear = zDate.getYear();
    }

    public static ExpirationLine fromString( String pFromToString )
    {
        pFromToString = Strings.noEmpty( pFromToString );
        if ( pFromToString != null )
        {
            try
            {
                String[] zParts = Strings.parseChar( pFromToString, ' ' );
                int zQuantity = Integer.parseInt( zParts[0] );
                // "expired" / "expires" ....... zParts[1]
                // "on" ....... zParts[2]
                String zMM_YYYY = zParts[3];
                if ( (zMM_YYYY.length() == 7) && (zMM_YYYY.charAt( 2 ) == '/') )
                {
                    int zMonth = Integer.parseInt( zMM_YYYY.substring( 0, 2 ) );
                    int zYear = Integer.parseInt( zMM_YYYY.substring( 3 ) );
                    return new ExpirationLine( zQuantity, zMonth, zYear );
                }
            }
            catch ( RuntimeException e )
            {
                LOGGER.warn.log( e, pFromToString );
            }
        }
        return null;
    }

    @Override
    public String toString()
    {
        return Integers.padIt( 3, mQuantity ) + " " + ((getDaysRemaining() <= 0) ? "expired on" : "expires on") + " " + getExpiration() + " ";
    }

    public int getDaysRemaining()
    {
        return CalendarYMD.today().daysTill( getExpirationDate() );
    }

    public CalendarYMD getExpiration()
    {
        return new CalendarYMD( mYear, mMonth );
    }

    public CalendarYMD getExpirationDate()
    {
        return new CalendarYMD( mYear, mMonth, Month.daysIn( mYear, mMonth ) );
    }

    public int getQuantity()
    {
        return mQuantity;
    }

    public int getMonth()
    {
        return mMonth;
    }

    public int getYear()
    {
        return mYear;
    }

    public int compareTo( ExpirationLine them )
    {
        return compareEm( compare( this.mYear, them.mYear ), //
                          compare( this.mMonth, them.mMonth ), //
                          compare( this.mQuantity, them.mQuantity ) );
    }

    public boolean equals( ExpirationLine them )
    {
        return (this == them) || //
               ((them != null) && //
                (this.mQuantity == them.mQuantity) && //
                (this.mMonth == them.mMonth) && //
                (this.mYear == them.mYear));
    }

    @Override
    public boolean equals( Object o )
    {
        return (this == o) || (o instanceof ExpirationLine && equals( (ExpirationLine) o ));
    }

    @Override
    public int hashCode()
    {
        return hashCodeEm( mQuantity, mMonth, mYear );
    }

    public static boolean expirationDateMatchesTightString( CalendarYMD pExpDate, String pFourDigitExpDate )
    {
        if ( (pFourDigitExpDate != null) && (pFourDigitExpDate.length() == 4) )
        {
            try
            {
                return expirationDateMatches( pExpDate, //
                                              Integer.parseInt( pFourDigitExpDate.substring( 0, 2 ) ), //
                                              2000 + Integer.parseInt( pFourDigitExpDate.substring( 2 ) ) );
            }
            catch ( NumberFormatException e )
            {
                // Whatever...
            }
        }
        return false;
    }

    public static boolean expirationDateMatches( CalendarYMD pExpDate, int pMonth, int pYear )
    {
        return (pExpDate != null) && (pExpDate.getMonth() == pMonth) && (pExpDate.getYear() == pYear);
    }
}

Commits for litesoft/trunk/Java/core/Anywhere/src/org/litesoft/core/util/ExpirationLine.java

Diff revisions: vs.
Revision Author Commited Message
860 Diff Diff GeorgeS picture GeorgeS Mon 05 Nov, 2012 01:39:02 +0000
853 Diff Diff GeorgeS picture GeorgeS Sun 04 Nov, 2012 15:18:21 +0000
851 Diff Diff GeorgeS picture GeorgeS Mon 08 Oct, 2012 00:05:32 +0000

Breaking the code as Temporal changes are implemented...

809 Diff Diff GeorgeS picture GeorgeS Thu 16 Aug, 2012 04:10:46 +0000
802 Diff Diff GeorgeS picture GeorgeS Wed 15 Aug, 2012 04:04:47 +0000
801 Diff Diff GeorgeS picture GeorgeS Wed 15 Aug, 2012 03:59:02 +0000
49 Diff Diff GeorgeS picture GeorgeS Mon 12 Apr, 2010 02:59:10 +0000

License Text

2 GeorgeS picture GeorgeS Sun 07 Feb, 2010 12:50:58 +0000