/* * [MaxiZD.java] * * Summary: Zip Descriptor. Describes one zip archive. Zip files are never modified once created. * * Copyright: (c) 2003-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 10.2 2009-04-03 tidy up code to check presence of necessary files to make it more WORA. * They may be obsoleted and their files moved to another archive however. * Replaces old ZD class. All this was to add a retiredOnTimestamp long * field. Also changed status from an int to an enum. Does not include a list of all files in the Zip. */ package com.mindprod.replicatorsender; import com.mindprod.fastcat.FastCat; import com.mindprod.replicatorcommon.Config; import com.mindprod.replicatorcommon.MiniZD; import com.mindprod.replicatorcommon.ZipnameContext; import java.io.File; import java.io.Serializable; import java.text.DecimalFormat; /** * Zip Descriptor. Describes one zip archive. Zip files are never modified once created. * * @author Roedy Green, Canadian Mind Products * @version 10.2 2009-04-03 tidy up code to check presence of necessary files to make it more WORA. * They may be obsoleted and their files moved to another archive howevever. * Replaces old ZD class. All this was to add a retiredOnTimestamp long * field. Also changed status from an int to an enum. Does not include a list of all files in the Zip. * @since 2003-08-28 */ public final class MaxiZD extends MiniZD implements Serializable { /** * Layout version number, also acts as file type identifier. */ public static final long serialVersionUID = 44L; /** * display ratio as a percentage */ private static final DecimalFormat PERCENT_FORMAT = new DecimalFormat( "##0.0%" ); /** * Status of this zip archive. */ public ZDStatus status = ZDStatus.A_UNCREATED; /** * Timestamp this zip was created. Used to control repacking. * This refers to the age of the zip file, not the age of the contents. */ public long creationTimestamp = Config.NULL_TIMESTAMP; /** * HowToProcess many total bytes of uncompressed deadwood live in this zip. Deadwood are elements that have been * updated. The old version still lives in the old zip. */ public long deadwood = 0; /** * HowToProcess many total bytes of uncompressed livewood live in this zip. Livewood are elements in active use in * the archive that have not been updated since they were placed in this zip. */ public long livewood = 0; /** * Timestamp this zip retired, used to delay emaciation/deletion. */ public long retiredOnTimestamp = Config.NULL_TIMESTAMP; /** * Constructor */ @SuppressWarnings( { "ValueOfIncrementOrDecrementUsed" } ) public MaxiZD() { // this arranges that the first file is z1.zip super( ReplicatorSender.nextZipNumber++ ); creationTimestamp = System.currentTimeMillis(); ReplicatorSender.allZips.add( this ); } /** * special constructor used for version rollover * * @param zipNumber number of associated zip file. */ public MaxiZD( int zipNumber ) { super( zipNumber ); creationTimestamp = System.currentTimeMillis(); } /** * assign a given file to this zip. maintains livewood. Does not actually create the zip file or add an element to * it. Does not modify fd. * * @param fd descriptor for the file. */ public void assignFile( MaxiFD fd ) { livewood += fd.getFileLength(); // we track only the clumping of the last file of the zip. setPossiblyHigherClumpingInZip( fd.getClumping() ); } // end assignFile /** * a debugging string that display all the fields of MaxiZD * * @return full debug string representation of the MaxiZD */ public String dump() { final FastCat sb = new FastCat( 16 ); sb.append( "MaxiZD z" ); sb.append( zipNumber ); sb.append( " status:" ); sb.append( status ); sb.append( " deadwood:" ); sb.append( deadwood ); sb.append( " " ); final double denom = deadwood + livewood; final double ratio = denom == 0 ? 0 : deadwood / denom; sb.append( PERCENT_FORMAT.format( ratio ) ); sb.append( " livewood:" ); sb.append( livewood ); sb.append( " created:" ); sb.append( Config.TIMESTAMP_MILLISECOND_FORMAT.format( creationTimestamp ) ); // no need for separate clumping, part of lastElement. if ( retiredOnTimestamp != Config.NULL_TIMESTAMP ) { sb.append( " retiredOnTimestamp:" ); sb.append( Config.TIMESTAMP_MILLISECOND_FORMAT.format( retiredOnTimestamp ) ); } if ( highestClumpingInZip != Config.NULL_TIMESTAMP ) { sb.append( " highestclump: " ); sb.append( Config.TIMESTAMP_MILLISECOND_FORMAT.format( highestClumpingInZip ) ); } return sb.toString(); } /** * equals uses case-insensitive definition * * @param obj other object to compare to * * @return true if have same zip numbers */ public boolean equals( Object obj ) { return obj instanceof MaxiZD && zipNumber == ( ( MaxiZD ) obj ).zipNumber; } /** * get the fully qualified filename of this zip: e.g. E:\mindprod\replicator\z42.zip * * @param where which name, ON_SOURCE, ON_TARGET, ON_RECEIVER_ZIP_URL, ON_WEBSITE, RELATIVE * * @return fully qualified name of the zip file. */ public String getZipFilename( final ZipnameContext where ) { switch ( where ) { case ON_SOURCE: return ConfigForSender .SENDER_ZIP_STAGING_DIR + File .separatorChar + getRelativeZipFilename(); case ON_TARGET: case ON_WEBSITE: case ON_RECEIVER_ZIP_URL: case RELATIVE: default: return super.getZipFilename( where ); } } /** * hashCode use zipNumber * * @return hashcode */ public int hashCode() { return zipNumber; } /** * Strip back to a MiniZD, just the essentials. * * @return MiniZD with just the fields needed at the receiver. */ public MiniZD strip() { return new MiniZD( this ); } }