001package io.ebeaninternal.server.autotune.service; 002 003import io.ebeaninternal.server.autotune.model.Autotune; 004import io.ebeaninternal.server.autotune.model.Origin; 005import io.ebeaninternal.server.autotune.model.ProfileDiff; 006import io.ebeaninternal.server.autotune.model.ProfileEmpty; 007import io.ebeaninternal.server.autotune.model.ProfileNew; 008 009import java.util.Comparator; 010import java.util.List; 011 012/** 013 * Sorts Autotune document by 014 */ 015public class SortAutoTuneDocument { 016 017 018 /** 019 * Set the diff and new entries by bean type followed by key. 020 */ 021 public static void sort(Autotune document) { 022 023 ProfileDiff profileDiff = document.getProfileDiff(); 024 if (profileDiff != null) { 025 profileDiff.getOrigin().sort(NAME_KEY_SORT); 026 } 027 ProfileNew profileNew = document.getProfileNew(); 028 if (profileNew != null) { 029 profileNew.getOrigin().sort(NAME_KEY_SORT); 030 } 031 ProfileEmpty profileEmpty = document.getProfileEmpty(); 032 if (profileEmpty != null) { 033 profileEmpty.getOrigin().sort(KEY_SORT); 034 } 035 List<Origin> origins = document.getOrigin(); 036 if (!origins.isEmpty()) { 037 origins.sort(NAME_KEY_SORT); 038 } 039 } 040 041 private static final OriginNameKeySort NAME_KEY_SORT = new OriginNameKeySort(); 042 043 private static final OriginKeySort KEY_SORT = new OriginKeySort(); 044 045 /** 046 * Comparator sort by bean type then key. 047 */ 048 private static class OriginNameKeySort implements Comparator<Origin> { 049 050 @Override 051 public int compare(Origin o1, Origin o2) { 052 int comp = o1.getBeanType().compareTo(o2.getBeanType()); 053 if (comp == 0) { 054 comp = o1.getKey().compareTo(o2.getKey()); 055 } 056 return comp; 057 } 058 } 059 060 /** 061 * Comparator sort by bean type then key. 062 */ 063 private static class OriginKeySort implements Comparator<Origin> { 064 065 @Override 066 public int compare(Origin o1, Origin o2) { 067 return o1.getKey().compareTo(o2.getKey()); 068 } 069 } 070}