Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

WILL_SORT_TEST

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class WillSortTest {

    public static void main(String args[]) {

        List<DataDupeEndpoint> endpointList = getTestEndpointList();

        System.out.println("ORIGINAL ENDPOINT LIST;");

        for (DataDupeEndpoint item : endpointList) {
            System.out.println(item.toString());
        }

        endpointList.sort(getEndpointComparator());

        System.out.println("\n\n");
        System.out.println("SORTED ENDPOINT LIST:");

        for (DataDupeEndpoint item : endpointList) {
            System.out.println(item.toString());
        }
    }

    private static List<DataDupeEndpoint> getTestEndpointList() {
        List<DataDupeEndpoint> endpointList = new ArrayList<>();

        DataDupeEndpoint dataDupeEndpoint1 = new DataDupeEndpoint()
        {{
            source = "TRVDEV";
            target = "TRVQA";
            schemaMap = Arrays.asList(new SchemaMap[] {
                    new SchemaMap() {{
                        sourceSchema = "DBO";
                        targetSchema = "BIRPT";
                    }},
                    new SchemaMap() {{
                        sourceSchema = "BIRPT";
                        targetSchema = "BIRPT";
                    }},
                    new SchemaMap() {{
                        sourceSchema = "A";
                        targetSchema = "B";
                    }},
                    new SchemaMap() {{
                        sourceSchema = "B";
                        targetSchema = "A";
                    }},
                    new SchemaMap() {{
                        sourceSchema = "A";
                        targetSchema = "A";
                    }},
            });
        }};

        DataDupeEndpoint dataDupeEndpoint2 = new DataDupeEndpoint()
        {{
            source = "TRVUAT";
            target = "TRVPRD_2";
        }};

        DataDupeEndpoint dataDupeEndpoint3 = new DataDupeEndpoint()
        {{
            source = "TRVDEV";
            target = "TRVQA";
            schemaMap = Arrays.asList(new SchemaMap[] {
                    new SchemaMap() {{
                        sourceSchema = "DBO";
                        targetSchema = "BIRPT2";
                    }},
                    new SchemaMap() {{
                        sourceSchema = "BIRPT";
                        targetSchema = "BIRPT";
                    }}
            });
        }};

        DataDupeEndpoint dataDupeEndpoint4 = new DataDupeEndpoint()
        {{
            source = "TRVDEV";
            target = "TRVQA";
            schemaMap = Arrays.asList(new SchemaMap[] {
                    /*new SchemaMap() {{
                        sourceSchema = "DBO";
                        targetSchema = "BIRPT1";
                    }},*/
                    new SchemaMap() {{
                        sourceSchema = "BIRPT";
                        targetSchema = "BIRPT";
                    }}
            });
        }};

        DataDupeEndpoint dataDupeEndpoint5 = new DataDupeEndpoint()
        {{
            source = "TRVUAT";
            target = "TRVPRD_1";
        }};

        DataDupeEndpoint dataDupeEndpoint6 = new DataDupeEndpoint()
        {{
            source = "TRVUAT";
            target = "TRVPRD_2";
            schemaMap = Arrays.asList(new SchemaMap[] {
                    new SchemaMap() {{
                        sourceSchema = "BIRPT";
                        targetSchema = "BIRPT";
                    }}
            });
        }};

        endpointList.add(dataDupeEndpoint6);
        endpointList.add(dataDupeEndpoint1);
        endpointList.add(dataDupeEndpoint2);
        endpointList.add(dataDupeEndpoint3);
        endpointList.add(dataDupeEndpoint4);
        endpointList.add(dataDupeEndpoint5);
        //endpointList.add(dataDupeEndpoint6);

        return endpointList;
    }

    private static Comparator getEndpointComparator() {
        Comparator endpointComparator = new Comparator() {

            @Override
            public int compare(Object o1, Object o2) {
                DataDupeEndpoint e1 = o1 instanceof DataDupeEndpoint ? (DataDupeEndpoint) o1 : null;
                DataDupeEndpoint e2 = o2 instanceof DataDupeEndpoint ? (DataDupeEndpoint) o2 : null;

                int sourceEndpointCompare = e1.source.compareToIgnoreCase(e2.source);
                int targetEndpointCompare = e1.target.compareToIgnoreCase(e2.target);

                if (sourceEndpointCompare != 0) {
                    return sourceEndpointCompare;
                } else {
                    if (targetEndpointCompare != 0) {
                        return targetEndpointCompare;
                    } else {
                        if (e1.schemaMap == null && e2.schemaMap == null) {
                            return 0;
                        } else if (e1.schemaMap == null && e2.schemaMap != null) {
                            return -1;
                        } else if (e1.schemaMap != null && e2.schemaMap == null) {
                            return 1;
                        } else {
                            e1.schemaMap.sort(schemaMapComparator);
                            e2.schemaMap.sort(schemaMapComparator);

                            int schemaMap1 = e1.schemaMap.size();
                            int schemaMap2 = e2.schemaMap.size();

                            if (schemaMap1 < schemaMap2) {
                                return -1;
                            } else if (schemaMap1 > schemaMap2) {
                                return 1;
                            } else {
                                for (int i = 0; i < schemaMap1; i++) {
                                    int sourceSchemaCompare = e1.schemaMap.get(i).sourceSchema.compareToIgnoreCase(e2.schemaMap.get(i).sourceSchema);
                                    int targetSchemaCompare = e1.schemaMap.get(i).targetSchema.compareToIgnoreCase(e2.schemaMap.get(i).targetSchema);

                                    if (sourceSchemaCompare != 0) {
                                        return sourceSchemaCompare;
                                    } else {
                                        if (targetSchemaCompare != 0) {
                                            return targetSchemaCompare;
                                        }
                                    }
                                }
                                return 0;
                            }
                        }
                    }
                }
            }

            Comparator schemaMapComparator = new Comparator() {
                @Override
                public int compare(Object o1, Object o2) {
                    SchemaMap s1 = o1 instanceof SchemaMap ? (SchemaMap) o1 : null;
                    SchemaMap s2 = o2 instanceof SchemaMap ? (SchemaMap) o2 : null;

                    int sourceSchemaCompare = s1.sourceSchema.compareToIgnoreCase(s2.sourceSchema);
                    int targetSchemaCompare = s1.targetSchema.compareToIgnoreCase(s2.targetSchema);

                    if (sourceSchemaCompare != 0) {
                        return sourceSchemaCompare;
                    } else {
                        return targetSchemaCompare;
                    }
                }
            };
        };
        return endpointComparator;
    }

    private static class DataDupeEndpoint {
        public String source;
        public String target;
        public List<SchemaMap> schemaMap;

        @Override
        public String toString() {
            return "DataDupeEndpoint{" +
                    "source='" + source + '\'' +
                    ", target='" + target + '\'' +
                    ", schemaMap=" + schemaMap +
                    '}';
        }
    }

    private static class SchemaMap {
        public String sourceSchema;
        public String targetSchema;

        @Override
        public String toString() {
            return "SchemaMap{" +
                    "sourceSchema='" + sourceSchema + '\'' +
                    ", targetSchema='" + targetSchema + '\'' +
                    '}';
        }
    }
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.