litesoft
@ 947
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 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 |
// This Source Code is in the Public Domain per: http://unlicense.org package org.litesoft.core.simpletypes.temporal; import org.litesoft.core.simpletypes.nonpublic.*; import org.litesoft.core.simpletypes.temporal.nonpublic.*; /** * A Simple Timespan, which consists of a number of days, Hours, Mins, Secs, & MilliSecs. */ public final class SimpleTimespan extends CalendarSupport<SimpleTimespan> implements TemporalSimpleType { private static final long serialVersionUID = 1L; public static final String DEFAULT_TS_FORMAT = "ddd HH:mm:ss.SSS"; // Note this is representational - IT IS NOT USED! private int mDays, mHours, mMins, mSecs, mMilliSecs; /** * @deprecated - for Serialization */ protected SimpleTimespan() { } public SimpleTimespan( int pDays, int pHours, int pMins, int pSecs, int pMilliSecs ) throws IllegalArgumentException { setDays( pDays ); setHours( pHours ); setMins( pMins ); setSecs( pSecs ); setMilliSecs( pMilliSecs ); } public SimpleTimespan( int pDays, int pHours, int pMins, int pSecs ) throws IllegalArgumentException { this( pDays, pHours, pMins, pSecs, 0 ); } public SimpleTimespan( int pDays, int pHours, int pMins ) throws IllegalArgumentException { this( pDays, pHours, pMins, 0 ); } public SimpleTimespan( int pDays, int pHours ) throws IllegalArgumentException { this( pDays, pHours, 0 ); } public SimpleTimespan( int pDays ) throws IllegalArgumentException { this( pDays, 0 ); } public SimpleTimespan copy() { return new SimpleTimespan( getDays(), getHours(), getMins(), getSecs(), getMilliSecs() ); } public int getDays() { return mDays; } public int getHours() { return mHours; } public int getMins() { return mMins; } public int getSecs() { return mSecs; } public int getMilliSecs() { return mMilliSecs; } public void setDays( int pDays ) throws IllegalArgumentException { if ( pDays < 0 ) { throw new IllegalArgumentException( "Negative Spans not supported" ); } mDays = pDays; } public void setHours( int pHours ) { mHours = validate( "Hour", pHours, 23 ); } public void setMins( int pMins ) { mMins = validate( "Min", pMins, 59 ); } public void setSecs( int pSecs ) { mSecs = validate( "Sec", pSecs, 59 ); } public void setMilliSecs( int pMilliSecs ) { mMilliSecs = validate( "MilliSec", pMilliSecs, 999 ); } /** * Adjust the Days. Note: Negative Durations are Not Supported and will give unreliable results. */ public void addDays( int pDays ) { if ( pDays != 0 ) { mDays += pDays; normalize(); } } /** * Adjust the Hours. Note: Negative Durations are Not Supported and will give unreliable results. */ public void addHours( int pHours ) { if ( pHours != 0 ) { mHours += pHours; normalize(); } } /** * Adjust the Minutes. Note: Negative Durations are Not Supported and will give unreliable results. */ public void addMins( int pMins ) throws IllegalArgumentException { if ( pMins != 0 ) { mMins += pMins; normalize(); } } /** * Adjust the Seconds. Note: Negative Durations are Not Supported and will give unreliable results. */ public void addSecs( int pSecs ) throws IllegalArgumentException { if ( pSecs != 0 ) { mSecs += pSecs; normalize(); } } /** * Adjust the MilliSeconds. Note: Negative Durations are Not Supported and will give unreliable results. */ public void addMilliSecs( int pMilliSecs ) throws IllegalArgumentException { if ( pMilliSecs != 0 ) { mMilliSecs += pMilliSecs; normalize(); } } private void normalize() { while ( mMilliSecs >= 1000 ) { mSecs++; mMilliSecs -= 1000; } while ( mMilliSecs < 0 ) { mSecs--; mMilliSecs += 1000; } while ( mSecs >= 60 ) { mMins++; mSecs -= 60; } while ( mSecs < 0 ) { mMins--; mSecs += 60; } while ( mMins >= 60 ) { mHours++; mMins -= 60; } while ( mMins < 0 ) { mHours--; mMins += 60; } while ( mHours >= 24 ) { mDays++; mHours -= 24; } while ( mHours < 0 ) { mDays--; mHours += 24; } } /** * True if this is 'lessthan' pThem. * <p/> * * @param pThem !null * * @throws IllegalArgumentException if pThem is null */ public boolean lessthan( SimpleTimespan pThem ) throws IllegalArgumentException { return LLltORgt( pThem, "lessthan", -1 ); } /** * True if this is 'greaterthan' pThem. * <p/> * * @param pThem !null * * @throws IllegalArgumentException if pThem is null */ public boolean greaterthan( SimpleTimespan pThem ) throws IllegalArgumentException { return LLltORgt( pThem, "greaterthan", 1 ); } private boolean LLltORgt( SimpleTimespan pThem, String pWhat, int pCompareRetValue ) throws IllegalArgumentException { undefinedResultIfNull( pWhat, pThem ); return pCompareRetValue == compareTo( pThem ); } @Override public boolean equals( Object o ) { return (this == o) || // ((o instanceof SimpleTimespan) && equals( (SimpleTimespan) o )); } public boolean equals( SimpleTimespan them ) { return (this == them) || // ((them != null) // && equal( this.mDays, them.mDays ) // && equal( this.mHours, them.mHours ) // && equal( this.mMins, them.mMins ) // && equal( this.mSecs, them.mSecs ) // && equal( this.mMilliSecs, them.mMilliSecs ) // ); } @Override public int hashCode() { return hashCodeEm( calcHashCode( mDays ), // calcHashCode( mHours ), // calcHashCode( mMins ), // calcHashCode( mSecs ), // calcHashCode( mMilliSecs ) ); } @Override public int compareTo( SimpleTimespan them ) { return compareEm( compare( this.mDays, them.mDays ), // compare( this.mHours, them.mHours ), // compare( this.mMins, them.mMins ), // compare( this.mSecs, them.mSecs ), // compare( this.mMilliSecs, them.mMilliSecs ) ); } public static String toString( SimpleTimespan pFromString ) { return (pFromString != null) ? pFromString.toString() : null; } @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append( mDays ); if ( mMilliSecs != 0 ) { sb.append( ' ' ).append( new SimpleTime( mHours, mMins, mSecs, mMilliSecs ) ); } else if ( mSecs != 0 ) { sb.append( ' ' ).append( new SimpleTime( mHours, mMins, mSecs ) ); } else if ( mMins != 0 ) { sb.append( ' ' ).append( new SimpleTime( mHours, mMins ) ); } else if ( mHours != 0 ) { sb.append( ' ' ).append( new SimpleTime( mHours ) ); } return sb.toString(); } public static SimpleTimespan fromString( String pToString ) throws IllegalArgumentException { if ( pToString == null ) { return null; } // yyyy-MM-dd HH:mm:ss.SSS String[] parts = SimpleParser.parse( pToString, " ::.", 1 ); int zDays = parseInt( parts[0], "Days", pToString ); switch ( parts.length ) { case 2: return new SimpleTimespan( zDays, // parseInt( parts[1], "Hours", pToString ) ); case 3: return new SimpleTimespan( zDays, // parseInt( parts[1], "Hours", pToString ), // parseInt( parts[2], "Mins", pToString ) ); case 4: return new SimpleTimespan( zDays, // parseInt( parts[1], "Hours", pToString ), // parseInt( parts[2], "Mins", pToString ), // parseInt( parts[3], "Secs", pToString ) ); case 5: return new SimpleTimespan( zDays, // parseInt( parts[1], "Hours", pToString ), // parseInt( parts[2], "Mins", pToString ), // parseInt( parts[3], "Secs", pToString ), // parseInt( parts[4], "MilliSecs", pToString ) ); default: throw new IllegalStateException( "parts.length = " + parts.length ); } } } |
Commits for litesoft/trunk/Java/core/Anywhere/src/org/litesoft/core/simpletypes/temporal/SimpleTimespan.java
Revision | Author | Commited | Message |
---|---|---|---|
947 Diff | GeorgeS | Fri 06 Jun, 2014 23:36:56 +0000 | Correct Spelling of package! |
850 Diff | GeorgeS | Tue 11 Sep, 2012 17:23:33 +0000 | |
849 Diff | GeorgeS | Tue 11 Sep, 2012 17:11:59 +0000 | Clean up serialization |
50 Diff | GeorgeS | Tue 13 Apr, 2010 11:51:38 +0000 | |
49 Diff | GeorgeS | Mon 12 Apr, 2010 02:59:10 +0000 | License Text |
2 | GeorgeS | Sun 07 Feb, 2010 12:50:58 +0000 |