litesoft
@ 809
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 |
// 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.simpletypes.temporal.nonpublic.*; import org.litesoft.core.typeutils.*; import org.litesoft.logger.*; public final class ExpirationLine extends CompareSupport<ExpirationLine> { private static final Logger LOGGER = LoggerFactory.getLogger( ExpirationLine.class ); 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; SimpleDate zDate = today(); zDate.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 today().daysTill( getExpirationDate() ); } public SimpleDate getExpiration() { return new SimpleDate( "MM/yyyy", mYear, mMonth ); } public SimpleDate getExpirationDate() { return new SimpleDate( SimpleDate.DEFAULT_DATE_FORMAT, mYear, mMonth, CalendarSupport.daysInMonth( mYear, mMonth ) ); } public int getQuantity() { return mQuantity; } public int getMonth() { return mMonth; } public int getYear() { return mYear; } private SimpleDate today() { return new SimpleDate( SimpleDate.DEFAULT_DATE_FORMAT, new UtilDateAdaptor() ); } 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( SimpleDate 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( SimpleDate pExpDate, int pMonth, int pYear ) { return (pExpDate != null) && (pExpDate.getMonth() == pMonth) && (pExpDate.getYear() == pYear); } } |