Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.help > #2734
| From | Jeff Higgins <jeff@invalid.invalid> |
|---|---|
| Newsgroups | comp.lang.java.help |
| Subject | Re: using TreeMap how to return the Object ? |
| Date | 2013-07-01 10:49 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <kqs4gn$s8l$1@dont-email.me> (permalink) |
| References | <f11f9858-f870-42af-a3a3-e9fdb945ba41@googlegroups.com> <kqhkq6$k8g$1@dont-email.me> |
On 06/27/2013 11:19 AM, Jeff Higgins wrote:
> On 06/27/2013 01:30 AM, moonhkt wrote:
>> public void process_hosts () {
>> File ihost = new File(ifn);
>> String delimscfg = "\\s+"; /** multi-space */
>> String aline ;
>> int i = 0 ;
>> try {
>> BufferedReader br1 = new BufferedReader(new FileReader(ihost));
>> while ((aline = br1.readLine()) != null) {
>> String [] tokens = aline.split(delimscfg);
>> try {
>> if ( ! tokens[0].trim().startsWith("#")) {
>> /* System.out.print("-->");
>> System.out.print(tokens[0].trim());
>> System.out.print(" ");
>> System.out.print(tokens[0].trim().substring(0));
>> System.out.println("<--"); */
>
> You'll want a (probably immutable) java.util.Comparable key,
> or a java.util.Comparator for your key in a java.util.SortedMap.
>
> Why store your key with your value?
>
>> ipx.put(tokens[0].trim(),new
>> eleHost(tokens[1].trim(),"x"));
>> }
>> } catch (ArrayIndexOutOfBoundsException e) {
>> continue;
>> }
>> }
>> } catch (IOException e) {
>> System.out.println(e);
>> }
>> }
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.util.Arrays;
import sun.net.util.IPAddressUtil;
/**
* <p>This class represents an Internet Protocol Version 6 (IPv6)
address.</p>
* <p>An instance of an IPv6Address consists of an raw IPv6 address
* (a sixteen element byte array) and has no knowledge of the internal
* (IPv6) structure of the raw IPv6 address.</p>
* <p>This class is intended to demonstrate concepts discussed in a
usenet posting.</p>
* <p><strong>FOR TESTING PURPOSES ONLY -- NOT FOR PRODUCTION
USE</strong></p>
* @author jeff
*
*/
public final class IPv6Address implements Comparable<IPv6Address> {
/**
* <p>Creates an IPv6Address based on the provided IPv6 raw address.</p>
* <p>If address is null, or other than sixteen bytes in length,
* the IPv6 unspecified address is returned.</p>
* <p>Possible to introduce an IPv6 mapped or compatible IPv4 address
here.</p>
*
* @param address a sixteen element byte array (raw IPv6 address)
* @return an IPv6Address object created from the raw IP address,
* or the IPv6 unspecified address if address is null or other than
sixteen bytes in length
*/
public static IPv6Address valueOf(byte[] address) {
return new IPv6Address(address);
}
/**
* <p>Creates an IPv6Address based on the provided IPv6 address
textual representation.</p>
* <p>If address is null, or otherwise invalid,
* the IPv6 unspecified address is returned.</p>
* <p>IPv6 mapped or compatible IPv4 address are considered invalid
here.</p>
*
* @param text representation of an IPv6 address
* @return an IPv6Address object created from the IPv6 address text,
* or the IPv6 unspecified address if address is null or invalid per
* sun.net.util.IPAddressUtil.textToNumericFormatV6(address)
*/
public static IPv6Address valueOf(String address) {
return new IPv6Address(address);
}
/**
* The IPv6 raw address stored by this class is in network byte order,
* the MSB is at index 0 and the LSB is at index 15.</p>
*
* @see java.lang.Comparable#compareTo(java.lang.Object)
*/
@Override
public int compareTo(IPv6Address address) {
int comparison = 0;
for (int i = 0; i < bytes.length; i++) {
comparison = Byte.compare(bytes[i], address.bytes[i]);
if (comparison != 0)
break;
}
return comparison;
}
/*
* (non-Javadoc)
*
* IDE generated equals method
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
IPv6Address other = (IPv6Address) obj;
if (!Arrays.equals(bytes, other.bytes))
return false;
return true;
}
/*
* (non-Javadoc)
*
* IDE generated hashCode method
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + Arrays.hashCode(bytes);
return result;
}
/**
* <p>Converts this IP address to a String. The string returned
* is of the IPv6 "preferred" form:</p>
* <p>RFC2373 Section 2.2 Text Representation of Addresses
* <p>1. The preferred form is x:x:x:x:x:x:x:x, where the 'x's are the
* hexadecimal values of the eight 16-bit pieces of the address.
* <br>Examples:</p>
* <pre>
FEDC:BA98:7654:3210:FEDC:BA98:7654:3210
1080:0:0:0:8:800:200C:417A
* </pre>
*
* @return a string representation of this IP address
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
CharBuffer buffer = ByteBuffer.wrap(bytes).asCharBuffer();
while (buffer.hasRemaining()) {
builder.append(Integer.toHexString(buffer.get()));
if (buffer.hasRemaining())
builder.append(":");
}
return builder.toString();
}
private final byte[] bytes;
private IPv6Address(byte[] address) {
bytes = new byte[16];
if (address != null && address.length == 16)
System.arraycopy(address, 0, bytes, 0, 16);
}
private IPv6Address(String address) {
@SuppressWarnings("restriction")
byte[] test = IPAddressUtil.textToNumericFormatV6(address);
bytes = new byte[16];
if (test != null && test.length == 16) {
System.arraycopy(test, 0, bytes, 0, 16);
}
}
}
Back to comp.lang.java.help | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
using TreeMap how to return the Object ? moonhkt <moonhkt@gmail.com> - 2013-06-26 22:30 -0700
Re: using TreeMap how to return the Object ? Jeff Higgins <jeff@invalid.invalid> - 2013-06-27 11:19 -0400
Re: using TreeMap how to return the Object ? Jeff Higgins <jeff@invalid.invalid> - 2013-06-27 13:15 -0400
Re: using TreeMap how to return the Object ? Jeff Higgins <jeff@invalid.invalid> - 2013-07-01 10:49 -0400
Re: using TreeMap how to return the Object ? Lew <lewbloch@gmail.com> - 2013-06-28 10:42 -0700
Re: using TreeMap how to return the Object ? Patricia Shanahan <pats@acm.org> - 2013-06-28 19:13 -0700
Re: using TreeMap how to return the Object ? Gene Wirchenko <genew@telus.net> - 2013-07-01 14:16 -0700
csiph-web