package com.chaoticdawgsoftware.algorithms.retrievers;

import java.security.Provider;
import java.security.Security;
import java.util.*;

/**
 * ProviderAlgorithmRetrievingFactory.java
 * Created by ChaoticDawg on 1/20/17.
 */

class ProviderAlgorithmRetrievingFactory {
    private static Provider[] providers = Security.getProviders();
    private static Set<String> keyNameSet = new HashSet<>();

    static ArrayList<String> getAlgorithmList(String algorithm) {
        String keyName = "";
        for (Provider provider : providers) {
            Enumeration<Object> providerKeys = provider.keys();
            while (providerKeys.hasMoreElements()) {
                Object key = providerKeys.nextElement();
                if (key.toString().startsWith(algorithm + ".")) {
                    keyName = key.toString().substring((algorithm + ".").length());
                }
                if (key.toString().startsWith("Alg.Alias." + algorithm + ".")) {
                    keyName = key.toString().substring(("Alg.Alias." + algorithm + ".").length());
                }
                String[] strings = keyName.split(" ");
                keyName = strings[0];
                if (!keyName.equals("")) {
                    keyNameSet.add(keyName);
                }
            }
        }

        return getSortedAlgorithmList();
    }

    private static ArrayList<String> getSortedAlgorithmList() {
        ArrayList<String> keyList = new ArrayList<>(keyNameSet);
        Collections.sort(keyList);
        return keyList;
    }
}

@@ -335,69 +335,8 @@
 s();
-%0A    private static Set%3CString%3E keyNameSet = new HashSet%3C%3E();
 %0A%0A  
@@ -411,79 +411,454 @@
     
-String keyName = %22%22;%0A        for (Provider provider : providers) %7B%0A    
+return getSortedAlgorithmList(buildAlgorithmList(algorithm));%0A    %7D%0A%0A    private static ArrayList%3CString%3E buildAlgorithmList(String algorithm) %7B%0A        Set%3CString%3E keyNameSet = new HashSet%3C%3E();%0A        for (Provider provider : providers)%0A            getKeyList(algorithm, keyNameSet, provider);%0A%0A        return new ArrayList%3C%3E(keyNameSet);%0A    %7D%0A%0A    private static void getKeyList(String algorithm, Set%3CString%3E keyNameSet, Provider provider) %7B%0A
     
@@ -921,20 +921,16 @@
         
-    
 while (p
@@ -959,18 +959,16 @@
 ments())
- %7B
 %0A       
@@ -976,24 +976,45 @@
     
-    Object key =
+addKeyNameToSet(getKeyName(algorithm,
  pro
@@ -1040,46 +1040,385 @@
 nt()
+), keyNameSet)
 ;%0A    
+%7D%0A%0A
     
-        if (key.toString().s
+private static String getKeyName(String algorithm, Object key) %7B%0A        String keyName = checkKeyBeginning(algorithm, key);%0A%0A        return (keyName.split(%22 %22))%5B0%5D;%0A    %7D%0A%0A    private static String checkKeyBeginning(String algorithm, Object key) %7B%0A        String keyName = checkKeyStartsWithAlgorithmName(algorithm, key);%0A        keyName = checkKeyS
 tart
@@ -1422,16 +1422,21 @@
 artsWith
+Alias
 (algorit
@@ -1441,129 +1441,399 @@
 ithm
- + %22.%22)) %7B%0A         
+, key, keyName);%0A        return keyName;%0A
     
+%7D%0A%0A
     
-   keyName = key.toString().substring((algorithm + %22.%22).length());%0A                %7D%0A        
+private static String checkKeyStartsWithAlgorithmName(String algorithm, Object key) %7B%0A        if (key.toString().startsWith(algorithm + %22.%22))%0A            return key.toString().substring((algorithm + %22.%22).length());%0A        return %22%22;%0A    %7D%0A%0A    private static String checkKeyStartsWithAlias(String algorithm, Object key, String keyName) %7B%0A
     
@@ -1898,22 +1898,17 @@
  + %22.%22))
- %7B%0A   
+%0A
         
@@ -1903,37 +1903,32 @@
 %22))%0A            
-     
 keyName = key.to
@@ -2002,119 +2002,113 @@
     
-  
+return keyName;%0A
     
-  
 %7D
+%0A
 %0A    
-            String%5B%5D strings = keyName.split(%22 %22);%0A                keyName = strings%5B0%5D;%0A        
+private static void addKeyNameToSet(String keyName, Set%3CString%3E keyNameSet) %7B%0A
     
@@ -2135,18 +2135,16 @@
 als(%22%22))
- %7B
 %0A       
@@ -2144,32 +2144,24 @@
             
-        
 keyNameSet.a
@@ -2177,92 +2177,8 @@
 e);%0A
-                %7D%0A            %7D%0A        %7D%0A%0A        return getSortedAlgorithmList();%0A
     
@@ -2240,28 +2240,16 @@
 thmList(
-) %7B%0A        
 ArrayLis
@@ -2269,39 +2269,11 @@
 List
- = new ArrayList%3C%3E(keyNameSet);
+) %7B
 %0A   

Refactor ProviderAlgorithmRetrievingFactory