Skip to content
This repository has been archived by the owner on Oct 15, 2021. It is now read-only.

Commit

Permalink
Fix performance issue of XML string date representation.
Browse files Browse the repository at this point in the history
  • Loading branch information
eduramiba committed Oct 11, 2011
1 parent c21577c commit bfb9e21
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 45 deletions.
2 changes: 1 addition & 1 deletion AttributesAPI/manifest.mf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Manifest-Version: 1.0
OpenIDE-Module: org.gephi.data.attributes.api
OpenIDE-Module-Localizing-Bundle: org/gephi/data/attributes/api/Bundle.properties
OpenIDE-Module-Specification-Version: 0.8.0.2
OpenIDE-Module-Specification-Version: 0.8.0.3
AutoUpdate-Essential-Module: true
32 changes: 17 additions & 15 deletions AttributesAPI/src/org/gephi/data/attributes/api/AttributeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ Development and Distribution License("CDDL") (collectively, the
import java.util.GregorianCalendar;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import org.openide.util.Exceptions;
import org.openide.util.Lookup;

/**
Expand All @@ -54,6 +53,15 @@ Development and Distribution License("CDDL") (collectively, the
*/
public abstract class AttributeUtils {

private static DatatypeFactory dateFactory;

static {
try {
dateFactory = DatatypeFactory.newInstance();
} catch (DatatypeConfigurationException ex) {
}
}

public abstract boolean isNodeColumn(AttributeColumn column);

public abstract boolean isEdgeColumn(AttributeColumn column);
Expand Down Expand Up @@ -101,7 +109,7 @@ public static synchronized AttributeUtils getDefault() {
}

/**
* Used for export (writes XML date strings).
* Used for attributes representation.
*
* @param d a double to convert from
*
Expand All @@ -110,19 +118,13 @@ public static synchronized AttributeUtils getDefault() {
* @throws IllegalArgumentException if {@code d} is infinite.
*/
public static String getXMLDateStringFromDouble(double d) {
try {
DatatypeFactory dateFactory = DatatypeFactory.newInstance();
if (d == Double.NEGATIVE_INFINITY) {
return "-Infinity";
} else if (d == Double.POSITIVE_INFINITY) {
return "Infinity";
}
GregorianCalendar gc = new GregorianCalendar();
gc.setTimeInMillis((long) d);
return dateFactory.newXMLGregorianCalendar(gc).toXMLFormat().substring(0, 23);
} catch (DatatypeConfigurationException ex) {
Exceptions.printStackTrace(ex);
return "";
if (d == Double.NEGATIVE_INFINITY) {
return "-Infinity";
} else if (d == Double.POSITIVE_INFINITY) {
return "Infinity";
}
GregorianCalendar gc = new GregorianCalendar();
gc.setTimeInMillis((long) d);
return dateFactory.newXMLGregorianCalendar(gc).toXMLFormat().substring(0, 23);
}
}
2 changes: 1 addition & 1 deletion DynamicAPI/manifest.mf
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ Manifest-Version: 1.0
AutoUpdate-Essential-Module: true
OpenIDE-Module: org.gephi.dynamic.api
OpenIDE-Module-Localizing-Bundle: org/gephi/dynamic/api/Bundle.properties
OpenIDE-Module-Specification-Version: 0.8.0.2
OpenIDE-Module-Specification-Version: 0.8.0.3

54 changes: 26 additions & 28 deletions DynamicAPI/src/org/gephi/dynamic/DynamicUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,15 @@ Development and Distribution License("CDDL") (collectively, the
* @author Cezary Bartosiak
*/
public final class DynamicUtilities {
private static DatatypeFactory dateFactory;

static {
try {
dateFactory = DatatypeFactory.newInstance();
} catch (DatatypeConfigurationException ex) {
}
}

/**
* Used for import (parses XML date strings).
*
Expand All @@ -96,24 +104,18 @@ public final class DynamicUtilities {
*/
public static double getDoubleFromXMLDateString(String str) {
try {
DatatypeFactory dateFactory = DatatypeFactory.newInstance();
return dateFactory.newXMLGregorianCalendar(str.length() > 23 ? str.substring(0, 23) : str).
toGregorianCalendar().getTimeInMillis();
} catch (IllegalArgumentException ex) {
//Try simple format
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
return dateFactory.newXMLGregorianCalendar(str.length() > 23 ? str.substring(0, 23) : str).
toGregorianCalendar().getTimeInMillis();
} catch (IllegalArgumentException ex) {
//Try simple format
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date date = dateFormat.parse(str);
return date.getTime();
} catch (ParseException ex1) {
Exceptions.printStackTrace(ex1);
return 0.0;
}
Date date = dateFormat.parse(str);
return date.getTime();
} catch (ParseException ex1) {
Exceptions.printStackTrace(ex1);
return 0.0;
}
} catch (DatatypeConfigurationException ex) {
Exceptions.printStackTrace(ex);
return 0.0;
}
}

Expand All @@ -127,20 +129,16 @@ public static double getDoubleFromXMLDateString(String str) {
* @throws IllegalArgumentException if {@code d} is infinite.
*/
public static String getXMLDateStringFromDouble(double d) {
try {
DatatypeFactory dateFactory = DatatypeFactory.newInstance();
if (Double.isInfinite(d)) {
throw new IllegalArgumentException("The passed double cannot be infinite.");
}
GregorianCalendar gc = new GregorianCalendar();
gc.setTimeInMillis((long) d);
return dateFactory.newXMLGregorianCalendar(gc).toXMLFormat().substring(0, 23);
} catch (DatatypeConfigurationException ex) {
Exceptions.printStackTrace(ex);
return "";
if (d == Double.NEGATIVE_INFINITY) {
return "-Infinity";
} else if (d == Double.POSITIVE_INFINITY) {
return "Infinity";
}
GregorianCalendar gc = new GregorianCalendar();
gc.setTimeInMillis((long) d);
return dateFactory.newXMLGregorianCalendar(gc).toXMLFormat().substring(0, 23);
}

/**
* Returns a new {@code DynamicType} instance that contains a given
* {@code Interval} in.
Expand Down

0 comments on commit bfb9e21

Please sign in to comment.