View Javadoc
1   /*
2    * dpkg - Debian Package library and the Debian Package Maven plugin
3    * (c) Copyright 2016 Gerrit Hohl
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18   */
19  package net.sourceforge.javadpkg.store;
20  
21  
22  /**
23   * <p>
24   * A {@link FileHash} implementation.
25   * </p>
26   *
27   * @author Gerrit Hohl (gerrit-hohl@users.sourceforge.net)
28   * @version <b>1.0</b>, 27.04.2016 by Gerrit Hohl
29   */
30  public class FileHashImpl implements FileHash {
31  
32  
33  	/** The name of the source. */
34  	private String	name;
35  	/** The path in the target system. */
36  	private String	path;
37  	/** The hash of the content. */
38  	private byte[]	hash;
39  	/** The hash of the content as hex number. */
40  	private String	hashAsHex;
41  
42  
43  	/**
44  	 * <p>
45  	 * Creates a file hash.
46  	 * </p>
47  	 *
48  	 * @param name
49  	 *            The name of the source.
50  	 * @param path
51  	 *            The path in the target system.
52  	 * @param hash
53  	 *            The hash of the content.
54  	 * @throws IllegalArgumentException
55  	 *             If any of the parameters are <code>null</code>.
56  	 */
57  	public FileHashImpl(String name, String path, byte[] hash) {
58  		super();
59  		
60  		if (name == null)
61  			throw new IllegalArgumentException("Argument name is null.");
62  		if (path == null)
63  			throw new IllegalArgumentException("Argument path is null.");
64  		if (hash == null)
65  			throw new IllegalArgumentException("Argument hash is null.");
66  			
67  		this.name = name;
68  		this.path = path;
69  		this.hash = hash.clone();
70  		this.hashAsHex = this.createHex(this.hash);
71  	}
72  
73  
74  	/**
75  	 * <p>
76  	 * Creates a hex representation of the number stored in the array.
77  	 * </p>
78  	 *
79  	 * @param data
80  	 *            The array.
81  	 * @return The hex representation.
82  	 */
83  	private String createHex(byte[] data) {
84  		StringBuilder sb;
85  		int number;
86  		
87  		
88  		sb = new StringBuilder();
89  		for (int index = 0; index < data.length; index++) {
90  			number = data[index] & 0xFF;
91  			if (number < 0x10) {
92  				sb.append('0');
93  			}
94  			sb.append(Integer.toHexString(number));
95  		}
96  		return sb.toString();
97  	}
98  
99  
100 	@Override
101 	public String getName() {
102 		return this.name;
103 	}
104 	
105 	
106 	@Override
107 	public String getPath() {
108 		return this.path;
109 	}
110 	
111 	
112 	@Override
113 	public byte[] getHash() {
114 		return this.hash.clone();
115 	}
116 	
117 	
118 	@Override
119 	public String getHashAsHex() {
120 		return this.hashAsHex;
121 	}
122 	
123 	
124 }