You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openaz.apache.org by co...@apache.org on 2015/05/05 14:23:03 UTC
[05/54] [abbrv] [partial] incubator-openaz git commit: Do an eclipse
based "cleanup" on the project
http://git-wip-us.apache.org/repos/asf/incubator-openaz/blob/38b6d318/openaz-xacml/src/main/java/com/att/research/xacml/std/datatypes/IPAddress.java
----------------------------------------------------------------------
diff --git a/openaz-xacml/src/main/java/com/att/research/xacml/std/datatypes/IPAddress.java b/openaz-xacml/src/main/java/com/att/research/xacml/std/datatypes/IPAddress.java
index 2eccb4f..43b28de 100644
--- a/openaz-xacml/src/main/java/com/att/research/xacml/std/datatypes/IPAddress.java
+++ b/openaz-xacml/src/main/java/com/att/research/xacml/std/datatypes/IPAddress.java
@@ -35,15 +35,15 @@ import java.text.ParseException;
import com.att.research.xacml.api.SemanticString;
/**
- * IPAddress represents either an IPv4 or IPv6 network address with optional (IPv4)masks or (IPv6)prefixes and port range components.
- *
+ * IPAddress represents either an IPv4 or IPv6 network address with optional (IPv4)masks or (IPv6)prefixes and
+ * port range components.
*/
public abstract class IPAddress implements SemanticString {
/**
- * Given a string purporting to represent an <code>IPAddress</code>, try to convert it into an object.
- * The string may represent either an <code>IPv4Address</code> or an <code>IPv6Address</code>,
- * and may include a mask (for IPv4) or a prefix (for IPv6) and may also include a <code>PortRange</code>
+ * Given a string purporting to represent an <code>IPAddress</code>, try to convert it into an object. The
+ * string may represent either an <code>IPv4Address</code> or an <code>IPv6Address</code>, and may include
+ * a mask (for IPv4) or a prefix (for IPv6) and may also include a <code>PortRange</code>
*
* @param addressString
* @return
http://git-wip-us.apache.org/repos/asf/incubator-openaz/blob/38b6d318/openaz-xacml/src/main/java/com/att/research/xacml/std/datatypes/IPv4Address.java
----------------------------------------------------------------------
diff --git a/openaz-xacml/src/main/java/com/att/research/xacml/std/datatypes/IPv4Address.java b/openaz-xacml/src/main/java/com/att/research/xacml/std/datatypes/IPv4Address.java
index d81cc31..ef9c2cd 100644
--- a/openaz-xacml/src/main/java/com/att/research/xacml/std/datatypes/IPv4Address.java
+++ b/openaz-xacml/src/main/java/com/att/research/xacml/std/datatypes/IPv4Address.java
@@ -35,51 +35,57 @@ import java.util.Arrays;
/**
* IPV4Address extends {@link IPAddress} for IPv4 addresses.
- *
*/
public class IPv4Address extends IPAddress {
-
// Future note:
// If we ever need to merge the IPv4 and IPv6 formats, the only issue is the v4 Mask vs the v6 Prefix.
- // They are usually represented differently (4 octets vs. one number), but they are actually equivalent since they are both specifying which bits
- // in the address are defining a set of subnets. The trick is that IPv4 is expected to start with the top-most bit and fill in to the right,
- // and the prefix says how many of those bits have been set to 1. Typical examples:
- // 255.0.0.0 = 8
- // 255.255.0.0 = 16
- // 255.255.255.0 = 24
- // Thus it is possible to hold the addressMask for IPv4 internally as a single number and translate its representation to/from the octet version
- // as input string is parsed and output string generated, which might allow for common processing between IPv4 and IPv6.
-
- private short[] addressBytes;
- private short[] addressMask;
- private PortRange portRange;
+ // They are usually represented differently (4 octets vs. one number), but they are actually equivalent
+ // since they are both specifying which bits
+ // in the address are defining a set of subnets. The trick is that IPv4 is expected to start with the
+ // top-most bit and fill in to the right,
+ // and the prefix says how many of those bits have been set to 1. Typical examples:
+ // 255.0.0.0 = 8
+ // 255.255.0.0 = 16
+ // 255.255.255.0 = 24
+ // Thus it is possible to hold the addressMask for IPv4 internally as a single number and translate its
+ // representation to/from the octet version
+ // as input string is parsed and output string generated, which might allow for common processing between
+ // IPv4 and IPv6.
+
+ private short[] addressBytes;
+ private short[] addressMask;
+ private PortRange portRange;
public IPv4Address(short[] addressBytesIn, short[] addressMaskIn, PortRange portRangeIn) {
addressBytes = addressBytesIn;
addressMask = addressMaskIn;
portRange = portRangeIn;
if (addressBytesIn.length != 4) {
- throw new IllegalArgumentException("Invalid byte count for IPv4 address: " + addressBytesIn.length);
+ throw new IllegalArgumentException("Invalid byte count for IPv4 address: "
+ + addressBytesIn.length);
}
}
protected static short[] getAddress(String ipv4AddressString) throws ParseException {
- String[] addressParts = ipv4AddressString.split("[.]",-1);
+ String[] addressParts = ipv4AddressString.split("[.]", -1);
if (addressParts == null || addressParts.length != 4) {
- throw new ParseException("Invalid IPv4 address string \"" + ipv4AddressString + "\": invalid address", 0);
+ throw new ParseException("Invalid IPv4 address string \"" + ipv4AddressString
+ + "\": invalid address", 0);
}
- short[] addressBytes = new short[4];
- for (int i = 0 ; i < 4 ; i++) {
+ short[] addressBytes = new short[4];
+ for (int i = 0; i < 4; i++) {
try {
- int octet = Integer.parseInt(addressParts[i]);
+ int octet = Integer.parseInt(addressParts[i]);
if (octet < 0 || octet > 255) {
- throw new ParseException("Invalid IPv4 address string \"" + ipv4AddressString + "\": invalid octet: \"" + addressParts[i], 0);
+ throw new ParseException("Invalid IPv4 address string \"" + ipv4AddressString
+ + "\": invalid octet: \"" + addressParts[i], 0);
} else {
- addressBytes[i] = (short)octet;
+ addressBytes[i] = (short)octet;
}
} catch (NumberFormatException ex) {
- throw new ParseException("Invalid IPv4 address string \"" + ipv4AddressString + "\": invalid octet: \"" + addressParts[i], 0);
+ throw new ParseException("Invalid IPv4 address string \"" + ipv4AddressString
+ + "\": invalid octet: \"" + addressParts[i], 0);
}
}
return addressBytes;
@@ -96,45 +102,43 @@ public class IPv4Address extends IPAddress {
if (ipv4AddressString == null || ipv4AddressString.length() == 0) {
return null;
}
- int slashPos = ipv4AddressString.indexOf('/');
- int colonPos = ipv4AddressString.indexOf(':');
+ int slashPos = ipv4AddressString.indexOf('/');
+ int colonPos = ipv4AddressString.indexOf(':');
if ((colonPos >= 0) && (colonPos < slashPos)) {
- throw new ParseException("Invalid IPv4 address string \"" + ipv4AddressString + "\": out of order components", colonPos);
+ throw new ParseException("Invalid IPv4 address string \"" + ipv4AddressString
+ + "\": out of order components", colonPos);
}
- int endAddress = (slashPos >= 0 ? slashPos : (colonPos >= 0 ? colonPos : ipv4AddressString.length() ));
+ int endAddress = (slashPos >= 0 ? slashPos : (colonPos >= 0 ? colonPos : ipv4AddressString.length()));
if (endAddress < 7) {
- throw new ParseException("Invalid IPv4 address string \"" + ipv4AddressString + "\": address too short", 0);
+ throw new ParseException("Invalid IPv4 address string \"" + ipv4AddressString
+ + "\": address too short", 0);
}
- short[] addressBytes = getAddress(ipv4AddressString.substring(0, endAddress));
- short[] maskBytes = null;
+ short[] addressBytes = getAddress(ipv4AddressString.substring(0, endAddress));
+ short[] maskBytes = null;
if (slashPos >= 0) {
int endMask = (colonPos >= 0 ? colonPos : ipv4AddressString.length());
- maskBytes = getAddress(ipv4AddressString.substring(slashPos+1, endMask));
+ maskBytes = getAddress(ipv4AddressString.substring(slashPos + 1, endMask));
}
- PortRange portRange = null;
+ PortRange portRange = null;
if (colonPos >= 0) {
- if (ipv4AddressString.substring(colonPos+1).length() < 1) {
- throw new ParseException("Invalid IPv4 address string \"" + ipv4AddressString + "\": no portrange given after ':'", colonPos+1);
+ if (ipv4AddressString.substring(colonPos + 1).length() < 1) {
+ throw new ParseException("Invalid IPv4 address string \"" + ipv4AddressString
+ + "\": no portrange given after ':'", colonPos + 1);
}
- portRange = PortRange.newInstance(ipv4AddressString.substring(colonPos+1));
+ portRange = PortRange.newInstance(ipv4AddressString.substring(colonPos + 1));
}
return new IPv4Address(addressBytes, maskBytes, portRange);
}
-
-
-
-
/**
- * Guess whether or not the given string is intended to be an IPv4 address.
- * Because there may be all kinds of errors in the representation, the only way to know that it is a legal IPv4 address is to fully parse it,
- * and if the parse fails then we only know that is is not a legal IPv4 address (it might be a legal IPv6 or other address).
- *
- * We could just have the IPAddress.newInstance method call the full parser and do some quick checks there.
- * The disadvantage of that is that we would need to set up a try/catch (expensive) and in many cases there would be unneeded method calls involved.
- * So we take a guess here.
+ * Guess whether or not the given string is intended to be an IPv4 address. Because there may be all kinds
+ * of errors in the representation, the only way to know that it is a legal IPv4 address is to fully parse
+ * it, and if the parse fails then we only know that is is not a legal IPv4 address (it might be a legal
+ * IPv6 or other address). We could just have the IPAddress.newInstance method call the full parser and do
+ * some quick checks there. The disadvantage of that is that we would need to set up a try/catch
+ * (expensive) and in many cases there would be unneeded method calls involved. So we take a guess here.
*
* @param ipv4AddressString
* @return
@@ -144,8 +148,8 @@ public class IPv4Address extends IPAddress {
return false;
} else {
// V4 addresses contain dots between multiple groups and at most one colon (may be none)
- if (ipv4AddressString.indexOf('.') < ipv4AddressString.lastIndexOf('.') &&
- ipv4AddressString.indexOf(':') == ipv4AddressString.lastIndexOf(':') ) {
+ if (ipv4AddressString.indexOf('.') < ipv4AddressString.lastIndexOf('.')
+ && ipv4AddressString.indexOf(':') == ipv4AddressString.lastIndexOf(':')) {
return true;
} else {
return false;
@@ -153,10 +157,9 @@ public class IPv4Address extends IPAddress {
}
}
-
@Override
public String stringValue() {
- StringBuilder stringBuilder = new StringBuilder();
+ StringBuilder stringBuilder = new StringBuilder();
if (addressBytes != null && addressBytes.length > 0) {
stringBuilder.append(formatAddress(addressBytes));
@@ -175,13 +178,11 @@ public class IPv4Address extends IPAddress {
return stringBuilder.toString();
}
-
@Override
public String toString() {
return stringValue();
}
-
@Override
public boolean equals(Object obj) {
// for the moment assume that different IP formats can never be equal
@@ -190,10 +191,10 @@ public class IPv4Address extends IPAddress {
} else if (obj == this) {
return true;
} else {
- IPv4Address ipAddress = (IPv4Address)obj;
+ IPv4Address ipAddress = (IPv4Address)obj;
- short[] bytesThis = this.addressBytes;
- short[] bytesThat = ipAddress.addressBytes;
+ short[] bytesThis = this.addressBytes;
+ short[] bytesThat = ipAddress.addressBytes;
if (bytesThis == null) {
if (bytesThat != null) {
return false;
@@ -204,8 +205,8 @@ public class IPv4Address extends IPAddress {
}
}
- bytesThis = this.addressMask;
- bytesThat = ipAddress.addressMask;
+ bytesThis = this.addressMask;
+ bytesThat = ipAddress.addressMask;
if (bytesThis == null) {
if (bytesThat != null) {
return false;
http://git-wip-us.apache.org/repos/asf/incubator-openaz/blob/38b6d318/openaz-xacml/src/main/java/com/att/research/xacml/std/datatypes/IPv6Address.java
----------------------------------------------------------------------
diff --git a/openaz-xacml/src/main/java/com/att/research/xacml/std/datatypes/IPv6Address.java b/openaz-xacml/src/main/java/com/att/research/xacml/std/datatypes/IPv6Address.java
index a070dd6..f66510c 100644
--- a/openaz-xacml/src/main/java/com/att/research/xacml/std/datatypes/IPv6Address.java
+++ b/openaz-xacml/src/main/java/com/att/research/xacml/std/datatypes/IPv6Address.java
@@ -35,39 +35,42 @@ import java.util.Arrays;
/**
* IPV4Address extends {@link IPAddress} for IPv4 addresses.
- *
*/
public class IPv6Address extends IPAddress {
- private static final short[] NULL_ADDRESS = { 0, 0, 0, 0, 0, 0, 0, 0 };
-
-
+ private static final short[] NULL_ADDRESS = {
+ 0, 0, 0, 0, 0, 0, 0, 0
+ };
// Future note:
// If we ever need to merge the IPv4 and IPv6 formats, the only issue is the v4 Mask vs the v6 Prefix.
- // They are usually represented differently (4 octets vs. one number), but they are actually equivalent since they are both specifying which bits
- // in the address are defining a set of subnets. The trick is that IPv4 is expected to start with the top-most bit and fill in to the right,
- // and the prefix says how many of those bits have been set to 1. Typical examples:
- // 255.0.0.0 = 8
- // 255.255.0.0 = 16
- // 255.255.255.0 = 24
- // Thus it is possible to hold the addressMask for IPv4 internally as a single number (as IPv6 does) and translate its representation to/from the octet version
- // as input string is parsed and output string generated, which might allow for common processing between IPv4 and IPv6.
-
- private short[] addressBytes;
- private Short prefix;
- private PortRange portRange;
+ // They are usually represented differently (4 octets vs. one number), but they are actually equivalent
+ // since they are both specifying which bits
+ // in the address are defining a set of subnets. The trick is that IPv4 is expected to start with the
+ // top-most bit and fill in to the right,
+ // and the prefix says how many of those bits have been set to 1. Typical examples:
+ // 255.0.0.0 = 8
+ // 255.255.0.0 = 16
+ // 255.255.255.0 = 24
+ // Thus it is possible to hold the addressMask for IPv4 internally as a single number (as IPv6 does) and
+ // translate its representation to/from the octet version
+ // as input string is parsed and output string generated, which might allow for common processing between
+ // IPv4 and IPv6.
+
+ private short[] addressBytes;
+ private Short prefix;
+ private PortRange portRange;
public IPv6Address(short[] addressBytesIn, Short prefixIn, PortRange portRangeIn) {
addressBytes = addressBytesIn;
prefix = prefixIn;
portRange = portRangeIn;
if (addressBytesIn.length != 8) {
- throw new IllegalArgumentException("Invalid byte count for IPv6 address: " + addressBytesIn.length);
+ throw new IllegalArgumentException("Invalid byte count for IPv6 address: "
+ + addressBytesIn.length);
}
}
-
/**
* Is this address really an IPv4 address in IPv6 format?
*
@@ -78,7 +81,7 @@ public class IPv6Address extends IPAddress {
if (addressBytes == null || addressBytes.length < 8) {
return false;
}
- for (int i = 0 ; i < 5 ; i++) {
+ for (int i = 0; i < 5; i++) {
if (addressBytes[i] != 0) {
return false;
}
@@ -86,24 +89,23 @@ public class IPv6Address extends IPAddress {
return (addressBytes[5] == (short)0xFFFF);
}
-
/**
- * Generate a string representing just the address part without the [], prefix or portrange.
- * This accounts for the case of an IPv4-form address being represented in IPv6 format.
+ * Generate a string representing just the address part without the [], prefix or portrange. This accounts
+ * for the case of an IPv4-form address being represented in IPv6 format.
*
* @param addressParts
* @return
*/
public static String formatAddress(short[] addressParts) {
- StringBuilder stringBuilder = new StringBuilder();
+ StringBuilder stringBuilder = new StringBuilder();
if (isIPv4Address(addressParts)) {
stringBuilder.append("::FFFF:");
- short[] ipv4Octets = new short[4];
- ipv4Octets[0] = (short)((addressParts[6] >> 8) & 0xFF);
- ipv4Octets[1] = (short)(addressParts[6] & 0xFF);
- ipv4Octets[2] = (short)((addressParts[7] >> 8) & 0xFF);
- ipv4Octets[3] = (short)(addressParts[7] & 0xFF);
+ short[] ipv4Octets = new short[4];
+ ipv4Octets[0] = (short)((addressParts[6] >> 8) & 0xFF);
+ ipv4Octets[1] = (short)(addressParts[6] & 0xFF);
+ ipv4Octets[2] = (short)((addressParts[7] >> 8) & 0xFF);
+ ipv4Octets[3] = (short)(addressParts[7] & 0xFF);
stringBuilder.append(IPv4Address.formatAddress(ipv4Octets));
} else {
/*
@@ -111,15 +113,16 @@ public class IPv6Address extends IPAddress {
*/
int zeroPos = -1;
int zeroLen = 0;
- for (int i = 0 ; i < addressParts.length ; ) {
+ for (int i = 0; i < addressParts.length;) {
if (addressParts[i] == 0) {
- int zeroCount = 1;
- for (int j = i+1 ; j < addressParts.length && addressParts[j] == 0 ; j++, zeroCount++);
+ int zeroCount = 1;
+ for (int j = i + 1; j < addressParts.length && addressParts[j] == 0; j++, zeroCount++)
+ ;
if (zeroCount > zeroLen) {
zeroPos = i;
zeroLen = zeroCount;
}
- i += zeroCount;
+ i += zeroCount;
} else {
i++;
}
@@ -128,14 +131,14 @@ public class IPv6Address extends IPAddress {
/*
* Start formatting the output
*/
- for (int i = 0 ; i < addressParts.length ; ) {
+ for (int i = 0; i < addressParts.length;) {
if (addressParts[i] == 0) {
if (i == zeroPos) {
if (i == 0) {
stringBuilder.append(':');
}
stringBuilder.append(':');
- i += zeroLen;
+ i += zeroLen;
} else {
stringBuilder.append('0');
i++;
@@ -156,13 +159,10 @@ public class IPv6Address extends IPAddress {
return stringBuilder.toString();
}
-
-
-
/**
- * Parses a string that can represent an IPv6 address into an array of 8 shorts representing
- * the 8 16-bit components of the address. Allows for the IPv4 equivalence address: ::FFFF:w.x.y.z
- * where the 32 bits of the IPv4 address are encoded into the lower two shorts of the IPv6 address
+ * Parses a string that can represent an IPv6 address into an array of 8 shorts representing the 8 16-bit
+ * components of the address. Allows for the IPv4 equivalence address: ::FFFF:w.x.y.z where the 32 bits of
+ * the IPv4 address are encoded into the lower two shorts of the IPv6 address
*
* @param ipv6Address
* @return
@@ -173,7 +173,7 @@ public class IPv6Address extends IPAddress {
return NULL_ADDRESS;
}
- String parseString = ipv6Address;
+ String parseString = ipv6Address;
if (ipv6Address.startsWith("::")) {
/*
* Skip over the first ':' of the '::' to avoid getting two null fields in a row at the beginning
@@ -183,83 +183,84 @@ public class IPv6Address extends IPAddress {
/*
* Skip over the last ':' of the '::' to avoid getting two null fields in a row at the end
*/
- parseString = ipv6Address.substring(0, ipv6Address.length()-1);
+ parseString = ipv6Address.substring(0, ipv6Address.length() - 1);
}
- String[] addressFields = parseString.split("[:]",-1);
+ String[] addressFields = parseString.split("[:]", -1);
if (addressFields == null || addressFields.length == 0 || addressFields.length > 8) {
throw new ParseException("Invalid IPv6Address string \"" + ipv6Address + "\"", 0);
}
- boolean isIPv4 = false;
- if (addressFields.length == 3 && addressFields[0].length() == 0 && addressFields[1].equalsIgnoreCase("FFFF") && addressFields[2].contains(".")) {
- isIPv4 = true;
+ boolean isIPv4 = false;
+ if (addressFields.length == 3 && addressFields[0].length() == 0
+ && addressFields[1].equalsIgnoreCase("FFFF") && addressFields[2].contains(".")) {
+ isIPv4 = true;
}
- short[] addressShorts = new short[8];
- int nAddressShorts = 0;
- int missingFields = (isIPv4 ? 4 : 8 - addressFields.length);
- boolean sawMissingField = false;
+ short[] addressShorts = new short[8];
+ int nAddressShorts = 0;
+ int missingFields = (isIPv4 ? 4 : 8 - addressFields.length);
+ boolean sawMissingField = false;
- for (int i = 0 ; i < addressFields.length ; i++) {
+ for (int i = 0; i < addressFields.length; i++) {
if (addressFields[i].length() == 0) {
/*
* An empty field indicates a consecutive string of zero values.
*/
if (sawMissingField) {
- throw new ParseException("Invalid IPv6Address string \"" + ipv6Address + "\": multiple zero runs", i);
+ throw new ParseException("Invalid IPv6Address string \"" + ipv6Address
+ + "\": multiple zero runs", i);
}
sawMissingField = true;
addressShorts[nAddressShorts++] = 0;
- for (int j = 0 ; j < missingFields ; j++) {
- addressShorts[nAddressShorts++] = 0;
+ for (int j = 0; j < missingFields; j++) {
+ addressShorts[nAddressShorts++] = 0;
}
} else if (addressFields[i].indexOf('.') >= 0) {
if (nAddressShorts != 6) {
- throw new ParseException("Invalid IPv6Address string \"" + ipv6Address + "\": misplaced IPv4 address", i);
+ throw new ParseException("Invalid IPv6Address string \"" + ipv6Address
+ + "\": misplaced IPv4 address", i);
}
/*
* IPv4 address
*/
- short[] ipv4Octets = null;
+ short[] ipv4Octets = null;
try {
- ipv4Octets = IPv4Address.getAddress(addressFields[i]);
+ ipv4Octets = IPv4Address.getAddress(addressFields[i]);
} catch (ParseException ex) {
- throw new ParseException("Invalid IPv4Address in Ipv6Address \"" + addressFields[i] + "\"", i);
+ throw new ParseException("Invalid IPv4Address in Ipv6Address \"" + addressFields[i]
+ + "\"", i);
}
if (ipv4Octets == null) {
- throw new ParseException("Invalid Ipv4Address in IPv6Address \"" + addressFields[i] + "\"", i);
+ throw new ParseException("Invalid Ipv4Address in IPv6Address \"" + addressFields[i]
+ + "\"", i);
}
- assert(ipv4Octets.length == 4);
+ assert (ipv4Octets.length == 4);
addressShorts[nAddressShorts++] = (short)(ipv4Octets[0] * 256 + ipv4Octets[1]);
addressShorts[nAddressShorts++] = (short)(ipv4Octets[2] * 256 + ipv4Octets[3]);
} else {
try {
- addressShorts[nAddressShorts++] = (short)(Integer.parseInt(addressFields[i], 16));
+ addressShorts[nAddressShorts++] = (short)(Integer.parseInt(addressFields[i], 16));
} catch (NumberFormatException ex) {
- throw new ParseException("Invalid IPv6Address component \"" + addressFields[i] + "\": invalid hex", i);
+ throw new ParseException("Invalid IPv6Address component \"" + addressFields[i]
+ + "\": invalid hex", i);
}
}
}
if (nAddressShorts < 8) {
- throw new ParseException("Invalid IPv6Address string \"" + ipv6Address + "\": not enough address fields", 0);
+ throw new ParseException("Invalid IPv6Address string \"" + ipv6Address
+ + "\": not enough address fields", 0);
}
return addressShorts;
}
-
-
-
/**
- * Specs not clear on format of a "prefix". It could be either
- * "[" <address> [ "/" <prefix> ] "]" [ ":" portrange ]
- * or
- * "[" <address> "]" [ "/" <prefix> ] [ ":" portrange ]
- * but we cannot tell which the specs are saying.
- * To avoid problems in the future it seems safest to implement both of these formats.
+ * Specs not clear on format of a "prefix". It could be either "[" <address> [ "/" <prefix> ] "]" [ ":"
+ * portrange ] or "[" <address> "]" [ "/" <prefix> ] [ ":" portrange ] but we cannot tell which the specs
+ * are saying. To avoid problems in the future it seems safest to implement both of these formats.
*
* @param ipv6AddressString
* @return
@@ -270,49 +271,51 @@ public class IPv6Address extends IPAddress {
if (ipv6AddressString == null || ipv6AddressString.length() == 0) {
return null;
} else if (!ipv6AddressString.startsWith("[")) {
- throw new ParseException("Invalid IPv6Address string \"" + ipv6AddressString + "\": missing opening bracket", 0);
+ throw new ParseException("Invalid IPv6Address string \"" + ipv6AddressString
+ + "\": missing opening bracket", 0);
}
/*
* Start with the address
*/
- int addressStart = 1;
- int addressEnd = ipv6AddressString.indexOf(']', addressStart);
+ int addressStart = 1;
+ int addressEnd = ipv6AddressString.indexOf(']', addressStart);
if (addressEnd < 0) {
- throw new ParseException("Invalid IPv6Address string \"" + ipv6AddressString + "\": missing closing bracket", 0);
+ throw new ParseException("Invalid IPv6Address string \"" + ipv6AddressString
+ + "\": missing closing bracket", 0);
} else if ((addressEnd - addressStart) < 2) {
- throw new ParseException("Invalid IPv6Address string \"" + ipv6AddressString + "\": empty address", 0);
+ throw new ParseException("Invalid IPv6Address string \"" + ipv6AddressString
+ + "\": empty address", 0);
}
// If address has prefix inside the brackets, adjust the end
int slashIndex = ipv6AddressString.indexOf("/");
if (slashIndex > -1 && slashIndex < ipv6AddressString.indexOf("]")) {
// do not include the prefix in the address
- addressEnd = slashIndex ;
+ addressEnd = slashIndex;
}
- short[] addressShorts = getAddress(ipv6AddressString.substring(addressStart, addressEnd));
- int nextPos = addressEnd;
+ short[] addressShorts = getAddress(ipv6AddressString.substring(addressStart, addressEnd));
+ int nextPos = addressEnd;
/*
- * See if there is a slash for the prefix
- * The four cases we are looking at are:
- * ]
- * ]/<prefix>
- * /<prefix>]
- * all of which may be followed by ":<portrange>"
+ * See if there is a slash for the prefix The four cases we are looking at are: ] ]/<prefix>
+ * /<prefix>] all of which may be followed by ":<portrange>"
*/
// adjust starting point for case of ]/<prefix>
Short prefix = null;
- if (nextPos < ipv6AddressString.length() - 1 && ipv6AddressString.charAt(nextPos) == ']' && ipv6AddressString.charAt(nextPos + 1) == '/') {
+ if (nextPos < ipv6AddressString.length() - 1 && ipv6AddressString.charAt(nextPos) == ']'
+ && ipv6AddressString.charAt(nextPos + 1) == '/') {
nextPos++;
}
if (nextPos < ipv6AddressString.length() && ipv6AddressString.charAt(nextPos) == '/') {
nextPos++;
- if (nextPos >= ipv6AddressString.length() || ipv6AddressString.charAt(nextPos) == ']' || ipv6AddressString.charAt(nextPos) == ':') {
- throw new ParseException("Invalid Ipv6Address string \"" + ipv6AddressString + "\": prefix designation without value", nextPos);
+ if (nextPos >= ipv6AddressString.length() || ipv6AddressString.charAt(nextPos) == ']'
+ || ipv6AddressString.charAt(nextPos) == ':') {
+ throw new ParseException("Invalid Ipv6Address string \"" + ipv6AddressString
+ + "\": prefix designation without value", nextPos);
}
- addressStart = nextPos;
- addressEnd = ipv6AddressString.indexOf(']', addressStart);
+ addressStart = nextPos;
+ addressEnd = ipv6AddressString.indexOf(']', addressStart);
if (addressEnd < 0) {
// looking at ]/<prefix> or ]/<prefix>:<portRange>
@@ -327,12 +330,13 @@ public class IPv6Address extends IPAddress {
// prefix cannot be larger than 128 (and really should not be 128!)
if (prefix > 128) {
- throw new ParseException("Invalid Ipv6Address string \"" + ipv6AddressString + "\": prefix is larger than 128", nextPos);
+ throw new ParseException("Invalid Ipv6Address string \"" + ipv6AddressString
+ + "\": prefix is larger than 128", nextPos);
}
- nextPos = addressEnd;
+ nextPos = addressEnd;
}
// adjust for case of
- // /<prefix>]
+ // /<prefix>]
if (nextPos < ipv6AddressString.length() && ipv6AddressString.charAt(nextPos) == ']') {
nextPos++;
}
@@ -340,34 +344,32 @@ public class IPv6Address extends IPAddress {
/*
* See if there is a ':' for the port range
*/
- PortRange portRange = null;
+ PortRange portRange = null;
if (nextPos < ipv6AddressString.length() && ipv6AddressString.charAt(nextPos) == ':') {
- if (ipv6AddressString.substring(nextPos+1).length() < 1) {
- throw new ParseException("Invalid IPv6 address string \"" + ipv6AddressString + "\": no portrange given after ':'", nextPos+1);
+ if (ipv6AddressString.substring(nextPos + 1).length() < 1) {
+ throw new ParseException("Invalid IPv6 address string \"" + ipv6AddressString
+ + "\": no portrange given after ':'", nextPos + 1);
}
- portRange = PortRange.newInstance(ipv6AddressString.substring(nextPos+1));
- nextPos = ipv6AddressString.length();
+ portRange = PortRange.newInstance(ipv6AddressString.substring(nextPos + 1));
+ nextPos = ipv6AddressString.length();
}
if (nextPos < ipv6AddressString.length()) {
- throw new ParseException("Invalid Ipv6Address string \"" + ipv6AddressString + "\": unknown content at end", nextPos);
+ throw new ParseException("Invalid Ipv6Address string \"" + ipv6AddressString
+ + "\": unknown content at end", nextPos);
}
return new IPv6Address(addressShorts, prefix, portRange);
}
-
-
-
/**
- * Guess whether or not the given string is intended to be an IPv6 address.
- * Because there may be all kinds of errors in the representation, the only way to know that it is a legal IPv6 address is to fully parse it,
- * and if the parse fails then we only know that is is not a legal IPv6 address (it might be a legal IPv4 or other address).
- *
- * We could just have the IPAddress.newInstance method call the full parser and do some quick checks there.
- * The disadvantage of that is that we would need to set up a try/catch (expensive) and in many cases there would be unneeded method calls involved.
- * So we take a guess here.
+ * Guess whether or not the given string is intended to be an IPv6 address. Because there may be all kinds
+ * of errors in the representation, the only way to know that it is a legal IPv6 address is to fully parse
+ * it, and if the parse fails then we only know that is is not a legal IPv6 address (it might be a legal
+ * IPv4 or other address). We could just have the IPAddress.newInstance method call the full parser and do
+ * some quick checks there. The disadvantage of that is that we would need to set up a try/catch
+ * (expensive) and in many cases there would be unneeded method calls involved. So we take a guess here.
*
* @param ipv4AddressString
* @return
@@ -377,8 +379,8 @@ public class IPv6Address extends IPAddress {
return false;
} else {
// V6 addresses should contain no dots and multiple colons
- if (ipv4AddressString.indexOf(':') < ipv4AddressString.lastIndexOf(':') &&
- ipv4AddressString.indexOf('.') == ipv4AddressString.lastIndexOf('.') ) {
+ if (ipv4AddressString.indexOf(':') < ipv4AddressString.lastIndexOf(':')
+ && ipv4AddressString.indexOf('.') == ipv4AddressString.lastIndexOf('.')) {
return true;
} else {
return false;
@@ -386,21 +388,16 @@ public class IPv6Address extends IPAddress {
}
}
-
/**
- * The specifications are ambiguous on how to handle /prefix in IPv6.
- * There are two possibilities and the documents are silent on how to handle it, and no examples are readily apparent on the internet.
- * The two possible representations are:
- * [<address>/prefix]
- * and
- * [<address>]/prefix
- * The prefix is optional as is the ":<portrange>" that can follow it.
- *
- * We have chosen to use the first representation because the prefix seems to be part of the address itself, which is supposed to be inside the brackets.
+ * The specifications are ambiguous on how to handle /prefix in IPv6. There are two possibilities and the
+ * documents are silent on how to handle it, and no examples are readily apparent on the internet. The two
+ * possible representations are: [<address>/prefix] and [<address>]/prefix The prefix is optional as is
+ * the ":<portrange>" that can follow it. We have chosen to use the first representation because the
+ * prefix seems to be part of the address itself, which is supposed to be inside the brackets.
*/
@Override
public String stringValue() {
- StringBuilder stringBuilder = new StringBuilder("[");
+ StringBuilder stringBuilder = new StringBuilder("[");
if (addressBytes != null) {
stringBuilder.append(formatAddress(addressBytes));
@@ -420,14 +417,11 @@ public class IPv6Address extends IPAddress {
return stringBuilder.toString();
}
-
@Override
public String toString() {
return stringValue();
}
-
-
@Override
public boolean equals(Object obj) {
// for the moment assume that different IP formats can never be equal
@@ -436,10 +430,10 @@ public class IPv6Address extends IPAddress {
} else if (obj == this) {
return true;
} else {
- IPv6Address ipAddress = (IPv6Address)obj;
+ IPv6Address ipAddress = (IPv6Address)obj;
- short[] bytesThis = this.addressBytes;
- short[] bytesThat = ipAddress.addressBytes;
+ short[] bytesThis = this.addressBytes;
+ short[] bytesThat = ipAddress.addressBytes;
if (bytesThis == null) {
if (bytesThat != null) {
return false;
@@ -450,8 +444,8 @@ public class IPv6Address extends IPAddress {
}
}
- Short prefixThis = this.prefix;
- Short prefixThat = ipAddress.prefix;
+ Short prefixThis = this.prefix;
+ Short prefixThat = ipAddress.prefix;
if (prefixThis == null) {
if (prefixThat != null) {
return false;
@@ -479,5 +473,4 @@ public class IPv6Address extends IPAddress {
}
}
-
}
http://git-wip-us.apache.org/repos/asf/incubator-openaz/blob/38b6d318/openaz-xacml/src/main/java/com/att/research/xacml/std/datatypes/ISO8601Date.java
----------------------------------------------------------------------
diff --git a/openaz-xacml/src/main/java/com/att/research/xacml/std/datatypes/ISO8601Date.java b/openaz-xacml/src/main/java/com/att/research/xacml/std/datatypes/ISO8601Date.java
index 3a0ffbb..ae65963 100644
--- a/openaz-xacml/src/main/java/com/att/research/xacml/std/datatypes/ISO8601Date.java
+++ b/openaz-xacml/src/main/java/com/att/research/xacml/std/datatypes/ISO8601Date.java
@@ -39,23 +39,22 @@ import java.util.TimeZone;
import com.att.research.xacml.api.SemanticString;
/**
- * ISO8601Date is a representation of an ISO8601 format date without a time component.
- * NOTE: This is actually the extended specification for XACML3.0, not a strict ISO8601 date
- *
+ * ISO8601Date is a representation of an ISO8601 format date without a time component. NOTE: This is actually
+ * the extended specification for XACML3.0, not a strict ISO8601 date
*/
public class ISO8601Date implements IDateTime<ISO8601Date>, Comparable<ISO8601Date>, SemanticString {
- private ISO8601DateTime dateTime;
+ private ISO8601DateTime dateTime;
protected ISO8601Date(ISO8601DateTime iso8601DateTime) {
- this.dateTime = iso8601DateTime;
+ this.dateTime = iso8601DateTime;
}
public ISO8601Date(String timeZone, int yearIn, int monthIn, int dayIn) {
- this.dateTime = new ISO8601DateTime(timeZone, yearIn, monthIn, dayIn, 0, 0, 0, 0);
+ this.dateTime = new ISO8601DateTime(timeZone, yearIn, monthIn, dayIn, 0, 0, 0, 0);
}
public ISO8601Date(TimeZone timeZone, int yearIn, int monthIn, int dayIn) {
- this.dateTime = new ISO8601DateTime(timeZone, yearIn, monthIn, dayIn, 0, 0, 0, 0);
+ this.dateTime = new ISO8601DateTime(timeZone, yearIn, monthIn, dayIn, 0, 0, 0, 0);
}
public ISO8601Date(ISO8601TimeZone timeZone, int yearIn, int monthIn, int dayIn) {
@@ -69,26 +68,33 @@ public class ISO8601Date implements IDateTime<ISO8601Date>, Comparable<ISO8601Da
public boolean getHasTimeZone() {
return this.dateTime.getHasTimeZone();
}
+
public Calendar getCalendar() {
return this.dateTime.getCalendar();
}
+
public String getTimeZone() {
return this.dateTime.getTimeZone();
}
+
public int getYear() {
return this.dateTime.getYear();
}
+
public int getMonth() {
return this.dateTime.getMonth();
}
+
public int getDay() {
return this.dateTime.getDay();
}
+ @Override
public ISO8601Date add(ISO8601Duration iso8601Duration) {
return new ISO8601Date(this.dateTime.add(iso8601Duration));
}
+ @Override
public ISO8601Date sub(ISO8601Duration iso8601Duration) {
return new ISO8601Date(this.dateTime.sub(iso8601Duration));
}
@@ -105,20 +111,24 @@ public class ISO8601Date implements IDateTime<ISO8601Date>, Comparable<ISO8601Da
} else if (obj == null || !(obj instanceof ISO8601Date)) {
return false;
} else {
- ISO8601Date iso8601Date = (ISO8601Date)obj;
+ ISO8601Date iso8601Date = (ISO8601Date)obj;
return this.dateTime.equals(iso8601Date.dateTime);
}
}
/**
- * Gets the <code>String</code> representation of the ISO8601 date represented by this <code>ISO8601Date</code> object.
+ * Gets the <code>String</code> representation of the ISO8601 date represented by this
+ * <code>ISO8601Date</code> object.
*
- * @param includeTimeZone <code>boolean</code> indicating whether the time zone information should be included
- * @return the <code>String</code> representation of the ISO8601 date represented by this <code>ISO8601Date</code> object.
+ * @param includeTimeZone <code>boolean</code> indicating whether the time zone information should be
+ * included
+ * @return the <code>String</code> representation of the ISO8601 date represented by this
+ * <code>ISO8601Date</code> object.
*/
public String stringValue(boolean includeTimeZone) {
- StringBuilder stringBuilder = new StringBuilder();
- stringBuilder.append(String.format("%d-%02d-%02d", this.dateTime.getYear(), this.dateTime.getMonth(), this.dateTime.getDay()));
+ StringBuilder stringBuilder = new StringBuilder();
+ stringBuilder.append(String.format("%d-%02d-%02d", this.dateTime.getYear(), this.dateTime.getMonth(),
+ this.dateTime.getDay()));
if (includeTimeZone && this.dateTime.getHasTimeZone()) {
stringBuilder.append(this.dateTime.getTimeZone());
}
@@ -143,67 +153,68 @@ public class ISO8601Date implements IDateTime<ISO8601Date>, Comparable<ISO8601Da
public static ISO8601Date fromCalendar(Calendar calendar) {
int year;
if (calendar.get(Calendar.ERA) == GregorianCalendar.BC) {
- year = 1 - calendar.get(Calendar.YEAR);
+ year = 1 - calendar.get(Calendar.YEAR);
} else {
- year = calendar.get(Calendar.YEAR);
+ year = calendar.get(Calendar.YEAR);
}
- return new ISO8601Date(calendar.getTimeZone(), year, calendar.get(Calendar.MONTH)+1, calendar.get(Calendar.DAY_OF_MONTH));
+ return new ISO8601Date(calendar.getTimeZone(), year, calendar.get(Calendar.MONTH) + 1,
+ calendar.get(Calendar.DAY_OF_MONTH));
}
public static ISO8601Date fromDate(Date date) {
- Calendar cal = Calendar.getInstance();
+ Calendar cal = Calendar.getInstance();
cal.setTime(date);
return ISO8601Date.fromCalendar(cal);
}
public static ISO8601Date fromISO8601DateString(String strDate) throws ParseException {
- int startPos = ParseUtils.nextNonWhite(strDate, 0);
+ int startPos = ParseUtils.nextNonWhite(strDate, 0);
- ParseUtils.ParseValue<Integer> parseValue = ParseUtils.getSignedValue(strDate, startPos);
+ ParseUtils.ParseValue<Integer> parseValue = ParseUtils.getSignedValue(strDate, startPos);
if (parseValue == null) {
throw new ParseException("Invalid year", startPos);
}
- int year = parseValue.getValue();
+ int year = parseValue.getValue();
if (parseValue.getNextPos() < 4) {
throw new ParseException("Invalid year (must be at least 4 digits)", startPos);
}
- startPos += parseValue.getNextPos();
+ startPos += parseValue.getNextPos();
if (startPos >= strDate.length() || strDate.charAt(startPos) != '-') {
throw new ParseException("Invalid date", startPos);
}
startPos++;
- int month = ParseUtils.getTwoDigitValue(strDate, startPos);
+ int month = ParseUtils.getTwoDigitValue(strDate, startPos);
if (month < 0 || month > 12) {
throw new ParseException("Invalid month", startPos);
}
- startPos += 2;
+ startPos += 2;
if (startPos >= strDate.length() || strDate.charAt(startPos) != '-') {
throw new ParseException("Invalid date", startPos);
}
startPos++;
- int day = ParseUtils.getTwoDigitValue(strDate, startPos);
+ int day = ParseUtils.getTwoDigitValue(strDate, startPos);
if (day < 1 || day > 31) {
throw new ParseException("Invalid day", startPos);
}
- startPos += 2;
+ startPos += 2;
/*
* Now determine if there is a timezone
*/
- String timezone = null;
+ String timezone = null;
if (startPos < strDate.length()) {
- switch(strDate.charAt(startPos)) {
+ switch (strDate.charAt(startPos)) {
case 'Z':
- timezone = "GMT";
+ timezone = "GMT";
startPos++;
break;
case '-':
case '+':
- if (startPos+5 < strDate.length()) {
- timezone = "GMT" + strDate.substring(startPos, startPos+6);
+ if (startPos + 5 < strDate.length()) {
+ timezone = "GMT" + strDate.substring(startPos, startPos + 6);
} else {
throw new ParseException("Invalid timezone", startPos);
}
http://git-wip-us.apache.org/repos/asf/incubator-openaz/blob/38b6d318/openaz-xacml/src/main/java/com/att/research/xacml/std/datatypes/ISO8601DateTime.java
----------------------------------------------------------------------
diff --git a/openaz-xacml/src/main/java/com/att/research/xacml/std/datatypes/ISO8601DateTime.java b/openaz-xacml/src/main/java/com/att/research/xacml/std/datatypes/ISO8601DateTime.java
index 26a41bd..0f2a9a5 100644
--- a/openaz-xacml/src/main/java/com/att/research/xacml/std/datatypes/ISO8601DateTime.java
+++ b/openaz-xacml/src/main/java/com/att/research/xacml/std/datatypes/ISO8601DateTime.java
@@ -39,22 +39,21 @@ import java.util.TimeZone;
import com.att.research.xacml.api.SemanticString;
/**
- * ISO8601Datetime is a combination of an {@link ISO8601Date} and a {@link ISO8601Time}
- * with a common {@link com.att.research.xacml.std.datatypes.ISO8601TimeZone}.
- *
- * Note: This is a temporary implementation. It appears Java 8 will have better classes for dealing with ISO8601 dates and times.
- *
+ * ISO8601Datetime is a combination of an {@link ISO8601Date} and a {@link ISO8601Time} with a common
+ * {@link com.att.research.xacml.std.datatypes.ISO8601TimeZone}. Note: This is a temporary implementation. It
+ * appears Java 8 will have better classes for dealing with ISO8601 dates and times.
*/
-public class ISO8601DateTime implements IDateTime<ISO8601DateTime>, Comparable<ISO8601DateTime>, SemanticString {
- private static final long TZOFFSET_14_HOURS_MILLIS = 14*60*60*1000;
+public class ISO8601DateTime implements IDateTime<ISO8601DateTime>, Comparable<ISO8601DateTime>,
+ SemanticString {
+ private static final long TZOFFSET_14_HOURS_MILLIS = 14 * 60 * 60 * 1000;
private boolean hasTimeZone;
private Calendar calendar;
private void validateDate() {
this.calendar.getTime();
- int year = this.calendar.get(Calendar.YEAR);
+ int year = this.calendar.get(Calendar.YEAR);
if (this.calendar.isSet(Calendar.ERA) && this.calendar.get(Calendar.ERA) == GregorianCalendar.BC) {
- year = 0 - year + 1;
+ year = 0 - year + 1;
}
}
@@ -62,7 +61,7 @@ public class ISO8601DateTime implements IDateTime<ISO8601DateTime>, Comparable<I
/**
* Creates a new <code>ISO8601DateTime</code> using the supplied field values.
*
- * @param timeZone the <code>String</code> name of the time zone. Use null for non-time-zoned instances
+ * @param timeZone the <code>String</code> name of the time zone. Use null for non-time-zoned instances
* @param yearIn the integer year (-9999 to 9999)
* @param monthIn the integer month (1-12)
* @param dayIn the integer day of the month (1-31)
@@ -71,20 +70,21 @@ public class ISO8601DateTime implements IDateTime<ISO8601DateTime>, Comparable<I
* @param secondIn the integer second of the minute (0-59)
* @param millisecondIn the integer milliseconds (0-999)
*/
- public ISO8601DateTime(TimeZone timeZone, int yearIn, int monthIn, int dayIn, int hourIn, int minuteIn, int secondIn, int millisecondIn) {
- this.calendar = Calendar.getInstance();
+ public ISO8601DateTime(TimeZone timeZone, int yearIn, int monthIn, int dayIn, int hourIn, int minuteIn,
+ int secondIn, int millisecondIn) {
+ this.calendar = Calendar.getInstance();
this.calendar.setLenient(false);
if (timeZone != null) {
- this.hasTimeZone = true;
+ this.hasTimeZone = true;
this.calendar.setTimeZone(timeZone);
}
if (yearIn <= 0) {
this.calendar.set(Calendar.ERA, GregorianCalendar.BC);
- this.calendar.set(Calendar.YEAR, Math.abs(yearIn)+1);
+ this.calendar.set(Calendar.YEAR, Math.abs(yearIn) + 1);
} else {
this.calendar.set(Calendar.YEAR, yearIn);
}
- this.calendar.set(Calendar.MONTH, monthIn-1);
+ this.calendar.set(Calendar.MONTH, monthIn - 1);
this.calendar.set(Calendar.DAY_OF_MONTH, dayIn);
this.calendar.set(Calendar.HOUR_OF_DAY, hourIn);
this.calendar.set(Calendar.MINUTE, minuteIn);
@@ -97,34 +97,40 @@ public class ISO8601DateTime implements IDateTime<ISO8601DateTime>, Comparable<I
this.validateDate();
}
- public ISO8601DateTime(String timeZone, int yearIn, int monthIn, int dayIn, int hourIn, int minuteIn, int secondIn, int millisecondIn) {
- this((timeZone == null ? null : TimeZone.getTimeZone(timeZone)), yearIn, monthIn, dayIn, hourIn, minuteIn, secondIn, millisecondIn);
+ public ISO8601DateTime(String timeZone, int yearIn, int monthIn, int dayIn, int hourIn, int minuteIn,
+ int secondIn, int millisecondIn) {
+ this((timeZone == null ? null : TimeZone.getTimeZone(timeZone)), yearIn, monthIn, dayIn, hourIn,
+ minuteIn, secondIn, millisecondIn);
}
public ISO8601DateTime(ISO8601TimeZone timeZone, ISO8601Date iso8601Date, ISO8601Time iso8601Time) {
- this((timeZone == null ? null : timeZone.getTimeZoneString()), iso8601Date.getYear(), iso8601Date.getMonth(), iso8601Date.getDay(), iso8601Time.getHour(), iso8601Time.getMinute(), iso8601Time.getSecond(), iso8601Time.getMillisecond());
+ this((timeZone == null ? null : timeZone.getTimeZoneString()), iso8601Date.getYear(), iso8601Date
+ .getMonth(), iso8601Date.getDay(), iso8601Time.getHour(), iso8601Time.getMinute(), iso8601Time
+ .getSecond(), iso8601Time.getMillisecond());
}
- public ISO8601DateTime(int yearIn, int monthIn, int dayIn, int hourIn, int minuteIn, int secondIn, int millisecondIn) {
+ public ISO8601DateTime(int yearIn, int monthIn, int dayIn, int hourIn, int minuteIn, int secondIn,
+ int millisecondIn) {
this((TimeZone)null, yearIn, monthIn, dayIn, hourIn, minuteIn, secondIn, millisecondIn);
}
/**
- * Creates a new <code>ISO8601DateTime</code> using the given <code>Calendar</code>. If <code>hasTimeZoneIn</code>
- * is true, then the time zone of the given <code>Calendar</code> is used, otherwise this is a non-time-zoned <code>ISO8601DateTime</code>
+ * Creates a new <code>ISO8601DateTime</code> using the given <code>Calendar</code>. If
+ * <code>hasTimeZoneIn</code> is true, then the time zone of the given <code>Calendar</code> is used,
+ * otherwise this is a non-time-zoned <code>ISO8601DateTime</code>
*
* @param calendarIn the <code>Calendar</code> for the new <code>ISO8601DateTime</code>
- * @param hasTimeZoneIn if true, create a time-zoned <code>ISO8601DateTime</code> else create a non-time-zoned <code>ISO8601DateTime</code>
+ * @param hasTimeZoneIn if true, create a time-zoned <code>ISO8601DateTime</code> else create a
+ * non-time-zoned <code>ISO8601DateTime</code>
*/
private ISO8601DateTime(Calendar calendarIn, boolean hasTimeZoneIn) {
/*
- this.calendar = Calendar.getInstance();
- this.calendar.setLenient(false);
- this.calendar.setTimeZone(calendarIn.getTimeZone());
- this.calendar.setTimeInMillis(calendarIn.getTimeInMillis());
- */
- this.calendar = calendarIn;
- this.hasTimeZone = hasTimeZoneIn;
+ * this.calendar = Calendar.getInstance(); this.calendar.setLenient(false);
+ * this.calendar.setTimeZone(calendarIn.getTimeZone());
+ * this.calendar.setTimeInMillis(calendarIn.getTimeInMillis());
+ */
+ this.calendar = calendarIn;
+ this.hasTimeZone = hasTimeZoneIn;
}
/**
@@ -138,13 +144,14 @@ public class ISO8601DateTime implements IDateTime<ISO8601DateTime>, Comparable<I
public String getTimeZone() {
if (this.getHasTimeZone()) {
- int tzOffsetMillis = this.calendar.get(Calendar.ZONE_OFFSET);
+ int tzOffsetMillis = this.calendar.get(Calendar.ZONE_OFFSET);
if (tzOffsetMillis == 0) {
return "Z";
} else {
- int tzOffsetHours = Math.abs(tzOffsetMillis) / (60 * 60 * 1000);
- int tzOffsetMinutes = (Math.abs(tzOffsetMillis) - (tzOffsetHours * 60 * 60 * 1000)) / (60 * 1000);
- StringBuilder stringBuilder = new StringBuilder();
+ int tzOffsetHours = Math.abs(tzOffsetMillis) / (60 * 60 * 1000);
+ int tzOffsetMinutes = (Math.abs(tzOffsetMillis) - (tzOffsetHours * 60 * 60 * 1000))
+ / (60 * 1000);
+ StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append((tzOffsetMillis < 0 ? '-' : '+'));
stringBuilder.append(String.format("%02d", tzOffsetHours));
stringBuilder.append(':');
@@ -158,28 +165,34 @@ public class ISO8601DateTime implements IDateTime<ISO8601DateTime>, Comparable<I
}
public int getYear() {
- int year = this.calendar.get(Calendar.YEAR);
+ int year = this.calendar.get(Calendar.YEAR);
if (this.calendar.get(Calendar.ERA) == GregorianCalendar.BC) {
return -(year - 1);
} else {
return year;
}
}
+
public int getMonth() {
return this.calendar.get(Calendar.MONTH) + 1;
}
+
public int getDay() {
return this.calendar.get(Calendar.DAY_OF_MONTH);
}
+
public int getHour() {
return this.calendar.get(Calendar.HOUR_OF_DAY);
}
+
public int getMinute() {
return this.calendar.get(Calendar.MINUTE);
}
+
public int getSecond() {
return this.calendar.get(Calendar.SECOND);
}
+
public int getMillisecond() {
return this.calendar.get(Calendar.MILLISECOND);
}
@@ -190,7 +203,7 @@ public class ISO8601DateTime implements IDateTime<ISO8601DateTime>, Comparable<I
* @return a copy of the <code>Calendar</code> backing this <code>ISO8601DateTime</code>.
*/
public Calendar getCalendar() {
- Calendar calendarCopy = Calendar.getInstance();
+ Calendar calendarCopy = Calendar.getInstance();
calendarCopy.setLenient(false);
if (this.getHasTimeZone()) {
calendarCopy.setTimeZone(this.calendar.getTimeZone());
@@ -200,11 +213,12 @@ public class ISO8601DateTime implements IDateTime<ISO8601DateTime>, Comparable<I
}
/**
- * Gets a <code>ISO8601DateTime</code> equivalent to this <code>ISO8601DateTime</code> converted to
- * the GMT time zone. If this <code>ISO8601DateTime</code> is non-time-zoned, this method will throw
- * an illegal state exception.
+ * Gets a <code>ISO8601DateTime</code> equivalent to this <code>ISO8601DateTime</code> converted to the
+ * GMT time zone. If this <code>ISO8601DateTime</code> is non-time-zoned, this method will throw an
+ * illegal state exception.
*
- * @return a <code>ISO8601DateTime</code> equivalent to this <code>ISO8601DateTime</code> in the GMT time zone.
+ * @return a <code>ISO8601DateTime</code> equivalent to this <code>ISO8601DateTime</code> in the GMT time
+ * zone.
* @throws IllegalStateException if this <code>ISO8601DateTime</code> is non-time-zoned.
*/
public ISO8601DateTime getGMTDateTime() {
@@ -214,7 +228,7 @@ public class ISO8601DateTime implements IDateTime<ISO8601DateTime>, Comparable<I
if (this.calendar.get(Calendar.ZONE_OFFSET) == 0) {
return this;
} else {
- Calendar calendarGMT = Calendar.getInstance();
+ Calendar calendarGMT = Calendar.getInstance();
calendarGMT.setLenient(false);
calendarGMT.setTimeZone(TimeZone.getTimeZone("GMT"));
calendarGMT.setTimeInMillis(this.calendar.getTimeInMillis());
@@ -223,47 +237,49 @@ public class ISO8601DateTime implements IDateTime<ISO8601DateTime>, Comparable<I
}
private ISO8601DateTime add(ISO8601Duration iso8601Duration, int sign) {
- Calendar calendarCopy = this.getCalendar();
+ Calendar calendarCopy = this.getCalendar();
int value;
if ((value = iso8601Duration.getYears()) != 0) {
- calendarCopy.add(Calendar.YEAR, sign*value);
+ calendarCopy.add(Calendar.YEAR, sign * value);
}
if ((value = iso8601Duration.getMonths()) != 0) {
- calendarCopy.add(Calendar.MONTH, sign*value);
+ calendarCopy.add(Calendar.MONTH, sign * value);
}
if ((value = iso8601Duration.getDays()) != 0) {
- calendarCopy.add(Calendar.DAY_OF_YEAR, sign*value);
+ calendarCopy.add(Calendar.DAY_OF_YEAR, sign * value);
}
if ((value = iso8601Duration.getHours()) != 0) {
- calendarCopy.add(Calendar.HOUR_OF_DAY, sign*value);
+ calendarCopy.add(Calendar.HOUR_OF_DAY, sign * value);
}
if ((value = iso8601Duration.getMinutes()) != 0) {
- calendarCopy.add(Calendar.MINUTE, sign*value);
+ calendarCopy.add(Calendar.MINUTE, sign * value);
}
- int seconds = iso8601Duration.getSeconds();
+ int seconds = iso8601Duration.getSeconds();
if (seconds >= 1) {
- calendarCopy.add(Calendar.SECOND, sign*seconds);
+ calendarCopy.add(Calendar.SECOND, sign * seconds);
}
- int millis = iso8601Duration.getMillis();
+ int millis = iso8601Duration.getMillis();
if (millis != 0) {
- calendarCopy.add(Calendar.MILLISECOND, sign*millis);
+ calendarCopy.add(Calendar.MILLISECOND, sign * millis);
}
return new ISO8601DateTime(calendarCopy, this.getHasTimeZone());
}
/**
- * Adds the given <code>ISO8601Duration</code> to this <code>ISO8601DateTime</code> and returns
- * a new <code>ISO8601DateTime</code> with the result.
+ * Adds the given <code>ISO8601Duration</code> to this <code>ISO8601DateTime</code> and returns a new
+ * <code>ISO8601DateTime</code> with the result.
*
* @param iso8601Duration the <code>ISO8601Duration</code> to add
* @return a new <code>ISO8601DateTime</code> with the result of the addition
*/
+ @Override
public ISO8601DateTime add(ISO8601Duration iso8601Duration) {
return this.add(iso8601Duration, iso8601Duration.getDurationSign());
}
+ @Override
public ISO8601DateTime sub(ISO8601Duration iso8601Duration) {
return this.add(iso8601Duration, -iso8601Duration.getDurationSign());
}
@@ -280,28 +296,29 @@ public class ISO8601DateTime implements IDateTime<ISO8601DateTime>, Comparable<I
} else if (obj == null || !(obj instanceof ISO8601DateTime)) {
return false;
} else {
- ISO8601DateTime iso8601DateTime = (ISO8601DateTime)obj;
- return (this.getHasTimeZone() == iso8601DateTime.getHasTimeZone()) && (this.calendar.equals(iso8601DateTime.calendar));
+ ISO8601DateTime iso8601DateTime = (ISO8601DateTime)obj;
+ return (this.getHasTimeZone() == iso8601DateTime.getHasTimeZone())
+ && (this.calendar.equals(iso8601DateTime.calendar));
}
}
public static ISO8601DateTime fromCalendar(Calendar calendar) {
- Calendar calendarNew = Calendar.getInstance();
+ Calendar calendarNew = Calendar.getInstance();
calendarNew.setTime(calendar.getTime());
calendarNew.setLenient(false);
return new ISO8601DateTime(calendarNew, true);
}
public static ISO8601DateTime fromDate(Date date) {
- Calendar calendar = Calendar.getInstance();
+ Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.setLenient(false);
return new ISO8601DateTime(calendar, true);
}
/**
- * Creates a new <code>ISO8601DateTime</code> by parsing the given <code>String</code> in the extended ISO8601
- * format defined for XML.
+ * Creates a new <code>ISO8601DateTime</code> by parsing the given <code>String</code> in the extended
+ * ISO8601 format defined for XML.
*
* @param strDateTime the <code>String</code> in ISO8601 date-time format.
* @return a new <code>ISO8601DateTime</code>.
@@ -311,37 +328,37 @@ public class ISO8601DateTime implements IDateTime<ISO8601DateTime>, Comparable<I
/*
* Trim whitespace
*/
- int startPos = ParseUtils.nextNonWhite(strDateTime, 0);
+ int startPos = ParseUtils.nextNonWhite(strDateTime, 0);
- ParseUtils.ParseValue<Integer> parseValue = ParseUtils.getSignedValue(strDateTime, startPos);
+ ParseUtils.ParseValue<Integer> parseValue = ParseUtils.getSignedValue(strDateTime, startPos);
if (parseValue == null) {
throw new ParseException("Invalid year", startPos);
}
- int year = parseValue.getValue();
+ int year = parseValue.getValue();
if (parseValue.getNextPos() < 4) {
throw new ParseException("Invalid year (must be at least 4 digits)", startPos);
}
- startPos += parseValue.getNextPos();
+ startPos += parseValue.getNextPos();
if (startPos >= strDateTime.length() || strDateTime.charAt(startPos) != '-') {
throw new ParseException("Invalid date", startPos);
}
startPos++;
- int month = ParseUtils.getTwoDigitValue(strDateTime, startPos);
+ int month = ParseUtils.getTwoDigitValue(strDateTime, startPos);
if (month <= 0 || month > 12) {
throw new ParseException("Invalid month", startPos);
}
- startPos += 2;
+ startPos += 2;
if (startPos >= strDateTime.length() || strDateTime.charAt(startPos) != '-') {
throw new ParseException("Invalid date", startPos);
}
startPos++;
- int day = ParseUtils.getTwoDigitValue(strDateTime, startPos);
+ int day = ParseUtils.getTwoDigitValue(strDateTime, startPos);
if (day < 1 || day > 31) {
throw new ParseException("Invalid day", startPos);
}
- startPos += 2;
+ startPos += 2;
/*
* There should now be a 'T'
@@ -354,11 +371,11 @@ public class ISO8601DateTime implements IDateTime<ISO8601DateTime>, Comparable<I
/*
* Get the hours
*/
- int hours = ParseUtils.getTwoDigitValue(strDateTime, startPos);
+ int hours = ParseUtils.getTwoDigitValue(strDateTime, startPos);
if (hours < 0 || hours > 23) {
throw new ParseException("Invalid hours", startPos);
}
- startPos += 2;
+ startPos += 2;
if (startPos >= strDateTime.length() || strDateTime.charAt(startPos) != ':') {
throw new ParseException("Invalid date", startPos);
}
@@ -367,11 +384,11 @@ public class ISO8601DateTime implements IDateTime<ISO8601DateTime>, Comparable<I
/*
* Get the minutes
*/
- int minutes = ParseUtils.getTwoDigitValue(strDateTime, startPos);
+ int minutes = ParseUtils.getTwoDigitValue(strDateTime, startPos);
if (minutes < 0 || minutes > 59) {
throw new ParseException("Invalid minutes", startPos);
}
- startPos += 2;
+ startPos += 2;
if (startPos >= strDateTime.length() || strDateTime.charAt(startPos) != ':') {
throw new ParseException("Invalid date", startPos);
}
@@ -380,41 +397,41 @@ public class ISO8601DateTime implements IDateTime<ISO8601DateTime>, Comparable<I
/*
* Get the two-digit second of minute
*/
- int ss = ParseUtils.getTwoDigitValue(strDateTime, startPos);
+ int ss = ParseUtils.getTwoDigitValue(strDateTime, startPos);
if (ss < 0 || ss >= 60) {
throw new ParseException("Invalid second of minute", startPos);
}
- startPos += 2;
+ startPos += 2;
/*
* Now determine if we have a milliseconds portion
*/
- int ms = 0;
+ int ms = 0;
if (startPos < strDateTime.length()) {
if (strDateTime.charAt(startPos) == '.') {
startPos++;
if ((ms = ParseUtils.getThreeDigitValue(strDateTime, startPos)) < 0 || ms >= 1000) {
throw new ParseException("Invalid milliseconds", startPos);
}
- startPos += 3;
+ startPos += 3;
}
}
/*
* Now determine if there is a timezone
*/
- String timezone = null;
+ String timezone = null;
if (startPos < strDateTime.length()) {
- switch(strDateTime.charAt(startPos)) {
+ switch (strDateTime.charAt(startPos)) {
case 'Z':
- timezone = "GMT";
+ timezone = "GMT";
startPos++;
break;
case '-':
case '+':
- if (startPos+5 < strDateTime.length()) {
- timezone = "GMT" + strDateTime.substring(startPos, startPos+6);
+ if (startPos + 5 < strDateTime.length()) {
+ timezone = "GMT" + strDateTime.substring(startPos, startPos + 6);
} else {
throw new ParseException("Invalid timezone", startPos);
}
@@ -429,9 +446,9 @@ public class ISO8601DateTime implements IDateTime<ISO8601DateTime>, Comparable<I
@Override
public String stringValue() {
- StringBuilder stringBuilder = new StringBuilder();
+ StringBuilder stringBuilder = new StringBuilder();
if (this.calendar.get(Calendar.ERA) == GregorianCalendar.BC) {
- int yearBC = this.calendar.get(Calendar.YEAR) - 1;
+ int yearBC = this.calendar.get(Calendar.YEAR) - 1;
stringBuilder.append('-');
stringBuilder.append(String.format("%d", yearBC));
} else {
@@ -447,10 +464,10 @@ public class ISO8601DateTime implements IDateTime<ISO8601DateTime>, Comparable<I
stringBuilder.append(String.format("%02d", this.calendar.get(Calendar.MINUTE)));
stringBuilder.append(':');
stringBuilder.append(String.format("%02d", this.calendar.get(Calendar.SECOND)));
- int ms = this.calendar.get(Calendar.MILLISECOND);
+ int ms = this.calendar.get(Calendar.MILLISECOND);
if (ms != 0) {
stringBuilder.append('.');
- stringBuilder.append(String.format("%03d", ms));
+ stringBuilder.append(String.format("%03d", ms));
}
if (this.getHasTimeZone()) {
stringBuilder.append(this.getTimeZone());
@@ -464,8 +481,8 @@ public class ISO8601DateTime implements IDateTime<ISO8601DateTime>, Comparable<I
}
private static int compareCalendars(Calendar cal1, Calendar cal2) {
- long cal1Time = cal1.getTimeInMillis();
- long cal2Time = cal2.getTimeInMillis();
+ long cal1Time = cal1.getTimeInMillis();
+ long cal2Time = cal2.getTimeInMillis();
if (cal1Time < cal2Time) {
return -1;
} else if (cal1Time > cal2Time) {
@@ -481,26 +498,28 @@ public class ISO8601DateTime implements IDateTime<ISO8601DateTime>, Comparable<I
if (o.getHasTimeZone()) {
return compareCalendars(this.calendar, o.calendar);
} else {
- long thisMilliseconds = this.calendar.getTimeInMillis();
- long oMilliseconds = o.calendar.getTimeInMillis();
+ long thisMilliseconds = this.calendar.getTimeInMillis();
+ long oMilliseconds = o.calendar.getTimeInMillis();
if (thisMilliseconds < (oMilliseconds - TZOFFSET_14_HOURS_MILLIS)) {
return -1;
} else if (thisMilliseconds > (oMilliseconds + TZOFFSET_14_HOURS_MILLIS)) {
return 1;
} else {
- throw new IllegalArgumentException("Cannot compare this ISO8601DateTime with non-time-zoned ISO8601DateTime");
+ throw new IllegalArgumentException(
+ "Cannot compare this ISO8601DateTime with non-time-zoned ISO8601DateTime");
}
}
} else {
if (o.getHasTimeZone()) {
- long thisMilliseconds = this.calendar.getTimeInMillis();
- long oMilliseconds = o.calendar.getTimeInMillis();
+ long thisMilliseconds = this.calendar.getTimeInMillis();
+ long oMilliseconds = o.calendar.getTimeInMillis();
if ((thisMilliseconds + TZOFFSET_14_HOURS_MILLIS) < oMilliseconds) {
return -1;
} else if ((thisMilliseconds - TZOFFSET_14_HOURS_MILLIS) > oMilliseconds) {
return 1;
} else {
- throw new IllegalArgumentException("Cannot compare this ISO8601DateTime with time-zoned ISO8601DateTime");
+ throw new IllegalArgumentException(
+ "Cannot compare this ISO8601DateTime with time-zoned ISO8601DateTime");
}
} else {
/*