View Javadoc
1   /*
2    * Copyright (C) 2007-2012 Argeo GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.argeo.slc;
17  
18  
19  /** Canonical implementation of {@link NameVersion} */
20  public class DefaultNameVersion implements NameVersion,
21  		Comparable<NameVersion> {
22  	private String name;
23  	private String version;
24  
25  	public DefaultNameVersion() {
26  	}
27  
28  	/** Interprets string in OSGi-like format my.module.name;version=0.0.0 */
29  	public DefaultNameVersion(String nameVersion) {
30  		int index = nameVersion.indexOf(";version=");
31  		if (index < 0) {
32  			setName(nameVersion);
33  			setVersion(null);
34  		} else {
35  			setName(nameVersion.substring(0, index));
36  			setVersion(nameVersion.substring(index + ";version=".length()));
37  		}
38  	}
39  
40  	public DefaultNameVersion(String name, String version) {
41  		this.name = name;
42  		this.version = version;
43  	}
44  
45  	public DefaultNameVersion(NameVersion nameVersion) {
46  		this.name = nameVersion.getName();
47  		this.version = nameVersion.getVersion();
48  	}
49  
50  	public String getName() {
51  		return name;
52  	}
53  
54  	public void setName(String name) {
55  		this.name = name;
56  	}
57  
58  	public String getVersion() {
59  		return version;
60  	}
61  
62  	public void setVersion(String version) {
63  		this.version = version;
64  	}
65  
66  	@Override
67  	public boolean equals(Object obj) {
68  		if (obj instanceof NameVersion) {
69  			NameVersion nameVersion = (NameVersion) obj;
70  			return name.equals(nameVersion.getName())
71  					&& version.equals(nameVersion.getVersion());
72  		} else
73  			return false;
74  	}
75  
76  	@Override
77  	public int hashCode() {
78  		return name.hashCode() + version.hashCode();
79  	}
80  
81  	@Override
82  	public String toString() {
83  		return name + ":" + version;
84  	}
85  
86  	public int compareTo(NameVersion o) {
87  		if (o.getName().equals(name))
88  			return version.compareTo(o.getVersion());
89  		else
90  			return name.compareTo(o.getName());
91  	}
92  }