class Utilities {

    public static Envelope getEnvelopeAsWGS84(final Envelope envelope, boolean get2D)
            throws FactoryException, TransformException {
        if (envelope == null)
            throw new IllegalArgumentException("Specified envelope is null");
        Envelope requestedWGS84;
        final CoordinateReferenceSystem crs = envelope.getCoordinateReferenceSystem();

        // do we need to transform the requested envelope?
        if (!CRS.equalsIgnoreMetadata(crs, DefaultGeographicCRS.WGS84)) {
            GeneralEnvelope env = CRS.transform(envelope, DefaultGeographicCRS.WGS84);
            if (get2D) {
                requestedWGS84 = new Envelope2D(env);
                ((Envelope2D) requestedWGS84)
                        .setCoordinateReferenceSystem(DefaultGeographicCRS.WGS84);
            } else {
                requestedWGS84 = env;
                ((GeneralEnvelope) requestedWGS84)
                        .setCoordinateReferenceSystem(DefaultGeographicCRS.WGS84);
            }
            return requestedWGS84;

        } else {
            if (get2D)
                return new Envelope2D(envelope);
            else
                return new GeneralEnvelope(envelope);
        }
    }

    private  Utilities();

}