private Object predefined()

in endorsed/src/org.apache.sis.referencing/main/org/apache/sis/referencing/EPSGFactoryFallback.java [280:362]


    private Object predefined(String code, final int kind) throws NoSuchAuthorityCodeException {
        try {
            /*
             * Parse the value after the last ':' for an URN of the form "urn:ogc:def:crs:epsg::4326",
             * or after the last '#' for an URL of the form "http://www.opengis.net/gml/srs/epsg.xml#4326".
             * We do not bother to verify if the part before ':' or '#' is legal because this analysis has
             * already be done by `MultiAuthoritiesFactory`. The check for separator should be unnecessary,
             * but we nevertheless do it because this factory is sometime invoked directly rather than through
             * `MultiAuthoritiesFactory`. The direct invocation happens when `CRS.forCode(String)` fallbacks
             * on `AuthorityFactories.fallback(…)`.
             */
            final int s = Math.max(code.lastIndexOf(Constants.DEFAULT_SEPARATOR), code.lastIndexOf('#'));
            code = CharSequences.trimWhitespaces(code, s + 1, code.length()).toString();
            final short n = Short.parseShort(code);
            if ((kind & (ELLIPSOID | DATUM | CRS)) != 0) {
                for (final CommonCRS crs : CommonCRS.values()) {
                    /*
                     * In a complete EPSG dataset we could have an ambiguity below because the same code can be used
                     * for datum, ellipsoid and CRS objects. However, in the particular case of this EPSG-subset, we
                     * ensured that there is no such collision - see CommonCRSTest.ensureNoCodeCollision().
                     */
                    if ((kind & ELLIPSOID) != 0  &&  n == crs.ellipsoid) return crs.ellipsoid();
                    if ((kind & DATUM)     != 0  &&  n == crs.datum)     return crs.datum();
                    if ((kind & CRS) != 0) {
                        if (n == crs.geographic) return crs.geographic();
                        if (n == crs.geocentric) return crs.geocentric();
                        if (n == crs.geo3D)      return crs.geographic3D();
                        final double latitude;
                        int zone;
                        if (crs.northUTM != 0 && (zone = n - crs.northUTM) >= crs.firstZone && zone <= crs.lastZone) {
                            latitude = +1;          // Any north latitude below 56°N (because of Norway exception) is okay
                        } else if (crs.southUTM != 0 && (zone = n - crs.southUTM) >= crs.firstZone && zone <= crs.lastZone) {
                            latitude = -1;          // Any south latitude above 80°S (because of UPS south case) is okay.
                        } else if (n == crs.northUPS) {
                            latitude = Latitude.MAX_VALUE;
                            zone     = 30;                  // Any random UTM zone is okay.
                        } else if (n == crs.southUPS) {
                            latitude = Latitude.MIN_VALUE;
                            zone     = 30;                  // Any random UTM zone is okay.
                        } else {
                            continue;
                        }
                        return crs.universal(latitude, TransverseMercator.Zoner.UTM.centralMeridian(zone));
                    }
                }
                if ((kind & (DATUM | CRS)) != 0) {
                    for (final CommonCRS.Vertical candidate : CommonCRS.Vertical.values()) {
                        if (candidate.isEPSG) {
                            if ((kind & DATUM) != 0  &&  candidate.datum == n) return candidate.datum();
                            if ((kind & CRS)   != 0  &&  candidate.crs   == n) return candidate.crs();
                        }
                    }
                }
            }
            /*
             * Other kinds of objects (prime meridian, units of measurement, etc). We check those candidates only after
             * above loop (CRS, datum, etc.) in order to give precedence to CRS if the same code is used for both kinds
             * of objects. We do not bother to cache coordinate system and axis instances.
             */
            if ((kind & PRIME_MERIDIAN) != 0  &&  n == Constants.EPSG_GREENWICH) {
                return CommonCRS.WGS84.primeMeridian();
            }
            if ((kind & CS) != 0) {
                final CoordinateSystem cs = StandardDefinitions.createCoordinateSystem(n, false);
                if (cs != null) return cs;
            }
            if ((kind & AXIS) != 0) {
                final CoordinateSystemAxis axis = StandardDefinitions.createAxis(n, false);
                if (axis != null) return axis;
            }
            if ((kind & UNIT) != 0) {
                final Unit<?> unit = Units.valueOfEPSG(n);
                if (unit != null) return unit;
            }
        } catch (NumberFormatException cause) {
            final NoSuchAuthorityCodeException e = new NoSuchAuthorityCodeException(Resources.format(
                    Resources.Keys.NoSuchAuthorityCode_3, Constants.EPSG, toClass(kind), code), getAuthorityTitle(), code);
            e.initCause(cause);
            throw e;
        }
        throw new NoSuchAuthorityCodeException(Resources.format(Resources.Keys.NoSuchAuthorityCodeInSubset_4,
                Constants.EPSG, toClass(kind), code, getInstallationURL()), getAuthorityTitle(), code);
    }