View Javadoc

1   package net.sf.jpkgmk.pkgmap;
2   
3   import net.sf.jpkgmk.AbstractLineProvider;
4   import net.sf.jpkgmk.util.ObjectUtils;
5   
6   import org.apache.commons.logging.Log;
7   import org.apache.commons.logging.LogFactory;
8   
9   /**
10   * Header entry line for a pkgmap file
11   * 
12   * Each pkgmap file must have one line that provides information about the 
13   * number of parts, 
14   * maximum size of parts that make up the package, and, 
15   * optionally, the size of the package after compression (where size is given in 512-byte blocks). 
16   * This line is in the following format:
17   * <pre>
18   * : number_of_parts  maximum_part_size  compressed_pkg_size 
19   * </pre>
20   * 
21   * @author gommma (gommma AT users.sourceforge.net)
22   * @author Last changed by: $Author: gommma $
23   * @version $Revision: 2 $ $Date: 2008-08-20 21:14:19 +0200 (Mi, 20 Aug 2008) $
24   * @since 1.0
25   */
26  public class PkgMapEntryHeader extends AbstractLineProvider implements PkgMapEntry
27  {
28  	private PkgMapEntryType type = PkgMapEntryType.HEADER;
29  	
30  	private Integer numberOfParts;
31  	private Long maximumPartSize;
32  	private Long compressedPkgSize;
33  	
34  	
35  	public PkgMapEntryHeader(Integer numberOfParts, Long maximumPartSize) {
36  		this(numberOfParts, maximumPartSize, null);
37  	}
38  
39  
40  	public PkgMapEntryHeader(Integer numberOfParts, Long maximumPartSize,
41  			Long compressedPkgSize) {
42  		super();
43  		
44  		if(numberOfParts==null) {
45  			throw new NullPointerException("The parameter 'numberOfParts' must not be null");
46  		}
47  		if (maximumPartSize == null) {
48  			throw new NullPointerException(
49  					"The parameter 'maximumPartSize' must not be null");
50  		}
51  
52  		this.numberOfParts = numberOfParts;
53  		this.maximumPartSize = maximumPartSize;
54  		this.compressedPkgSize = compressedPkgSize;
55  	}
56  
57  
58  	public String getLine() {
59  		StringBuffer sb = new StringBuffer();
60  		sb.append(getKey());
61  		sb.append(" ").append(numberOfParts);
62  		sb.append(" ").append(maximumPartSize);
63  
64  		if(this.compressedPkgSize != null) {
65  			sb.append(" ").append(compressedPkgSize);
66  		}
67  		return sb.toString();
68  	}
69  
70  	public PkgMapEntryType getType() {
71  		return this.type;
72  	}
73  	
74  	/**
75  	 * @return The key of the header line which is a ":" (colon)
76  	 */
77  	public String getKey() {
78  		return this.type.getKey();
79  	}
80  
81  	@Override
82  	public int hashCode() {
83  		final int prime = 31;
84  		int result = 1;
85  		result = prime
86  				* result
87  				+ ((compressedPkgSize == null) ? 0 : compressedPkgSize
88  						.hashCode());
89  		result = prime * result
90  				+ ((maximumPartSize == null) ? 0 : maximumPartSize.hashCode());
91  		result = prime * result
92  				+ ((numberOfParts == null) ? 0 : numberOfParts.hashCode());
93  		result = prime * result + ((type == null) ? 0 : type.hashCode());
94  		return result;
95  	}
96  
97  
98  	@Override
99  	public boolean equals(Object obj) {
100 		if (this == obj)
101 			return true;
102 		if (obj == null)
103 			return false;
104 		if (getClass() != obj.getClass())
105 			return false;
106 		final PkgMapEntryHeader other = (PkgMapEntryHeader) obj;
107 		
108 		if(!ObjectUtils.areObjectsEqual(this.compressedPkgSize, other.compressedPkgSize))
109 			return false;
110 		if(!ObjectUtils.areObjectsEqual(this.maximumPartSize, other.maximumPartSize))
111 			return false;
112 		if(!ObjectUtils.areObjectsEqual(this.numberOfParts, other.numberOfParts))
113 			return false;
114 		if(!ObjectUtils.areObjectsEqual(this.type, other.type))
115 			return false;
116 
117 		return true;
118 	}
119 
120 	public boolean equals(Object obj, boolean ignoreLastModified) {
121 		return this.equals(obj);
122 	}
123 
124 	@Override
125 	public String toString() {
126 		StringBuffer sb = new StringBuffer();
127 		sb.append("numberOfParts=").append(numberOfParts);
128 		sb.append(",maximumPartSize=").append(maximumPartSize);
129 		sb.append(",compressedPkgSize=").append(compressedPkgSize);
130 		return sb.toString();
131 	}
132 	
133 	
134 	
135 	
136 	public static class PkgMapEntryHeaderParser implements PkgMapEntryParser
137 	{
138 		private Log log = LogFactory.getLog(PkgMapEntryHeaderParser.class);
139 		
140 		public PkgMapEntryHeaderParser()
141 		{
142 			
143 		}
144 		
145 		public boolean isHeaderLine(String line)
146 		{
147 			if(line.startsWith(PkgMapEntryType.HEADER.getKey())) {
148 				return true;
149 			}
150 			else {
151 				return false;
152 			}
153 		}
154 		
155 		public PkgMapEntryHeader parse(String line)
156 		{
157 			// Whitespace is the separator
158 			String[] entryList = line.split("\\s");
159 			// Must have at least 3 arguments: ": numParts maxPartSize"
160 			if(entryList.length < 3) {
161 				throw new IllegalArgumentException("The line '" + line + "' is invalid: must consist at least of 3 parameters");
162 			}
163 			
164 			String first = entryList[0];
165 			String second = entryList[1];
166 			String third = entryList[2];
167 			String fourth = null;
168 			
169 			// Has optional 4. parameter?
170 			if(entryList.length == 4) {
171 				fourth = entryList[3];
172 			}
173 			if(entryList.length > 4) {
174 				fourth = entryList[3];
175 				log.warn("Line has more than 4 entries - ignoring all after the 4th one... " + line);
176 			}
177 			
178 			// Parameter check
179 			if(!first.equals(PkgMapEntryType.HEADER.getKey())) {
180 				throw new IllegalArgumentException("The first parameter '" + first + "' is not the header key '" + PkgMapEntryType.HEADER.getKey() +"'");
181 			}
182 			Integer numberOfParts = new Integer(second);
183 			Long maxPartSize = new Long(third);
184 			
185 			Long compressedPkgSize = null;
186 			if(fourth != null && !fourth.equals("")) {
187 				compressedPkgSize = new Long(fourth);
188 			}
189 			
190 			PkgMapEntryHeader header = new PkgMapEntryHeader(numberOfParts, maxPartSize, compressedPkgSize);
191 			return header;
192 		}
193 	}
194 
195 }