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.impl;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import net.sourceforge.javadpkg.SymbolsEntry;
25  import net.sourceforge.javadpkg.control.PackageDependency;
26  import net.sourceforge.javadpkg.control.PackageVersion;
27  
28  
29  /**
30   * <p>
31   * A {@link SymbolsEntry} implementation.
32   * </p>
33   *
34   * @author Gerrit Hohl (gerrit-hohl@users.sourceforge.net)
35   * @version <b>1.0</b>, 11.01.2016 by Gerrit Hohl
36   */
37  public class SymbolsEntryImpl implements SymbolsEntry {
38  	
39  	
40  	/** The name of the library. */
41  	private String				libraryName;
42  	/** The dependency as string value. */
43  	private String				dependencyValue;
44  	/** The dependency. */
45  	private PackageDependency	dependency;
46  
47  	/** The alternative dependency as string value. */
48  	private String				alternativeDependencyValue;
49  	/** The symbols. */
50  	private List<Symbol>		symbols;
51  	/** The build dependency. */
52  	private PackageDependency	buildDependsPackage;
53  
54  
55  	/**
56  	 * <p>
57  	 * Creates an entry.
58  	 * </p>
59  	 *
60  	 * @param libraryName
61  	 *            The name of the library.
62  	 * @throws IllegalArgumentException
63  	 *             If the name is <code>null</code>.
64  	 */
65  	private SymbolsEntryImpl(String libraryName) {
66  		super();
67  		
68  		if (libraryName == null)
69  			throw new IllegalArgumentException("Argument libraryName is null.");
70  			
71  		this.libraryName = libraryName;
72  		this.dependencyValue = null;
73  		this.dependency = null;
74  
75  		this.alternativeDependencyValue = null;
76  		this.symbols = new ArrayList<>();
77  	}
78  	
79  	
80  	/**
81  	 * <p>
82  	 * Creates an entry.
83  	 * </p>
84  	 *
85  	 * @param libraryName
86  	 *            The name of the library.
87  	 * @param dependencyValue
88  	 *            The dependency as string value.
89  	 * @throws IllegalArgumentException
90  	 *             If any of the parameters are <code>null</code>.
91  	 */
92  	public SymbolsEntryImpl(String libraryName, String dependencyValue) {
93  		this(libraryName);
94  
95  		if (dependencyValue == null)
96  			throw new IllegalArgumentException("Argument dependencyValue is null.");
97  			
98  		this.dependencyValue = dependencyValue;
99  	}
100 
101 
102 	/**
103 	 * <p>
104 	 * Creates an entry.
105 	 * </p>
106 	 *
107 	 * @param libraryName
108 	 *            The name of the library.
109 	 * @param dependency
110 	 *            The dependency.
111 	 * @throws IllegalArgumentException
112 	 *             If any of the parameters are <code>null</code>.
113 	 */
114 	public SymbolsEntryImpl(String libraryName, PackageDependency dependency) {
115 		this(libraryName);
116 		
117 		if (dependency == null)
118 			throw new IllegalArgumentException("Argument dependency is null.");
119 
120 		this.dependency = dependency;
121 	}
122 	
123 	
124 	/**
125 	 * <p>
126 	 * Sets the alternative dependency as string value.
127 	 * </p>
128 	 *
129 	 * @param alternativeDependencyValue
130 	 *            The alternative dependency.
131 	 */
132 	public void setAlternativeDependencyValue(String alternativeDependencyValue) {
133 		this.alternativeDependencyValue = alternativeDependencyValue;
134 	}
135 
136 
137 	/**
138 	 * <p>
139 	 * Adds a symbol.
140 	 * </p>
141 	 *
142 	 * @param name
143 	 *            The name of the symbol.
144 	 * @param version
145 	 *            The version of the symbol.
146 	 * @param upstreamVersion
147 	 *            The upstream version.
148 	 */
149 	public void addSymbol(String name, String version, PackageVersion upstreamVersion) {
150 		Symbol symbol;
151 		
152 		
153 		if (name == null)
154 			throw new IllegalArgumentException("Argument name is null.");
155 		if (version == null)
156 			throw new IllegalArgumentException("Argument version is null.");
157 		if (upstreamVersion == null)
158 			throw new IllegalArgumentException("Argument upstreamVersion is null.");
159 
160 		symbol = new Symbol(name, version, upstreamVersion);
161 		this.symbols.add(symbol);
162 	}
163 	
164 	
165 	/**
166 	 * <p>
167 	 * Sets the build dependency.
168 	 * </p>
169 	 *
170 	 * @param buildDependsPackage
171 	 *            The build dependency.
172 	 */
173 	public void setBuildDependsPackage(PackageDependency buildDependsPackage) {
174 		this.buildDependsPackage = buildDependsPackage;
175 	}
176 	
177 	
178 	@Override
179 	public String getText() {
180 		StringBuilder sb;
181 		
182 		
183 		sb = new StringBuilder();
184 		sb.append(this.libraryName);
185 		sb.append(' ');
186 		if (this.dependency == null)
187 			sb.append(this.dependencyValue);
188 		else {
189 			// TODO Need a PackageDependencyTransformer.
190 		}
191 		if (this.alternativeDependencyValue != null) {
192 			sb.append("\n| ");
193 			sb.append(this.alternativeDependencyValue);
194 		}
195 		if (this.symbols.isEmpty())
196 			sb.append('\n');
197 		else {
198 			for (Symbol symbol : this.symbols) {
199 				sb.append("\n ");
200 				sb.append(symbol.getName());
201 				sb.append('@');
202 				sb.append(symbol.getVersion());
203 				sb.append(' ');
204 				sb.append(symbol.getUpstreamVersion().getText());
205 			}
206 		}
207 		if (this.buildDependsPackage != null) {
208 			// TODO Need a PackageDependencyTransformer.
209 		}
210 		return sb.toString();
211 	}
212 
213 
214 	/* **********************************************************************
215 	 * **********************************************************************
216 	 * **********************************************************************
217 	 * **********************************************************************
218 	 * **********************************************************************
219 	 */
220 	
221 	
222 	/**
223 	 * <p>
224 	 * A symbol.
225 	 * </p>
226 	 *
227 	 * @author Gerrit Hohl (gerrit-hohl@users.sourceforge.net)
228 	 * @version <b>1.0</b>, 11.01.2016 by Gerrit Hohl
229 	 */
230 	private class Symbol {
231 		
232 		
233 		/** The name of the symbol. */
234 		private String			name;
235 		/** The version of the symbol. */
236 		private String			version;
237 		/** The upstream version. */
238 		private PackageVersion	upstreamVersion;
239 
240 
241 		/**
242 		 * <p>
243 		 * Creates a symbol.
244 		 * </p>
245 		 *
246 		 * @param name
247 		 *            The name of the symbol.
248 		 * @param version
249 		 *            The version of the symbol.
250 		 * @param upstreamVersion
251 		 *            The upstream version.
252 		 */
253 		public Symbol(String name, String version, PackageVersion upstreamVersion) {
254 			super();
255 			
256 			this.name = name;
257 			this.version = version;
258 			this.upstreamVersion = upstreamVersion;
259 		}
260 
261 
262 		/**
263 		 * <p>
264 		 * Returns the name of the symbol.
265 		 * </p>
266 		 *
267 		 * @return The name.
268 		 */
269 		public String getName() {
270 			return this.name;
271 		}
272 
273 
274 		/**
275 		 * <p>
276 		 * Returns the version of the symbol.
277 		 * </p>
278 		 *
279 		 * @return The version.
280 		 */
281 		public String getVersion() {
282 			return this.version;
283 		}
284 
285 
286 		/**
287 		 * <p>
288 		 * Returns the upstream version.
289 		 * </p>
290 		 *
291 		 * @return The upstream version.
292 		 */
293 		public PackageVersion getUpstreamVersion() {
294 			return this.upstreamVersion;
295 		}
296 
297 
298 	}
299 	
300 	
301 }