Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
6 changed files
with
228 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.alibaba.dcm; | ||
|
||
import java.io.Serializable; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import javax.annotation.concurrent.Immutable; | ||
|
||
/** | ||
* JVM whole dns cache info, including negative cache. | ||
* | ||
* @author Jerry Lee (oldratlee at gmail dot com) | ||
* @see DnsCacheEntry | ||
* @since 1.2.0 | ||
*/ | ||
@Immutable | ||
public class DnsCache implements Serializable { | ||
private static final long serialVersionUID = -8614746635950970028L; | ||
|
||
final List<DnsCacheEntry> cache; | ||
final List<DnsCacheEntry> negativeCache; | ||
|
||
public DnsCache(List<DnsCacheEntry> cache, List<DnsCacheEntry> negativeCache) { | ||
this.cache = cache; | ||
this.negativeCache = negativeCache; | ||
} | ||
|
||
public List<DnsCacheEntry> getCache() { | ||
// defensive copy | ||
return new ArrayList<DnsCacheEntry>(cache); | ||
} | ||
|
||
public List<DnsCacheEntry> getNegativeCache() { | ||
// defensive copy | ||
return new ArrayList<DnsCacheEntry>(negativeCache); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "DnsCache{" + | ||
"cache=" + cache + | ||
", negativeCache=" + negativeCache + | ||
'}'; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
|
||
DnsCache dnsCache = (DnsCache) o; | ||
|
||
if (cache != null ? !cache.equals(dnsCache.cache) : dnsCache.cache != null) | ||
return false; | ||
return !(negativeCache != null ? !negativeCache.equals(dnsCache.negativeCache) : dnsCache.negativeCache != null); | ||
|
||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
int result = cache != null ? cache.hashCode() : 0; | ||
result = 31 * result + (negativeCache != null ? negativeCache.hashCode() : 0); | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.alibaba.dcm; | ||
|
||
import org.junit.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Date; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* @author Jerry Lee (oldratlee at gmail dot com) | ||
*/ | ||
public class DnsCacheTest { | ||
@Test | ||
public void test_equals() throws Exception { | ||
final Date expiration = new Date(); | ||
DnsCacheEntry entry1 = new DnsCacheEntry("a.com", new String[]{"1.1.1.1"}, expiration); | ||
DnsCacheEntry entry2 = new DnsCacheEntry("b.com", new String[]{"1.1.1.2"}, expiration); | ||
DnsCacheEntry entry3 = new DnsCacheEntry("c.com", new String[]{"1.1.1.2"}, expiration); | ||
DnsCacheEntry entry4 = new DnsCacheEntry("d.com", new String[]{"1.1.1.2"}, expiration); | ||
|
||
DnsCache dnsCache1 = new DnsCache( | ||
Arrays.asList(entry1, entry2), | ||
Arrays.asList(entry3)); | ||
DnsCache dnsCache2 = new DnsCache( | ||
Arrays.asList(entry1, entry2), | ||
Arrays.asList(entry4)); | ||
|
||
assertNotEquals(dnsCache1, dnsCache2); | ||
} | ||
|
||
@Test | ||
public void test_toString() throws Exception { | ||
final Date expiration = new Date(); | ||
DnsCacheEntry entry1 = new DnsCacheEntry("a.com", new String[]{"1.1.1.1"}, expiration); | ||
DnsCacheEntry entry2 = new DnsCacheEntry("b.com", new String[]{"1.1.1.2"}, expiration); | ||
DnsCacheEntry entry3 = new DnsCacheEntry("c.com", new String[]{"1.1.1.2"}, expiration); | ||
|
||
DnsCache dnsCache = new DnsCache( | ||
Arrays.asList(entry1, entry2), | ||
Arrays.asList(entry3)); | ||
|
||
String expected = String.format("DnsCache{cache=[DnsCacheEntry{host='a.com', ips=[1.1.1.1], expiration=%s}" + | ||
", DnsCacheEntry{host='b.com', ips=[1.1.1.2], expiration=%<s}]" + | ||
", negativeCache=[DnsCacheEntry{host='c.com', ips=[1.1.1.2], expiration=%<s}]}", expiration); | ||
|
||
assertEquals(expected, dnsCache.toString()); | ||
} | ||
} |