mercredi 28 septembre 2016

How to improve the code quality to see if a string matches either one of the regex's Java

in one of my projects i need to compare the URI with several regex patterns(15+ regex patterns). Currently i have used a if ladder to see if either one of them gets matched and there onward the logical part of the code is executed.

Glimpse of the code now

         if (uri.matches(Constants.GET_ALL_APIS_STORE_REGEX)) {    
            long lastUpdatedTime = InBoundETagManager.apisGet(null, null, tenantDomain, null);
            String eTag = ETagGenerator.getETag(lastUpdatedTime);
            if (eTag.equals(ifNoneMatch)) {
                message.getExchange().put("ETag", eTag);
                generate304NotModifiedResponse(message);
            }
            message.getExchange().put("ETag", eTag);
        } else if (uri.matches(Constants.GET_API_FOR_ID_REGEX)) {   //        /apis/{apiId}
            apiId = UUIDList.get(0);
            String requestedTenantDomain = RestApiUtil.getRequestedTenantDomain(tenantDomain);
            long lastUpdatedTime = InBoundETagManager.apisApiIdGet(apiId, requestedTenantDomain, uri);
            String eTag = ETagGenerator.getETag(lastUpdatedTime);
            handleInterceptorResponse(message, ifNoneMatch, eTag);
        } else if (uri.matches(Constants.GET_SWAGGER_FOR_API_ID_REGEX)) {   //  /apis/{apiId}/swagger
            apiId = UUIDList.get(0);
            long lastUpdatedTime = InBoundETagManager.apisApiIdSwaggerGet(apiId, tenantDomain); 
            String eTag = ETagGenerator.getETag(lastUpdatedTime);
            if (lastUpdatedTime == 0L) {
                log.info("No last updated time available for the desired API swagger json file");
            }
            handleInterceptorResponse(message, ifNoneMatch, eTag);
        }........

can someone please introduce me with a more neat and clever way to doing this regex matching thing?? Any help would be much appreciated!!! thanks in advance

Aucun commentaire:

Enregistrer un commentaire