Skip to content

Commit 4d68883

Browse files
committed
Add a Pair class, and pretty-wrap it into a NamespaceDescriptor class
NamespaceDescriptor describes a gir namespace with its name and version, which is essentially a pair of strings. The pair is wrapped into a specific class for type safety and clarity.
1 parent d49ef88 commit 4d68883

2 files changed

Lines changed: 84 additions & 0 deletions

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package gir2java;
2+
3+
public class NamespaceDescriptor extends Pair<String, String> {
4+
5+
public NamespaceDescriptor(String name, String version) {
6+
super(name, version);
7+
}
8+
9+
public String getName() {
10+
return getA();
11+
}
12+
13+
public String getVersion() {
14+
return getB();
15+
}
16+
17+
@Override
18+
public String toString() {
19+
StringBuilder sb = new StringBuilder();
20+
sb.append(getName());
21+
sb.append('-');
22+
sb.append(getVersion());
23+
return sb.toString();
24+
}
25+
}

src/gir2java/Pair.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package gir2java;
2+
3+
/**
4+
* The usual Pair class. Immutable by default, but can be subclassed to make a mutable version.
5+
* @author relek
6+
*
7+
* @param <TA>
8+
* @param <TB>
9+
*/
10+
public class Pair<TA, TB> {
11+
12+
protected TA a;
13+
protected TB b;
14+
15+
public Pair(TA a, TB b) {
16+
this.a = a;
17+
this.b = b;
18+
}
19+
20+
TA getA() {
21+
return a;
22+
}
23+
24+
TB getB() {
25+
return b;
26+
}
27+
28+
@Override
29+
public int hashCode() {
30+
final int prime = 31;
31+
int result = 1;
32+
result = prime * result + ((a == null) ? 0 : a.hashCode());
33+
result = prime * result + ((b == null) ? 0 : b.hashCode());
34+
return result;
35+
}
36+
37+
@Override
38+
public boolean equals(Object obj) {
39+
if (this == obj)
40+
return true;
41+
if (obj == null)
42+
return false;
43+
if (getClass() != obj.getClass())
44+
return false;
45+
Pair other = (Pair) obj;
46+
if (a == null) {
47+
if (other.a != null)
48+
return false;
49+
} else if (!a.equals(other.a))
50+
return false;
51+
if (b == null) {
52+
if (other.b != null)
53+
return false;
54+
} else if (!b.equals(other.b))
55+
return false;
56+
return true;
57+
}
58+
59+
}

0 commit comments

Comments
 (0)