]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.util/src/org/argeo/util/naming/dns/SrvRecord.java
Cosmetic improvements
[lgpl/argeo-commons.git] / org.argeo.util / src / org / argeo / util / naming / dns / SrvRecord.java
1 package org.argeo.util.naming.dns;
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(false);
24 String otherHost = other.toHost(false);
25 if (host.length() == otherHost.length())
26 return host.compareTo(otherHost);
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(boolean withPort) {
47 String hostStr = hostname;
48 if (hostname.charAt(hostname.length() - 1) == '.')
49 hostStr = hostname.substring(0, hostname.length() - 1);
50 return hostStr + (withPort ? ":" + port : "");
51 }
52 }