package com.jscape.util.net.ssl;

import java.util.Comparator;

/**
 * @author Alex Usun
 */
public class PerfectForwardSecrecyCipherSuiteComparator
        implements Comparator<String> {

    private static final String[] SUB_STRINGS = {
            "TLS_ECDHE_",
            "TLS_DHE_",
            "_RSA_",
            "_ECDSA_",
            "_DSS_",
            "_256_",
            "_128_",
            "_SHA384",
            "_SHA256",
            "_SHA"
    };

    @Override
    public int compare(String s1, String s2) {
        for (String subString : SUB_STRINGS) {
            int nullCipherResult = compareContent(s1, s2, "_WITH_NULL_");
            if (nullCipherResult != 0) {
                return -nullCipherResult;
            }

            int result = compareContent(s1, s2, subString);
            if (result != 0) {
                return result;
            }
        }

        return 0;
    }

    private int compareContent(String s1, String s2, String substring) {
        boolean substringFound1 = s1.contains(substring);
        boolean substringFound2 = s2.contains(substring);

        if (substringFound1 && !substringFound2) {
            return -1;
        } else if (!substringFound1 && substringFound2) {
            return 1;
        }

        return 0;
    }
}
