View Javadoc
1   package org.argeo.naming;
2   
3   class SrvRecord implements Comparable<SrvRecord> {
4   	private final Integer priority;
5   	private final Integer weight;
6   	private final Integer port;
7   	private final String hostname;
8   
9   	public SrvRecord(Integer priority, Integer weight, Integer port, String hostname) {
10  		this.priority = priority;
11  		this.weight = weight;
12  		this.port = port;
13  		this.hostname = hostname;
14  	}
15  
16  	@Override
17  	public int compareTo(SrvRecord other) {
18  		// https: // en.wikipedia.org/wiki/SRV_record
19  		if (priority != other.priority)
20  			return priority - other.priority;
21  		if (weight != other.weight)
22  			return other.weight - other.weight;
23  		String host = toHost();
24  		String otherHost = other.toHost();
25  		if (host.length() == otherHost.length())
26  			return toHost().compareTo(other.toHost());
27  		else
28  			return host.length() - otherHost.length();
29  	}
30  
31  	@Override
32  	public boolean equals(Object obj) {
33  		if (obj instanceof SrvRecord) {
34  			SrvRecord other = (SrvRecord) obj;
35  			return priority == other.priority && weight == other.weight && port == other.port
36  					&& hostname.equals(other.hostname);
37  		}
38  		return false;
39  	}
40  
41  	@Override
42  	public String toString() {
43  		return priority + " " + weight;
44  	}
45  
46  	public String toHost() {
47  		String hostStr = hostname;
48  		if (hostname.charAt(hostname.length() - 1) == '.')
49  			hostStr = hostname.substring(0, hostname.length() - 1);
50  		return hostStr + ":" + port;
51  	}
52  }