1 /*
2  *  (C) 2001 by Argonne National Laboratory
3  *      See COPYRIGHT in top-level directory.
4  */
5 
6 /*
7  *  @author  Anthony Chan
8  */
9 package viewer.legends;
10 
11 import java.util.Comparator;
12 
13 // import base.drawable.Topology;
14 import base.drawable.Category;
15 import viewer.common.Parameters;
16 
17 public class LegendComparators
18 {
19     public  static final Comparator INDEX_ORDER
20                                     = new IndexOrder();
21     private static final Comparator PREVIEW_ORDER
22                                     = new PreviewOrder();
23     private static final Comparator TOPOLOGY_ORDER
24                                     = new TopologyOrder();
25     public  static final Comparator CASE_SENSITIVE_ORDER
26                                     = new CaseSensitiveOrder();
27     public  static final Comparator CASE_INSENSITIVE_ORDER
28                                     = new CaseInsensitiveOrder();
29     public  static final Comparator LONG_NAME_ORDER
30                                     = new LongNameOrder();
31     public  static final Comparator COUNT_ORDER
32                                     = new CountOrder();
33     public  static final Comparator INCL_RATIO_ORDER
34                                     = new InclusiveRatioOrder();
35     public  static final Comparator EXCL_RATIO_ORDER
36                                     = new ExclusiveRatioOrder();
37 
38     public static class IndexOrder implements Comparator
39     {
compare( Object o1, Object o2 )40         public int compare( Object o1, Object o2 )
41         {
42             Category type1 = (Category) o1;
43             Category type2 = (Category) o2;
44             return type1.getIndex() - type2.getIndex();
45         }
46     }
47 
48     /*
49        This comparator gives preference over Preview drawable
50        All Preview object's category indexes are negative as defined in
51        logformat/clogTOdrawable/InputLog.java & logformat/trace/InputLog.java.
52     */
53     public static class PreviewOrder implements Comparator
54     {
compare( Object o1, Object o2 )55         public int compare( Object o1, Object o2 )
56         {
57             Category type1  = (Category) o1;
58             Category type2  = (Category) o2;
59             int      pview1 = ( type1.getIndex() < 0 ? 0 : 1 );
60             int      pview2 = ( type2.getIndex() < 0 ? 0 : 1 );
61             return pview1 - pview2;
62         }
63     }
64 
65     public static class TopologyOrder implements Comparator
66     {
compare( Object o1, Object o2 )67         public int compare( Object o1, Object o2 )
68         {
69             Category type1      = (Category) o1;
70             Category type2      = (Category) o2;
71             return   type2.getTopology().hashCode()
72                    - type1.getTopology().hashCode();
73             // intentionally reversed, so arrow < state < event
74         }
75     }
76 
77     public static class CaseSensitiveOrder implements Comparator
78     {
compare( Object o1, Object o2 )79         public int compare( Object o1, Object o2 )
80         {
81             Category type1      = (Category) o1;
82             Category type2      = (Category) o2;
83             int      diff       = 0;
84             if ( Parameters.LEGEND_TOPOLOGY_ORDER ) {
85                 diff = TOPOLOGY_ORDER.compare( type1, type2 );
86                 if ( diff != 0 )
87                     return diff;
88             }
89             if ( Parameters.LEGEND_PREVIEW_ORDER ) {
90                 diff = PREVIEW_ORDER.compare( type1, type2 );
91                 if ( diff != 0 )
92                     return diff;
93             }
94             return type1.getName().compareTo( type2.getName() );
95         }
96     }
97 
98     public static class CaseInsensitiveOrder implements Comparator
99     {
compare( Object o1, Object o2 )100         public int compare( Object o1, Object o2 )
101         {
102             Category type1      = (Category) o1;
103             Category type2      = (Category) o2;
104             int      diff       = 0;
105             if ( Parameters.LEGEND_TOPOLOGY_ORDER ) {
106                 diff = TOPOLOGY_ORDER.compare( type1, type2 );
107                 if ( diff != 0 )
108                     return diff;
109             }
110             if ( Parameters.LEGEND_PREVIEW_ORDER ) {
111                 diff = PREVIEW_ORDER.compare( type1, type2 );
112                 if ( diff != 0 )
113                     return diff;
114             }
115             return type1.getName().compareToIgnoreCase( type2.getName() );
116         }
117     }
118 
119     public static class LongNameOrder implements Comparator
120     {
compare( Object o1, Object o2 )121         public int compare( Object o1, Object o2 )
122         {
123             Category type1      = (Category) o1;
124             Category type2      = (Category) o2;
125             return type1.getName().length() - type2.getName().length();
126         }
127     }
128 
129     public static class CountOrder implements Comparator
130     {
compare( Object o1, Object o2 )131         public int compare( Object o1, Object o2 )
132         {
133             Category type1 = (Category) o1;
134             Category type2 = (Category) o2;
135             long ldiff =   type1.getSummary().getDrawableCount()
136                          - type2.getSummary().getDrawableCount();
137             return ( ldiff < 0 ? -1 : ( ldiff == 0 ? 0 : 1 ) );
138         }
139     }
140 
141     public static class InclusiveRatioOrder implements Comparator
142     {
compare( Object o1, Object o2 )143         public int compare( Object o1, Object o2 )
144         {
145             Category type1 = (Category) o1;
146             Category type2 = (Category) o2;
147             float fdiff =   type1.getSummary().getRatio(true)
148                           - type2.getSummary().getRatio(true);
149             return ( fdiff < 0.0f ? -1 : ( fdiff == 0.0f ? 0 : 1 ) );
150         }
151     }
152 
153     public static class ExclusiveRatioOrder implements Comparator
154     {
compare( Object o1, Object o2 )155         public int compare( Object o1, Object o2 )
156         {
157             Category type1 = (Category) o1;
158             Category type2 = (Category) o2;
159             float fdiff =   type1.getSummary().getRatio(false)
160                           - type2.getSummary().getRatio(false);
161             return ( fdiff < 0.0f ? -1 : ( fdiff == 0.0f ? 0 : 1 ) );
162         }
163     }
164 }
165