You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@netbeans.apache.org by "asbachb (via GitHub)" <gi...@apache.org> on 2023/05/20 14:51:50 UTC

[GitHub] [netbeans] asbachb commented on a diff in pull request #5970: Refactoring: Simplify `Profile`

asbachb commented on code in PR #5970:
URL: https://github.com/apache/netbeans/pull/5970#discussion_r1199621792


##########
enterprise/j2ee.core/src/org/netbeans/api/j2ee/core/Profile.java:
##########
@@ -140,134 +123,23 @@ public String toString() {
 
     @CheckForNull
     public static Profile fromPropertiesString(@NullAllowed String value) {
-        String valueMinusQuotes = value;
-        if(value != null && value.contains("\"")){
-            valueMinusQuotes = value.replace("\"","");
-        }
-        if (J2EE_13.toPropertiesString().equals(valueMinusQuotes)) {
-            return J2EE_13;
-        } else if (J2EE_14.toPropertiesString().equals(valueMinusQuotes)) {
-            return J2EE_14;
-        } else if (JAVA_EE_5.toPropertiesString().equals(valueMinusQuotes)) {
-            return JAVA_EE_5;
-        } else if (JAVA_EE_6_FULL.toPropertiesString().equals(valueMinusQuotes)
-                || "EE_6_FULL".equals(value)){ // NOI18N
-            return JAVA_EE_6_FULL;
-        } else if (JAVA_EE_6_WEB.toPropertiesString().equals(valueMinusQuotes)
-                || "EE_6_WEB".equals(value)) {
-            return JAVA_EE_6_WEB;
-        } else if (JAVA_EE_7_FULL.toPropertiesString().equals(valueMinusQuotes)
-                || "EE_7_FULL".equals(value)) { // NOI18N
-            return JAVA_EE_7_FULL;
-        } else if (JAVA_EE_7_WEB.toPropertiesString().equals(valueMinusQuotes)
-                || "EE_7_WEB".equals(value)) {
-            return JAVA_EE_7_WEB;
-        } else if (JAVA_EE_8_FULL.toPropertiesString().equals(valueMinusQuotes)
-                || "EE_8_FULL".equals(value)) { // NOI18N
-            return JAVA_EE_8_FULL;
-        } else if (JAVA_EE_8_WEB.toPropertiesString().equals(valueMinusQuotes)
-                || "EE_8_WEB".equals(value)) {
-            return JAVA_EE_8_WEB;
-        } else if (JAKARTA_EE_8_FULL.toPropertiesString().equals(valueMinusQuotes)
-                || "JAKARTA_EE_8_FULL".equals(value)) {
-            return JAKARTA_EE_8_FULL;
-        } else if (JAKARTA_EE_8_WEB.toPropertiesString().equals(valueMinusQuotes)
-                || "JAKARTA_EE_8_WEB".equals(value)) {
-            return JAKARTA_EE_8_WEB;
-        } else if (JAKARTA_EE_9_FULL.toPropertiesString().equals(valueMinusQuotes)
-                || "JAKARTA_EE_9_FULL".equals(value)) {
-            return JAKARTA_EE_9_FULL;
-        } else if (JAKARTA_EE_9_WEB.toPropertiesString().equals(valueMinusQuotes)
-                || "JAKARTA_EE_9_WEB".equals(value)) {
-            return JAKARTA_EE_9_WEB;
-        } else if (JAKARTA_EE_9_1_FULL.toPropertiesString().equals(valueMinusQuotes)
-                || "JAKARTA_EE_9_1_FULL".equals(value)) {
-            return JAKARTA_EE_9_1_FULL;
-        } else if (JAKARTA_EE_9_1_WEB.toPropertiesString().equals(valueMinusQuotes)
-                || "JAKARTA_EE_9_1_WEB".equals(value)) {
-            return JAKARTA_EE_9_1_WEB;
-        } else if (JAKARTA_EE_10_FULL.toPropertiesString().equals(valueMinusQuotes)
-                || "JAKARTA_EE_10_FULL".equals(value)) {
-            return JAKARTA_EE_10_FULL;
-        } else if (JAKARTA_EE_10_WEB.toPropertiesString().equals(valueMinusQuotes)
-                || "JAKARTA_EE_10_WEB".equals(value)) {
-            return JAKARTA_EE_10_WEB;
-        } else {
-          return null;
+        if (value == null) {
+            return null;
         }
-    }
-
-    private static String getProfileVersion(@NonNull Profile profile) {
-        String profileDetails = profile.toPropertiesString();
-        int indexOfDash = profileDetails.indexOf("-");
-        if (indexOfDash != -1) {
-            return profileDetails.substring(0, indexOfDash);
-        }
-        return profileDetails;
-    }
 
-    private static boolean compareWebAndFull(@NonNull Profile profileToCompare, @NonNull Profile comparingVersion) {
-        boolean isThisFullProfile = isFullProfile(profileToCompare);
-        boolean isParamFullProfile = isFullProfile(comparingVersion);
-        if (isThisFullProfile && isParamFullProfile) {
-            // Both profiles are Java EE Full
-            return true;
-        }
-        if (!isThisFullProfile && !isParamFullProfile) {
-            // Both profiles are Java EE Web
-            return true;
-        }
-        if (isThisFullProfile && !isParamFullProfile) {
-            // profileToCompare is Java EE Full profile and comparingVersion is only Java EEWeb profile
-            return true;
-        }
-        return false;
-    }
-
-    private static boolean isFullProfile(@NonNull Profile profile) {
-        final String profileDetails = profile.toPropertiesString();
-        if (profileDetails.indexOf("-") == -1) {
-            return true;
+        String valueMinusQuotes = value;
+        if(value.contains("\"")){
+            valueMinusQuotes = value.replace("\"","");
         }
-        return false;
-    }
 
-    /**
-     * Compares if the first given profile has equal or higher Java EE version
-     * in comparison to the second profile.
-     *
-     * Please be aware of the following rules:
-     * <br/><br/>
-     *
-     * 1) Each Java EE X version is considered as lower than Java EE X+1 version
-     * (this applies regardless on Web/Full specification and in reality it means
-     * that even Java EE 6 Full version is considered as lower than Java EE 7 Web)
-     * <br/><br/>
-     *
-     * 2) Each Java EE X Web version is considered as lower than Java EE X Full
-     * <br/>
-     *
-     * @param profileToCompare profile that we want to compare
-     * @param comparingVersion version which we are comparing with
-     * @return <code>true</code> if the profile version is equal or higher in
-     *         comparison with the second one, <code>false</code> otherwise
-     * @since 1.19
-     */
-    private static boolean isVersionEqualOrHigher(@NonNull Profile profileToCompare, @NonNull Profile comparingVersion) {
-        int comparisonResult = Profile.UI_COMPARATOR.compare(profileToCompare, comparingVersion);
-        if (comparisonResult == 0) {
-            // The same version for both
-            return true;
-        } else {
-            String profileToCompareVersion = getProfileVersion(profileToCompare);
-            String comparingProfileVersion = getProfileVersion(comparingVersion);
-            if (profileToCompareVersion.equals(comparingProfileVersion)) {
-                return compareWebAndFull(profileToCompare, comparingVersion);
-            } else {
-                // profileToCompare has lower version than comparingVersion
-                return comparisonResult <= 0;
+        for (Profile profile : Profile.values()) {
+            if (profile.toPropertiesString().equals(valueMinusQuotes)
+                    || profile.name().equals(value)
+                    || profile.name().endsWith(value)) {
+                return profile;
             }
         }

Review Comment:
   I've done this on purpose to stick to existing logic.
   
   But I guess we can use `valueMinusQuotes` for all statements.



##########
enterprise/j2ee.core/src/org/netbeans/api/j2ee/core/Profile.java:
##########
@@ -140,134 +123,23 @@ public String toString() {
 
     @CheckForNull
     public static Profile fromPropertiesString(@NullAllowed String value) {
-        String valueMinusQuotes = value;
-        if(value != null && value.contains("\"")){
-            valueMinusQuotes = value.replace("\"","");
-        }
-        if (J2EE_13.toPropertiesString().equals(valueMinusQuotes)) {
-            return J2EE_13;
-        } else if (J2EE_14.toPropertiesString().equals(valueMinusQuotes)) {
-            return J2EE_14;
-        } else if (JAVA_EE_5.toPropertiesString().equals(valueMinusQuotes)) {
-            return JAVA_EE_5;
-        } else if (JAVA_EE_6_FULL.toPropertiesString().equals(valueMinusQuotes)
-                || "EE_6_FULL".equals(value)){ // NOI18N
-            return JAVA_EE_6_FULL;
-        } else if (JAVA_EE_6_WEB.toPropertiesString().equals(valueMinusQuotes)
-                || "EE_6_WEB".equals(value)) {
-            return JAVA_EE_6_WEB;
-        } else if (JAVA_EE_7_FULL.toPropertiesString().equals(valueMinusQuotes)
-                || "EE_7_FULL".equals(value)) { // NOI18N
-            return JAVA_EE_7_FULL;
-        } else if (JAVA_EE_7_WEB.toPropertiesString().equals(valueMinusQuotes)
-                || "EE_7_WEB".equals(value)) {
-            return JAVA_EE_7_WEB;
-        } else if (JAVA_EE_8_FULL.toPropertiesString().equals(valueMinusQuotes)
-                || "EE_8_FULL".equals(value)) { // NOI18N
-            return JAVA_EE_8_FULL;
-        } else if (JAVA_EE_8_WEB.toPropertiesString().equals(valueMinusQuotes)
-                || "EE_8_WEB".equals(value)) {
-            return JAVA_EE_8_WEB;
-        } else if (JAKARTA_EE_8_FULL.toPropertiesString().equals(valueMinusQuotes)
-                || "JAKARTA_EE_8_FULL".equals(value)) {
-            return JAKARTA_EE_8_FULL;
-        } else if (JAKARTA_EE_8_WEB.toPropertiesString().equals(valueMinusQuotes)
-                || "JAKARTA_EE_8_WEB".equals(value)) {
-            return JAKARTA_EE_8_WEB;
-        } else if (JAKARTA_EE_9_FULL.toPropertiesString().equals(valueMinusQuotes)
-                || "JAKARTA_EE_9_FULL".equals(value)) {
-            return JAKARTA_EE_9_FULL;
-        } else if (JAKARTA_EE_9_WEB.toPropertiesString().equals(valueMinusQuotes)
-                || "JAKARTA_EE_9_WEB".equals(value)) {
-            return JAKARTA_EE_9_WEB;
-        } else if (JAKARTA_EE_9_1_FULL.toPropertiesString().equals(valueMinusQuotes)
-                || "JAKARTA_EE_9_1_FULL".equals(value)) {
-            return JAKARTA_EE_9_1_FULL;
-        } else if (JAKARTA_EE_9_1_WEB.toPropertiesString().equals(valueMinusQuotes)
-                || "JAKARTA_EE_9_1_WEB".equals(value)) {
-            return JAKARTA_EE_9_1_WEB;
-        } else if (JAKARTA_EE_10_FULL.toPropertiesString().equals(valueMinusQuotes)
-                || "JAKARTA_EE_10_FULL".equals(value)) {
-            return JAKARTA_EE_10_FULL;
-        } else if (JAKARTA_EE_10_WEB.toPropertiesString().equals(valueMinusQuotes)
-                || "JAKARTA_EE_10_WEB".equals(value)) {
-            return JAKARTA_EE_10_WEB;
-        } else {
-          return null;
+        if (value == null) {
+            return null;
         }
-    }
-
-    private static String getProfileVersion(@NonNull Profile profile) {
-        String profileDetails = profile.toPropertiesString();
-        int indexOfDash = profileDetails.indexOf("-");
-        if (indexOfDash != -1) {
-            return profileDetails.substring(0, indexOfDash);
-        }
-        return profileDetails;
-    }
 
-    private static boolean compareWebAndFull(@NonNull Profile profileToCompare, @NonNull Profile comparingVersion) {
-        boolean isThisFullProfile = isFullProfile(profileToCompare);
-        boolean isParamFullProfile = isFullProfile(comparingVersion);
-        if (isThisFullProfile && isParamFullProfile) {
-            // Both profiles are Java EE Full
-            return true;
-        }
-        if (!isThisFullProfile && !isParamFullProfile) {
-            // Both profiles are Java EE Web
-            return true;
-        }
-        if (isThisFullProfile && !isParamFullProfile) {
-            // profileToCompare is Java EE Full profile and comparingVersion is only Java EEWeb profile
-            return true;
-        }
-        return false;
-    }
-
-    private static boolean isFullProfile(@NonNull Profile profile) {
-        final String profileDetails = profile.toPropertiesString();
-        if (profileDetails.indexOf("-") == -1) {
-            return true;
+        String valueMinusQuotes = value;
+        if(value.contains("\"")){
+            valueMinusQuotes = value.replace("\"","");
         }
-        return false;
-    }
 
-    /**
-     * Compares if the first given profile has equal or higher Java EE version
-     * in comparison to the second profile.
-     *
-     * Please be aware of the following rules:
-     * <br/><br/>
-     *
-     * 1) Each Java EE X version is considered as lower than Java EE X+1 version
-     * (this applies regardless on Web/Full specification and in reality it means
-     * that even Java EE 6 Full version is considered as lower than Java EE 7 Web)
-     * <br/><br/>
-     *
-     * 2) Each Java EE X Web version is considered as lower than Java EE X Full
-     * <br/>
-     *
-     * @param profileToCompare profile that we want to compare
-     * @param comparingVersion version which we are comparing with
-     * @return <code>true</code> if the profile version is equal or higher in
-     *         comparison with the second one, <code>false</code> otherwise
-     * @since 1.19
-     */
-    private static boolean isVersionEqualOrHigher(@NonNull Profile profileToCompare, @NonNull Profile comparingVersion) {
-        int comparisonResult = Profile.UI_COMPARATOR.compare(profileToCompare, comparingVersion);
-        if (comparisonResult == 0) {
-            // The same version for both
-            return true;
-        } else {
-            String profileToCompareVersion = getProfileVersion(profileToCompare);
-            String comparingProfileVersion = getProfileVersion(comparingVersion);
-            if (profileToCompareVersion.equals(comparingProfileVersion)) {
-                return compareWebAndFull(profileToCompare, comparingVersion);
-            } else {
-                // profileToCompare has lower version than comparingVersion
-                return comparisonResult <= 0;
+        for (Profile profile : Profile.values()) {
+            if (profile.toPropertiesString().equals(valueMinusQuotes)
+                    || profile.name().equals(value)
+                    || profile.name().endsWith(value)) {
+                return profile;
             }
         }

Review Comment:
   I've done this on purpose to stick to previous logic.
   
   But I guess we can use `valueMinusQuotes` for all statements.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists